package org.entity;
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import nl.captcha.Captcha;
import com.opensymphony.xwork2.ActionSupport;
public class RegistrationAction extends ActionSupport {
BloodBank register;
private Map<String, Object> session;
public BloodBank getRegister() {
return register;
}
public void setRegister(BloodBank register) {
this.register = register;
}
public Map<String, Object> getSession() {
return session;
}
public void setSession(Map<String, Object> session) {
this.session = session;
}
@Override
public String execute() throws Exception {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/foryou", "root", "siddheshkk");
System.out.println("Driver Loaded");
PreparedStatement st = con
.prepareStatement("insert into bbinfo(code,name,address,city,district,contactno,password,aname,email) "
+ "values(?,?,?,?,?,?,?,?,?)");
st.setString(1, register.getCode());
st.setString(2, register.getBbname());
st.setString(3, register.getAddress());
st.setString(4, register.getCity());
st.setString(5, register.getDistrict());
st.setString(6, register.getNumber());
st.setString(7, register.getPassword());
st.setString(8, register.getAname());
st.setString(9, register.getEmail());
st.executeUpdate();
st.close();
PreparedStatement st1 = con
.prepareStatement("insert into stockinfo(name,city,address,contact,email,code,apositive,anegative,bpositive,bnegative,abpositive,abnegative,opositive,onegative)"
+ "values(?,?,?,?,?,?,'0','0','0','0','0','0','0','0')");
st1.setString(1, register.getBbname());
st1.setString(2, register.getCity());
st1.setString(3, register.getAddress());
st1.setString(4, register.getNumber());
st1.setString(5, register.getEmail());
st1.setString(6, register.getCode());
st1.executeUpdate();
st1.close();
con.close();
System.out.println("done");
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}
@Override
public void validate() {
super.validate();
if (register.getCode().length() < 1 | register.getCode().equals("")) {
addFieldError("register.code", "Mandatory Field.");
}
if (register.getBbname().equals("") | register.getBbname().length() < 1) {
addFieldError("register.bbname", "Invalid Name");
}
if (register.getAddress().equals("")) {
addFieldError("register.address", "Invalid Address");
}
if (register.getCity().equals("")) {
addFieldError("register.city", "Invalid City");
}
if (register.getDistrict().equals("")) {
addFieldError("register.district", "Invalid District");
}
if (register.getPassword().length() > 8
| register.getPassword().equals("")) {
addFieldError("register.password", "Please Enter a valid Password");
}
if (!(register.getConfirm().equals(register.getPassword()))) {
addFieldError("register.confirm", "Password doesnt match");
}
if (register.getConfirm().isEmpty()) {
addFieldError("register.confirm", "Please enter confirmed password");
}
if (register.getAname().equals("")) {
addFieldError("register.aname", "Please enter your name");
;
}
if (register.getEmail().equals("")) {
addFieldError("register.email", "Invalid Email Id");
} else {
String expression = "^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
CharSequence inputStr = register.getEmail();
Pattern pattern = Pattern.compile(expression,
Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);
if (!matcher.matches())
addFieldError("register.email", "Invalid email address");
}
Captcha captcha = (Captcha) session.get(Captcha.NAME);
if (!(captcha.isCorrect(register.getCaptchaAnswer()))) {
addFieldError("register.captchaAnswer", "incorrect captcha");
}
}
}
每当我尝试在第123行注册给出空指针异常时,这是我的动作类。
这是Captcha代码从validate
方法开始的地方:
Captcha captcha = (Captcha) session.get(Captcha.NAME);
if (!(captcha.isCorrect(register.getCaptchaAnswer()))) {
addFieldError("register.captchaAnswer", "incorrect captcha");
}
}
}
我也附加了我的错误文件。下面是我的jsp文件。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Blood Bank Section</title>
<style type="text/css">
body {
color: black;
font-family: sans-serif;
font: italic;
}
h1 {
color: black;
background-color: black;
font: bold;
outline-color: gray;
font-style: oblique;
font-family: cursive;
}
h3 {
color: yellow;
background-color: olive;
}
.loginDialog {
position: fixed;
font-family: Arial, Helvetica, sans-serif;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: rgba(0, 0, 0, 0.8);
z-index: 0;
opacity: 0;
-webkit-transition: opacity 400ms ease-in;
-moz-transition: opacity 400ms ease-in;
transition: opacity 400ms ease-in;
pointer-events: none;
}
.loginDialog>div {
width: 400px;
position: relative;
margin: 10% auto;
padding: 5px 20px 13px 20px;
border-radius: 10px;
background: #fff;
background: -moz-linear-gradient(#fff, #999);
background: -webkit-linear-gradient(#fff, #999);
background: -o-linear-gradient(#fff, #999);
}
.close {
background: #606061;
color: #FFFFFF;
line-height: 25px;
position: absolute;
right: -12px;
text-align: center;
top: -10px;
width: 24px;
text-decoration: none;
font-weight: bold;
-webkit-border-radius: 12px;
-moz-border-radius: 12px;
border-radius: 12px;
-moz-box-shadow: 1px 1px 3px #000;
-webkit-box-shadow: 1px 1px 3px #000;
box-shadow: 1px 1px 3px #000;
}
.close:hover {
background: #00d9ff;
}
form p {
text-align: center;
}
form p input {
font-size: 16px;
height: 28px;
}
.loginDialog:target {
opacity: 1;
pointer-events: auto;
}
</style>
</head>
<body>
<a href="welcome.jsp">Home</a>     
<a href="Admin.jsp">Admin Panel</a>     
<a href="BloodBankSection.jsp">Blood Bank Section</a>     
<a href="#login">Blood Bank Login</a>     
<a href=DonorSection.jsp>Donor Section</a>     
<a href="Feedback.jsp">Feedback</a>     
<a href="aboutus.jsp">About Us</a>
</br>
</br>
<h5>Please Register to sign in.</h5>
<h3 align="left">Registration Details:</h3>
<h3 align="left">----------------------------</h3>
<div align="left">
<s:form action="registration" method="post"
onsubmit="return validateForm()">
<s:textfield label="Code " name="register.code" />
<s:textfield label="Blood Bank Name " name="register.bbname" />
<s:textfield label="Address " name="register.address" />
<s:textfield label="City " name="register.city" />
<s:textfield label="District " name="register.district" />
<s:textfield label="Contact Number " name="register.number" />
<s:textfield label="Email Id " name="register.email" />
<s:password label="Password " name="register.password" />
<s:password label="Confirm Password" name="register.confirm" />
<s:textfield label="Enter your name " name="register.aname" />
<tr>
<td></td>
<td><img src="<c:url value="simpleCaptcha.png" />" /></td>
</tr>
<s:textfield label="Enter code" key="register.captchaAnswer" size="30" />
<s:submit value="Register" />
</s:form>
<h5 align="left">Already a member?? then sign in.</h5>
</div>
<div id="login" class="loginDialog">
<div>
<a href="#close" title="close">X</a>
<h2>Login to web App</h2>
<form action="LoginAction" method="post">
<p>
<input type="text" name="login.code" placeholder="Registered Code">
</p>
<p>
<input type="password" name="login.password" placeholder="Password">
</p>
<p class="remember_me">
<label> <input type="checkbox" name="remember_me">Remember
me on this computer
</label>
</p>
<p>
<a href="ForgotPassword.jsp">Forgot Password ?</a>
</p>
<p class="submit">
<input type="submit" value="login">
</p>
</form>
</div>
</div>
</body>
</html>
答案 0 :(得分:0)
请检查您是否在可能的I18N属性文件中添加了 register.captchaAnswer 条目。
<s:textfield label="Enter code" key="register.captchaAnswer" size="30" />