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

写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)

    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况