📄 userregister.java
字号:
/**
* UserRegister是处理用户注册的Servlet
* 它负责检验来自表单userRegister.jsp的数据
* 并将合法信息写入数据库,并转到registerSuccess.jsp
* 如果信息不合法,将连同错误信息转回userRegister.jsp
*/
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import bean.UserInf;
import bean.DBClass;
import bean.StrClass;
public class UserRegister extends HttpServlet{
/* 覆盖doGet方法 */
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String toJsp="/registerSuccess.jsp"; //toJsp用于储存本Servlet将要转到的JSP地址
UserInf errInf=new UserInf(); //errInf用于储存错误提示信息
StrClass str=new StrClass();
/* 获取表单数据并存储在user类中 */
request.setCharacterEncoding("gb2312");
String id=request.getParameter("id").trim();
String pwd=request.getParameter("password");
String checkPwd=request.getParameter("checkPwd");
String name=request.getParameter("name").trim();
String sex=request.getParameter("sex").trim();
String address=request.getParameter("address").trim();
String code=request.getParameter("code").trim();
String tel=request.getParameter("tel").trim();
String email=request.getParameter("email").trim();
UserInf user=new UserInf(id,pwd,name,sex,address,code,tel,email);
/* 检验申请帐号是否合法 */
if(id.equals("")){
errInf.setId("*本项必须填写!");
toJsp="/userRegister.jsp";
}else if(id.length()>10||id.length()<4){
errInf.setId("*长度不符合要求!");
user.setId("");
toJsp="/userRegister.jsp";
}else if(isExist(id)){
errInf.setId("*帐号已经存在!");
user.setId("");
toJsp="/userRegister.jsp";
}
/* 检验申请密码是否合法 */
if(pwd.equals("")){
errInf.setPassword("*本项必须填写!");
toJsp="/userRegister.jsp";
}else if(pwd.length()>12||pwd.length()<6){
errInf.setPassword("*长度不符合要求!");
user.setPassword("");
toJsp="/userRegister.jsp";
}else if(!pwd.equals(checkPwd)){
errInf.setPassword("*与确认密码不符!");
user.setPassword("");
toJsp="/userRegister.jsp";
}
/* 检验用户姓名是否合法 */
if(name.equals("")){
errInf.setName("*本项必须填写!");
toJsp="/userRegister.jsp";
}
/* 检验用户地址是否合法 */
if(address.equals("")){
errInf.setAddress("*本项必须填写!");
toJsp="/userRegister.jsp";
}else if(address.length()<10){
errInf.setAddress("*请填写详细地址!");
user.setAddress("");
toJsp="/userRegister.jsp";
}
/* 检验邮编是否合法 */
if(code.equals("")){
errInf.setCode("*本项必须填写!");
toJsp="/userRegister.jsp";
}else if(code.length()!=6){
errInf.setCode("*邮编应该为6位数字!");
user.setCode("");
toJsp="/userRegister.jsp";
}else {
str.setStr(code);
if(!str.isNum()){
errInf.setCode("*邮编应该为6位数字!");
user.setCode("");
toJsp="/userRegister.jsp";
}
}
/* 检验电话是否合法 */
if(tel.equals("")){
errInf.setTel("*本项必须填写!");
toJsp="/userRegister.jsp";
}else {
str.setStr(tel);
if(!str.isNum()){
errInf.setTel("*电话号码应该为数字!");
user.setTel("");
toJsp="/userRegister.jsp";
}
}
/* 检验E-mail是否合法 */
if(email.equals("")){
errInf.setEmail("*本项必须填写!");
toJsp="/userRegister.jsp";
}else {
str.setStr(email);
if(!str.isEmail()){
errInf.setEmail("*E-mial地址有误!");
user.setEmail("");
toJsp="/userRegister.jsp";
}
}
/* 如果没有不合法项则将注册信息写入数据库 */
if(!toJsp.equals("/userRegister.jsp")){
String command="INSERT INTO customer VALUES("
+ "\'"+ id + "\'"+ ","
+ "\'"+ pwd + "\'"+ ","
+ "\'"+ name + "\'"+ ","
+ "\'"+ sex + "\'"+ ","
+ "\'"+ address + "\'"+ ","
+ "\'"+ code + "\'"+ ","
+ "\'"+ tel + "\'"+ ","
+ "\'"+ email + "\'"+ ")";
DBClass db=new DBClass();
db.connect();
db.executeUpdate(command);
db.closeConnection();
}
request.setAttribute("errInf", errInf);
request.setAttribute("userInf", user);
RequestDispatcher dispatcher =
request.getRequestDispatcher(toJsp);
dispatcher.forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
public boolean isExist(String id){
boolean bool=false;
String query="SELECT * FROM customer WHERE id="+"\'"+ id + "\'";
DBClass db=new DBClass();
db.connect();
ResultSet resultset=db.executeQuery(query);
try{
if(resultset.next()){
bool=true;
}
}catch(SQLException sqle){
System.err.println("Erro with connection:"+sqle);
}
db.closeConnection();
return bool;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -