2017-04-19 58 views
0

我正在嘗試創建簡單的Web應用程序。 這裏是我的web.xmlstackoverflowerror嘗試訪問Web應用程序時

<servlet> 
    <servlet-name>basicServlet</servlet-name> 
    <servlet-class>com.pack.BasicServlet </servlet-class> 
</servlet> 

<servlet-mapping> 
    <servlet-name>basicServlet</servlet-name> 
    <url-pattern>/*</url-pattern> 
</servlet-mapping> 

這是我index.jsp是位於webapp/WEB-INF/jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta charset="UTF-8"> 
     <title>Basic web page</title> 
    </head> 
    <body> 
     <h2>Data provided by server:</h2> 
     <p> 
      <c:choose> 
       <c:when test="${ not empty message }"> 
        <h3>${message}</h3> 
       </c:when> 
       <c:otherwise> 
        <h3>none</h3> 
       </c:otherwise> 
      </c:choose> 
     </p> 
    </body> 
</html> 

這是我servlet

public class BasicServlet extends HttpServlet { 

    private IBasicService basicService; 

    @Override 
    public void init() throws ServletException { 
     basicService = new BasicService(); 
    } 

    @Override 
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
     req.setAttribute("message", basicService.provideMessage()); 
     req.getRequestDispatcher("/jsp/index.jsp").forward(req, resp); // 23 line 
    } 
} 

我建了一個war並在Tomcat中部署它。但是,當我嘗試訪問它,我得到很長的堆棧跟蹤

javax.servlet.ServletException: Servlet execution threw an exception 
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
com.pack.BasicServlet.doGet(BasicServlet.java:23) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:635) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:742) 
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
com.pack.BasicServlet.doGet(BasicServlet.java:23) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:635) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:742) 
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
com.pack.BasicServlet.doGet(BasicServlet.java:23) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:635) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:742) 
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
com.pack.BasicServlet.doGet(BasicServlet.java:23) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:635) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:742) 
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
com.pack.BasicServlet.doGet(BasicServlet.java:23) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:635) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:742) 
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
... 

root cause

java.lang.StackOverflowError 
com.pack.BasicServlet.doGet(BasicServlet.java:22) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:635) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:742) 
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
.... 

問題是什麼?我很久以前使用了servlets,所以也許我忘了一些東西...

回答

3

您已經將您的servlet綁定到了URL模式/*,因此其上下文中的每個請求都將定向到該servlet。該servlet的doGet()方法嘗試將請求轉發到URL /jsp/index.jsp,但與servlet上下文中的每個其他URL一樣,該請求將被引導(返回)到servlet。這會創建一個無限遞歸,這在您的堆棧跟蹤中很明顯。

將您的servlet綁定到更具體的URL模式。或者,如果您想要預處理每個入站請求,請考慮通過Filter而不是servlet來實現該請求。

相關問題