servlet运行“/*”引起的java.lang.*Error

     <servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.jd.login.UI.LoginUIServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

若将servlet的url-pattern配置为“/*”时会拦截内部所有请求,使所有请求都由com.jd.login.UI.LoginUIServlet来处理,所以当项目跑起来后会出现栈溢出错误:java.lang.*Error

 @WebServlet(name = "LoginUIServlet",value="/servlet/LoginUIServlet")
public class LoginUIServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//出现死循环,因为所有请求都被拦截
request.getRequestDispatcher("/WEB-INF/pages/login.jsp").forward(request,response);
} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}

栈溢出异常如下:

servlet运行“/*”引起的java.lang.*Error

解决:

将web.xml中的servlet的url-pattern配置为具体的路径

     <servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.jd.login.UI.LoginUIServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/servlet/LoginUIServlet</url-pattern>
</servlet-mapping>

检验:

在浏览器中输入localhost/servlet/LoginUIServlet,结果如下

servlet运行“/*”引起的java.lang.*Error

备注:在IDEA中跑起来的web项目,在浏览器中访问不要添加应用名称。

上一篇:vue 编译原理 简介


下一篇:ubuntu 16.04 安装 Matlab R2016b后启动出现的问题