模拟springMVC实现自己的mvc架构

思路

写一个baseServlet继承httpServlet,重写service方法,在service方法中对前端的url
进行解析,通过反射调用方法,执行module层的逻辑。

我们只需要写servlet去继承baseServlet,不需要继承httpServlet,重写里面的方法。

流程如下:

  • 前端发送一个请求http://localhost:8080/user?method=login
  • 经过service方法将请求进行解析,获得method = login,通过反射调用login方法,实现请求的分发
  • 在login中实现登录的业务逻辑
  • login方法结束后,解析返回的字符串,判断走重定向还是请求转发

代码

baseServlet类:
中央控制器,负责调度分配请求

public class BaseServlet extends HttpServlet{
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			//获取需要被执行前的方法
			String methodName=request.getParameter("method");
			//获取当前对象对应的class对象
			Class clz=this.getClass();
			Method method= clz.getMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
			
			Object obj= method.invoke(this,request,response);
			if(Objects.nonNull(obj)){
				String page=obj.toString();
				if(page.startsWith("redirect:")){
					page=page.substring("redirect:".length());
					
					response.sendRedirect(page.trim());
				}else{
					request.getRequestDispatcher(page.trim()).forward(request, response);
				}
			}
		} catch (NoSuchMethodException | SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}

UserServlet类:
继承baseServlet,写业务逻辑

@WebServlet("/user.do")
@MultipartConfig
public class UserServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;

    public String login(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
    	System.out.println("123");
    	String uname=request.getParameter("username");
    	String password=request.getParameter("password");
    	
    	User user=new User();
    	user.setUname(uname);
    	user.setPassword(password);
    	Model model=ServiceFactory.newInstance().getUserService().login(user);
    	String r= request.getParameter("remerber");
    	if(model.isSuccess()){
    		request.getSession().setAttribute("user",model.getData());
    		if(Objects.equals("1",r)){
    			user=(User)model.getData();
    			user.setPassword(password);
    			String json=JSON.toJSONString(user);
    			json=URLEncoder.encode(json,"utf-8");
    			Cookie cookie=new Cookie("user",json);
    			cookie.setMaxAge(60*60*24*30);
    			response.addCookie(cookie);
    		}
    		return "redirect:topic.do?method=mytopic";
    	}else{
    		request.setAttribute("msg",model.getMsg());
    		return "login.jsp";
    	}
    }
    
    public String logout(HttpServletRequest request,HttpServletResponse response){
    	HttpSession s=request.getSession(false);
    	s.invalidate();
    	return "login.jsp";
    }
}

你可能感兴趣的:(spring全家桶,java高级,mvc,架构,servlet)