JSP+JavaBean+Servlet用户注册登录请求的处理

这个框架的搭建建立在上一次JSP+JavaBean的基础之上,进一步为了JSP只是当做页面,所以将链接JavaBean和jsp页面的LoginCheck.jsp和RegistCheck.jsp替换为servlet来处理这些请求和相应(servlet生而就是为了这两个工作的)

在servlet的编写上,由于老韩有些不同,刚刚是将老韩的数据库操作都封装到了一个DBAccess里面,当然,现在只有一张users表,如果有多张表,显然是不可取的,其实这本书就是讲ConnDb.java 和UserBean.java与UserBeanCL.java  进行了综合,变成了两个java,事实上都是差不多的,在表格少且清晰的情况下还是可以的,在表格多的时候还是不要为好,关键点还是代码的重用问题,我还是倾向于老韩的。

其实现在看来还是DBAccess这种方式的代码重复利用率较高

UserServlet.java

package com.gxm.controller;
//servlet里面是不会出现操作数据库的方法的
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.gxm.jspandbean.UserBean;

public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method = request.getParameter("method");
		if(method == null){
			PrintWriter out = response.getWriter();
			out.println("invalid request");
		}
		else if(method.equals("login")){
			this.login(request,response);
		}else if(method.equals("logout")){
			this.logout(request,response);
		}else if(method.equals("register")){
			this.register(request,response);
		}
	}
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}
    
	protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String password  = request.getParameter("password");
		//check null
		if(username == null||password == null){
			response.sendRedirect("login.jsp");
			//这个应该可以防止那个错误
			return;
		}
		UserBean ub = new UserBean();
		if(ub.isvalid(username, password)){
			HttpSession session = request.getSession();
			//session.setAttribute("username",username);
			request.setAttribute("username",username);
			request.getRequestDispatcher("welcome.jsp").forward(request,response);
			return;
		}else{
			request.getRequestDispatcher("login.jsp").forward(request,response);
		}
	}
	
	protected void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		session.removeAttribute("username");
		request.getRequestDispatcher("login.jsp").forward(request,response);
		return;
	}
	
	protected void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String password1  = request.getParameter("password1");
		String password2  = request.getParameter("password2");
		if(username == null||password1 == null||password2 == null||password1 == password2){
			response.sendRedirect("register.jsp");
			return;
		}
		
		UserBean ub = new UserBean();
		if(ub.isExist(username)){
			request.getRequestDispatcher("register.jsp").forward(request,response);
			return;
		}else{
			
			if(ub.add(username, password1)){
				request.getRequestDispatcher("login.jsp").forward(request,response);
				return;
			}
		}
		
	}
	
	
}


 

 

你可能感兴趣的:(jsp)