用工厂模式设计servlet

1建立action接口,让所有的servlet实现action的接口

package cn.it.houserent.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public interface Action {
	public String execute(HttpServletRequest request,HttpServletResponse response);
}

2写servlet类实现接口execute方法

登陆servlet

package cn.it.houserent.action;

import java.io.IOException;
import java.util.List;

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

import cn.it.houserent.bean.House;
import cn.it.houserent.bean.User;
import cn.it.houserent.biz.UserBiz;
import cn.it.houserent.biz.impl.HouseBizImpl;
import cn.it.houserent.biz.impl.UserBizImpl;

public class LoginServlet implements Action{

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) {
		String result = "";
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		UserBiz userBiz = new UserBizImpl();
		User user = userBiz.login(username, password);
		if (user==null){
			request.setAttribute("message", "帐号或密码不正确!");
			result="login.jsp";
		}else{
			List list=new HouseBizImpl().getHouseByUser(user);
			request.getSession().setAttribute("list", list);
			request.getSession().setAttribute("login", user);
			result ="page/manage.html";
		}
		return result;
	}

}

注册servlet

package cn.it.houserent.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.it.houserent.bean.User;
import cn.it.houserent.biz.UserBiz;
import cn.it.houserent.biz.impl.UserBizImpl;

public class RegistServlet implements Action {

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) {
		String result = "";

		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String telephone=request.getParameter("telephone");
		String name=request.getParameter("name");
		UserBiz userBiz = new UserBizImpl();
		Boolean isuser = userBiz.validate(username);
		
		if (isuser) {
			User user=new User();
			user.setUsername(username);
			user.setPassword(password);
			user.setTelephone(telephone);
			user.setName(name);
			user.setIsadmin("0");
			boolean isok=userBiz.register(user);
			if(isok){
				result="login.jsp";
			}else{
				request.setAttribute("message", "注册失败!");
				result="page/register.jsp";
			}
		} else {
			request.setAttribute("message", "该帐号已存在,注册失败!");
			result="page/register.jsp";
		}
		return result;
	}

}

3.建立工厂类,通过接口调用servlet

package cn.it.houserent.action;

public class ActionFactory {
	public static Action createAction(String actionName){
		Action action = null;
		if(actionName.equalsIgnoreCase("login")){
			action = new LoginServlet();
		}else if (actionName.equalsIgnoreCase("register")){
			action = new RegistServlet();
		}
		
		return action;
		
	}
}

4.通过servletAction来调度所有servlet

package cn.it.houserent.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

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

import cn.it.houserent.bean.House;
import cn.it.houserent.bean.User;
import cn.it.houserent.biz.UserBiz;
import cn.it.houserent.biz.impl.HouseBizImpl;
import cn.it.houserent.biz.impl.UserBizImpl;

public class ActionServlet extends HttpServlet {

	public ActionServlet() {
		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");
		String uri = request.getRequestURI();
		String contextPath = request.getContextPath();
		String actionPath = uri.substring(contextPath.length());
		String actionName = actionPath
				.substring(1, actionPath.lastIndexOf('.')).trim();
		Action action=ActionFactory.createAction(actionName);
		String result=action.execute(request, response);
		request.getRequestDispatcher(result).forward(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request,response);
		
	}
	public void init() throws ServletException {
		// Put your code here
	}

}


你可能感兴趣的:(servlet)