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 {
       //功能实现代码
    }

完成!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值