1 <servlet> 2 <servlet-name>login</servlet-name> 3 <servlet-class>com.jd.login.UI.LoginUIServlet</servlet-class> 4 </servlet> 5 <servlet-mapping> 6 <servlet-name>login</servlet-name> 7 <url-pattern>/*</url-pattern> 8 </servlet-mapping>
若将servlet的url-pattern配置为“/*”时会拦截内部所有请求,使所有请求都由com.jd.login.UI.LoginUIServlet来处理,所以当项目跑起来后会出现栈溢出错误:java.lang.StackOverflowError
1 @WebServlet(name = "LoginUIServlet",value="/servlet/LoginUIServlet") 2 public class LoginUIServlet extends HttpServlet { 3 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 4 //出现死循环,因为所有请求都被拦截 5 request.getRequestDispatcher("/WEB-INF/pages/login.jsp").forward(request,response); 6 } 7 8 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 9 this.doPost(request,response); 10 } 11 }
栈溢出异常如下:
解决:
将web.xml中的servlet的url-pattern配置为具体的路径
1 <servlet> 2 <servlet-name>login</servlet-name> 3 <servlet-class>com.jd.login.UI.LoginUIServlet</servlet-class> 4 </servlet> 5 <servlet-mapping> 6 <servlet-name>login</servlet-name> 7 <url-pattern>/servlet/LoginUIServlet</url-pattern> 8 </servlet-mapping>
检验:
在浏览器中输入localhost/servlet/LoginUIServlet,结果如下
备注:在IDEA中跑起来的web项目,在浏览器中访问不要添加应用名称。