小编典典

Tomcat在requestDispatcher#forward()上无限循环。StackOverflowError

tomcat

在不断重复这些行之后,Tomcat给了我StackOverflowError,这DiceBoardDispatcher是我的HttpServlet
所在的地方,在第34行中,我正在调用requestDispatcher#forward()

nl.rickhurkens.rollDice.web.diceBoard.DiceBoardDispatcher.doGet(DiceBoardDispatcher.java:34)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
        org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
        nl.rickhurkens.rollDice.web.diceBoard.DiceBoardDispatcher.doGet(DiceBoardDispatcher.java:34)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
        org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

我想这与我的部署有关,这是我的web.xml的相关部分:

  <servlet-mapping>
    <servlet-name>DiceBoard</servlet-name>
    <url-pattern>/dices/*</url-pattern>
  </servlet-mapping>

  <filter-mapping>
    <filter-name>RollDiceFilter</filter-name>
    <url-pattern>/dices/roll</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>SetSettingsFilter</filter-name>
    <url-pattern>/dices/setup</url-pattern>
  </filter-mapping>

我想做的是在同一页面上同时执行2个操作。我以为我会这样设置。每个动作都要经过自己的动作,Filter并且都以Servlet分派到的结尾JSP

我的网址格式映射曾经是to
/diceBoard/*,它是网络应用中某个文件夹。从那里改变到/dices/*解决问题url/dices。现在,我可以正常访问该页面了,但是当url/dices/roll我进入无限循环时(无论是POST还是GET)。

编辑:我的servlet代码:

public class DiceBoardDispatcher extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        RequestDispatcher view = request.getRequestDispatcher("diceBoardPage.jsp");
        view.forward(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        RequestDispatcher view = request.getRequestDispatcher("diceBoardPage.jsp");
        view.forward(request, response);
    }
}

和RollDiceFilter:

public class RollDiceFilter implements Filter {
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest)request;
        HttpServletResponse res = (HttpServletResponse)response;
        HttpSession session = req.getSession();

        DiceBoard diceBoard = (DiceBoard)session.getAttribute("diceBoard");
        String[] lockedValues = req.getParameterValues("lock");

        diceBoard.unlockAllDice();
        if (lockedValues != null) {
            for (String value : lockedValues) {
                if (value != null) {
                    try {
                        diceBoard.lockDice(Integer.parseInt(value));
                    } catch (DiceNotInCollectionException e) {
                        // TODO: redirect to error page.
                        e.printStackTrace();
                    }
                }
            }
        }
        diceBoard.getCup().roll();

        chain.doFilter(req, res);
    }

    public void init(FilterConfig fConfig) throws ServletException { }
    public void destroy() { }
}

阅读 639

收藏
2020-06-16

共1个答案

小编典典

这是您的错误所在的行:

RequestDispatcher view = request.getRequestDispatcher("diceBoardPage.jsp");

根据getRequestDispatcher
API
,输入uri是
对于当前servlet上下文的,因此,当您在以下位置执行servlet时

/dices/roll

…并执行“ diceBoardPage.jsp”的调度,实际上是调度到

/dices/diceBoardPage.jsp

将该模式映射到哪个servlet?根据您的部署描述符,每个以“ / dices /
*”开头的URL都映射到DiceBoard。IE相同的servlet。这是导致您的StackOverflowError的无限循环。

如果JSP必须保留在dicesuri内,则必须限制映射DiceBoardservlet 的URL模式。如果需要,请不要犹豫添加几个值:

<servlet-mapping>
    <servlet-name>DiceBoard</servlet-name>
    <url-pattern>/dices/one</url-pattern>
    <url-pattern>/dices/two</url-pattern>
    <url-pattern>/dices/three</url-pattern>
</servlet-mapping>
2020-06-16