qq_36960424 2017-04-27 15:15 采纳率: 0%
浏览 1786
已结题

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

最近在写一个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条回答 默认 最新

  • JE_GE 2017-04-27 15:25
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决