写javaee项目中关于Baseservlet的一个问题 50C

最近在写一个javaee项目,用到了多个servlet,发现这样很麻烦,然后模仿网上的代码,写了一个通用的servlet类--Baseservlet类并打包成jar包然后导入到类中,但是在运行时发生了错误,代码如下:

Baseservlet类:




public class BaseServlet extends HttpServlet {
    public void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");

        String methodname=request.getParameter("method");
        Method method=null;

        try {
            method = this.getClass().getMethod(methodname,
                    HttpServletRequest.class, HttpServletResponse.class);
        } catch (Exception e) {
            throw new RuntimeException("您要调用的方法:" + methodname + "它不存在!", e);
        }
        try {
            String result=(String) method.invoke(this,request,response);
            if(result != null && !result.trim().isEmpty()) {
                int index = result.indexOf(":");//获取第一个冒号的位置
                if(index == -1) {//如果没有冒号,使用转发
                    request.getRequestDispatcher(result).forward(request, response);
                } else {//如果存在冒号
                    String start = result.substring(0, index);//分割出前缀
                    String path = result.substring(index + 1);//分割出路径
                    if(start.equals("f")) {//前缀为f表示转发
                        request.getRequestDispatcher(path).forward(request, response);
                    } else if(start.equals("r")) {//前缀为r表示重定向
                        response.sendRedirect(request.getContextPath() + path);
                    }
                }
            }

    } catch (Exception e) {
        throw new RuntimeException(e);
}
}
}









customerservlet类:







 public class customerServlet extends BaseServlet {
    customerService cs=new customerService();
   public String add(HttpServletRequest request,HttpServletResponse response) throws InstantiationException, IllegalAccessException, InvocationTargetException{
       customer cus=Commenutils.toBean(request.getParameterMap(),customer.class );
       cs.add(cus);
       request.setAttribute("msg","添加成功");

       return "/msg.jsp";
   }
}

xml文件:



 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>webstudent</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
 <servlet>
    <servlet-name>customerServlet</servlet-name>
    <servlet-class>com.customer.servlet.customerServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>customerServlet</servlet-name>
    <url-pattern>/customerServlet</url-pattern>
  </servlet-mapping>

异常情况:

 严重: Servlet.service() for servlet [customerServlet] in context with path [/webstudent] threw exception
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at Servlet.BaseServlet.service(BaseServlet.java:45)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:474)

看异常好像是路径有问题,但不知道怎么改,希望大家能指导一下,谢谢

4个回答

Servlet.BaseServlet.service(BaseServlet.java:45)

45行是 throw new RuntimeException(e);

baseservlet没问题,估计是你add方法中封装请求数据出了问题。

试过在add方法中直接return字符串,还是报java.lang.reflect.InvocationTargetException的错

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!