尝试访问Web应用程序时出现stackoverflower错误

时间:2017-04-19 20:08:06

标签: java jsp tomcat servlets

我试图创建简单的Web应用程序。 这是我的web.xml

<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所以也许我忘记了什么......

1 个答案:

答案 0 :(得分:3)

您已将servlet绑定到URL模式/*,因此其上下文中的每个请求都将定向到servlet。 servlet的doGet()方法尝试将请求转发到URL /jsp/index.jsp,但是,与servlet的上下文中的每个其他URL一样,将被引导(返回)到servlet。这会产生无限递归,这实际上在堆栈跟踪中很明显。

将servlet绑定到更具体的URL模式。或者,如果要预处理每个入站请求,请考虑通过Filter而不是servlet来实现它。