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

写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 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决