Servlet抽取

问题描述

因为在之前的练习中每写一个功能都需要一个Servlet,那Servlet的数量就会相对较多,现在优化为一个模块一个Servlet,相当于数据库中一张表对应一个Servlet,在Servlet中提供不同的方法,完成用户的请求

实现

  1. 定义一个BaseServlet类,继承HttpServlet,复写service方法,这样请求进入到service方法中。
  2. 定义servlet,继承BaseServlet,设置访问路径为多一级目录形式(例如:"/user/*"),定义方法,编写功能实现
  3. 在BaseServlet的service方法中利用反射调用servlet的方法
    还不懂,上图

在这里插入图片描述

代码实现

BaseServlet

public class BaseServlet extends HttpServlet {
    
    
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
    	//获取请求的url路径
        StringBuffer url = req.getRequestURL();
        //获取方法名
        String methodName = url.substring(url.lastIndexOf("/") + 1);
        //这里判断请求的是否为html页面
        if(methodName.endsWith(".html")){
    
    
        	//获取页面父路径
            String url_html = url.substring(0,url.lastIndexOf("/",url.lastIndexOf("/")-1)+1);
            //重定向到指定页面
            resp.sendRedirect(url_html+methodName);
        }
        try {
    
    
        	//通过反射获取方法对象
            Method method = this.getClass().getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
           	//执行方法,this代表当前路径的Servlet类
            method.invoke(this,req,resp);
        } catch (NoSuchMethodException e) {
    
    
            e.printStackTrace();
        } catch (IllegalAccessException e) {
    
    
            e.printStackTrace();
        } catch (InvocationTargetException e) {
    
    
            e.printStackTrace();
        }

    }
}

Servlet

@WebServlet("/user/*")
public class UserServlet extends BaseServlet {
    
    
    private UserService userService = new UserServiceImpl();
    /**
     * 用户注册
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        HttpSession session = req.getSession();
        String checkcode = (String)session.getAttribute("CHECKCODE_SERVER");
        String check = req.getParameter("check");
        ResultInfo info = new ResultInfo();
        boolean flag = false;
        if(check.equalsIgnoreCase(checkcode)){
    
    
            session.removeAttribute("CHECKCODE_SERVER");
            Map<String, String[]> map = req.getParameterMap();
            User user = new User();
            try {
    
    
                BeanUtils.populate(user,map);
            } catch (IllegalAccessException e) {
    
    
                e.printStackTrace();
            } catch (InvocationTargetException e) {
    
    
                e.printStackTrace();
            }
            flag = userService.register(user);
            info.setFlag(flag);
            if(!flag){
    
    
                info.setErrorMsg("注册失败");
            }
        }else{
    
    
            info.setFlag(flag);
            info.setErrorMsg("验证码错误");
        }
        ObjectMapper jackson = new ObjectMapper();
        String infostr = jackson.writeValueAsString(info);
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(infostr);
        //jackson.writeValue(resp.getWriter(),info);
    }

    /**
     * 账号激活
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void active(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        //功能实现代码
    }

    /**
     * 用户登录
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
       //功能实现代码
    }

完成!

猜你喜欢

转载自blog.csdn.net/weixin_41058733/article/details/108142916