ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

javaWeb_BaseServlet --对于servlet的改写, 使得servlet更加优雅,类似于springmvc等

2019-12-01 13:02:04  阅读:304  来源: 互联网

标签:HttpServletRequest BaseServlet String HttpServletResponse str import servlet jav


目的 : 实现BaseServlet, 实现对于servlet的优雅编写, 减少重复代码的出现

需要解决的问题 : 

设计思路 : 继承HttpServlet, 重写service方法. 原本操作是:重写doGet与重写doPost(这两个函数原本由service调用)

实际代码 : 

  1. BaseServlet的实现 : 

 1 package com.theangryz.servlet;
 2 
 3 import javax.servlet.ServletException;
 4 import javax.servlet.http.HttpServlet;
 5 import javax.servlet.http.HttpServletRequest;
 6 import javax.servlet.http.HttpServletResponse;
 7 import java.io.IOException;
 8 import java.lang.reflect.Method;
 9 
10 public class BaseServlet extends HttpServlet {
11     @Override
12     protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
13         Class clazz = this.getClass();
14         String methodName = req.getParameter("method");
15         if (methodName == null || methodName.trim().isEmpty()){
16             throw new RuntimeException("没有输入method参数");
17         }
18         Method method = null;
19         try{
20             method = clazz.getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
21         }catch (Exception e){
22             throw new RuntimeException("你调用的方法" + methodName + "不存在");
23         }
24         String str = null;
25         try{
26             str = (String) method.invoke(this, req, resp);
27         }catch(Exception e){
28             throw new RuntimeException("方法执行出错");
29         }
30         if (str == null || str.trim().isEmpty()){
31             return ;
32         }
33         if (str.contains(":")){
34             int index = str.indexOf(":");
35             String prefix = str.substring(0, index);
36             String suffix = str.substring(index+1);
37             if (prefix.equalsIgnoreCase("f")){     //转发
38                 req.getRequestDispatcher(suffix).forward(req, resp);
39             }else if (prefix.equalsIgnoreCase("r")){   //重定向
40                 resp.sendRedirect(req.getContextPath() + suffix);
41             }else{
42                 throw new RuntimeException("当前版本不支持该前缀 : " + prefix);
43             }
44         }else{  //默认转发
45             req.getRequestDispatcher(str).forward(req, resp);
46         }
47     }
48 }

  2. AServlet的实现(该AServlet用于测试BaseServlet)

 1 package com.theangryz.servlet;
 2 
 3 import javax.servlet.ServletException;
 4 import javax.servlet.http.HttpServletRequest;
 5 import javax.servlet.http.HttpServletResponse;
 6 import java.io.IOException;
 7 
 8 public class AServlet extends BaseServlet{
 9     protected String addUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
10 
11         response.getWriter().print("addUser执行结束了");
12         return "/show.jsp";
13     }
14     protected String func1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
15         System.out.println("func1");
16         return "f:/show.jsp";
17     }
18     protected String func2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
19         System.out.println("func1");
20         return "r:/show.jsp";
21     }
22     protected String func3(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
23         System.out.println("func1");
24         return null;
25     }
26 }

 

实际编写中遇到的问题 : 

  1. 程序在执行 clazz.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class)一直抛出异常(methodName是正确的前提下).

    解决 : getMethod方法反射只能获取public的方法与继承得来的public方法. 但是在AServlet中方法的访问修饰为protected, 所以即使methodName是正确的, getMethod还是没有办法反射得到protected的方法. 所以程序使用了getDeclaredMethod()方法, 该方法可以得到除了继承得来的方法之外的所有方法.

 

标签:HttpServletRequest,BaseServlet,String,HttpServletResponse,str,import,servlet,jav
来源: https://www.cnblogs.com/gogotao/p/11965679.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有