在本项目中再添加实现用户注册的功能
1、添加userRegister.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
2、修改UserInfoServlet以实现对用户注册表单的请求响应
package com.px1987.j2eeweb.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.px1987.j2eeweb.model.*;
public class UserInfoServlet extends HttpServlet
{
public UserInfoServlet() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
out
.println("");
out.println("");
out.println("
out.println("
");out.print(" This is ");
out.print(this.getClass());
out.println(", 按时广泛特色" );
out.println(" ");
out.println("");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
request.setCharacterEncoding("gb2312"); //Struts ?
String menuID= request.getParameter("menuID");
int menuIDDigit=Integer.parseInt(menuID);
switch(menuIDDigit)
{
case 1: //用户登录吗
goUserLogin(request,response);
break;
case 2: //用户注册吗
goUserRegister(request,response);
break;
case 3: //用户密码修改吗
goUpdateUserPassWord(request,response);
break;
}
}
public void goUpdateUserPassWord(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
}
public void goUserRegister(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
String userName= request.getParameter("userName");
String userPassWord= request.getParameter("userPassWord");
String userDepartment= request.getParameter("userDepartment");
String userAdminLevel= request.getParameter("userAdminLevel");
String departAdminLevel= request.getParameter("departAdminLevel");
String userImage= request.getParameter("userImage");
UserInfoVO userInfoVOBean=new UserInfoVO();
userInfoVOBean.setUserName(userName);
userInfoVOBean.setUserPassWord(userPassWord);
userInfoVOBean.setUserDepartment(userDepartment);
userInfoVOBean.setUserAdminLevel(Integer.parseInt(userAdminLevel));
userInfoVOBean.setDepartAdminLevel(Integer.parseInt(departAdminLevel));
userInfoVOBean.setUserImage(userImage);
UserInfoManage userInfoManageBean=new UserInfoManage();
boolean okOrNot=userInfoManageBean.doUserRegister(userInfoVOBean);
if(okOrNot)
{
RequestDispatcher oneRequestDispatcher=request.getRequestDispatcher("index.jsp");
oneRequestDispatcher.forward(request, response);
}
else
{
RequestDispatcher oneRequestDispatcher=request.getRequestDispatcher("loginFailure.jsp");
oneRequestDispatcher.forward(request, response);
}
}
public void goUserLogin(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
String userName= request.getParameter("userName");
//重构
// userName=new String(userName.getBytes("ISO-8859-1"),"gb2312");
String userPassWord= request.getParameter("userPassWord"); //
UserInfoManage userInfoManageBean=new UserInfoManage();
UserInfoVO userInfoVOBean=new UserInfoVO();
userInfoVOBean.setUserName(userName); //也可以采用
userInfoVOBean.setUserPassWord(userPassWord);
boolean okOrNot=userInfoManageBean.doUserLogin(userInfoVOBean);
request.setAttribute("userNameString", userName);
if(okOrNot)
{
// response.sendRedirect("/J2EEWebApp/loginSuccess.jsp");
//转发
RequestDispatcher oneRequestDispatcher=request.getRequestDispatcher("loginSuccess.jsp");
oneRequestDispatcher.forward(request, response);
}
else
{
RequestDispatcher oneRequestDispatcher=request.getRequestDispatcher("loginFailure.jsp");
oneRequestDispatcher.forward(request, response);
}
}
/**
* Initialization of the servlet.
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
3、修改前面的UserInfoManage 业务组件类以实现用户注册功能的处理
package com.px1987.j2eeweb.model;
import com.px1987.j2eeweb.dao.*;
import java.util.*;
public class UserInfoManage
{
public UserInfoManage()
{
// TODO 自动生成构造函数存根
}
public boolean doUserLogin(UserInfoVO oneUserInfoVO)
{
String userName=oneUserInfoVO.getUserName();
String userPassWord=oneUserInfoVO.getUserPassWord();
boolean okOrNot=false;
UserManageDAOInterface oneUserManageDAOJDBCImple=new UserManageDAOJDBCImple();
UserInfoPO oneUserInfoPO=
oneUserManageDAOJDBCImple.SelectOneUserInfoData(userName, userPassWord);
if(oneUserInfoPO==null)
{
okOrNot=false;
}
else
{
okOrNot=true;
}
// boolean okOrNot=userName.equals("admin")&&userPassWord.equals("1234");
return okOrNot;
}
public boolean doUserRegister(UserInfoVO oneUserInfoVO)
{
UserManageDAOInterface oneUserManageDAOJDBCImple=new UserManageDAOJDBCImple();
UserInfoPO oneUserInfoPO=new UserInfoPO();
oneUserInfoPO.setUserName(oneUserInfoVO.getUserName());
oneUserInfoPO.setUserPassWord(oneUserInfoVO.getUserPassWord());
oneUserInfoPO.setUserAdminLevel(oneUserInfoVO.getUserAdminLevel());
oneUserInfoPO.setUserDepartment(oneUserInfoVO.getUserDepartment());
oneUserInfoPO.setUserImage(oneUserInfoVO.getUserImage());
oneUserInfoPO.setDepartAdminLevel(oneUserInfoVO.getDepartAdminLevel());
oneUserInfoPO.setRegisterTime( new Date().toLocaleString());
oneUserInfoPO.setId((int) new Date().getTime() );
boolean okOrNot=false;
okOrNot=oneUserManageDAOJDBCImple.InsertOneUserInfo(oneUserInfoPO);
return okOrNot;
}
}
4、修改前面的UserManageDAOJDBCImple类以完成用户注册功能的数据访问操作
package com.px1987.j2eeweb.dao;
import java.util.ArrayList;
import java.util.Map;
import java.sql.*;
public class UserManageDAOJDBCImple implements UserManageDAOInterface
{
private java.sql.Connection con=null;
ConnectDBInterface connectDBBean=null;
public UserManageDAOJDBCImple()
{
connectDBBean=new ConnectDBBean();
}
public boolean BatchDeleteUserInfo(ArrayList deletedUserIDs) {
// TODO 自动生成方法存根
return false;
}
public boolean DeleteOneUserInfo(int deletedUserID) {
// TODO 自动生成方法存根
return false;
}
public boolean InsertOneUserInfo(UserInfoPO oneUserInfoPO)
{
String insert_SqlStatement=null;
insert_SqlStatement="insert into userInfo values(?,?,?,?,?,?,?,?)";
con=connectDBBean.getConnection();
try
{
java.sql.PreparedStatement pstmt = con.prepareStatement(insert_SqlStatement,
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
pstmt.setString(1, oneUserInfoPO.getUserName());
pstmt.setString(2, oneUserInfoPO.getUserPassWord());
pstmt.setString(3, oneUserInfoPO.getUserDepartment());
pstmt.setInt(4, oneUserInfoPO.getUserAdminLevel());
pstmt.setInt(5, oneUserInfoPO.getDepartAdminLevel());
pstmt.setString(6, oneUserInfoPO.getUserImage());
pstmt.setString(7, oneUserInfoPO.getRegisterTime());
pstmt.setInt(8, oneUserInfoPO.getId());
pstmt.executeUpdate();
}
catch(SQLException e)
{
System.out.println("在查询数据库表时出现错误!");
return false;
}
return true;
}
public ArrayList SelectAllUserInfoData() {
// TODO 自动生成方法存根
return null;
}
public UserInfoPO SelectOneUserInfoData(String userName, String userPassWord)
{
ResultSet rs=null;
UserInfoPO oneUserInfo=null;
String select_SqlStatement=null;
select_SqlStatement="select * from userInfo where userName='"+
userName+"' and userPassWord='"+userPassWord+"'";
con=connectDBBean.getConnection();
try
{
java.sql.PreparedStatement pstmt = con.prepareStatement(select_SqlStatement,
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
rs = pstmt.executeQuery();
if(rs.next())
{
oneUserInfo=new UserInfoPO(); //目前只转换下面的几个属性
oneUserInfo.setUserName(rs.getString("userName"));
oneUserInfo.setUserPassWord(rs.getString("userPassWord"));
oneUserInfo.setUserDepartment(rs.getString("userDepartment"));
}
else
{
oneUserInfo=null;
}
}
catch(SQLException e)
{
System.out.println("在查询数据库表时出现错误!");
}
return oneUserInfo;
}
public ArrayList SelectSomeUserInfoData(Map whereMap) {
// TODO 自动生成方法存根
return null;
}
public boolean UpdateOneUserInfo(UserInfoPO oneUserInfoPO) {
// TODO 自动生成方法存根
return false;
}
}
5、部署并执行本系统
http://127.0.0.1:8080/J2EEWebApp/userRegister.jsp