Tomcat在requestDispatcher#forward()上无限循环. StackOverflowError [英] Tomcat infinte loop on requestDispatcher#forward(). StackOverflowError
问题描述
Tomcat不断重复这些行之后就给了我StackOverflowError,其中DiceBoardDispatcher
是我的HttpServlet,在第34行中,我正在调用requestDispatcher#forward()
.
Tomcat is giving me StackOverflowError after constantly repeating these lines, where DiceBoardDispatcher
is my HttpServlet and in line 34 I am calling 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的相关部分:
I guess it's got something to do with my deployment, here is the relevant part of my 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
结尾,而该Servlet
分派给JSP
.
What I am trying to do is have 2 actions both landing on the same page. I thought I'd set it up like this. Each action goes through its own Filter
and they both end in the Servlet
that dispatches to a JSP
.
我的url-pattern映射曾经是/diceBoard/*
,它是Web应用程序中的某个文件夹.从那里更改为/dices/*
可以解决转到url/dices
时的问题.现在,我可以正常访问该页面了,但是当进入url/dices/roll
时,我会遇到无限循环(无论是POST还是GET).
My url-patter mapping used to be to /diceBoard/*
, which is a folder somewhere in the web-app. Changing from there to /dices/*
fixed the problem when going to url/dices
. Now I can reach that page normally, but when going to url/dices/roll
I get the infinite loop (no matter if POST or GET).
我的servlet代码:
My servlet code:
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:
And 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() { }
}
推荐答案
这是您的错误所在的行:
This is the line where your error is located:
RequestDispatcher view = request.getRequestDispatcher("diceBoardPage.jsp");
根据 getRequestDispatcher API ,输入uri是相对于当前servlet上下文的 ,因此,当您在
According to the getRequestDispatcher API, the input uri is relative to the current servlet context, so when you are executing your servlet at
/dices/roll
...并且它执行对"diceBoardPage.jsp"的分派,实际上是在分派给
... and it performs a dispatch to "diceBoardPage.jsp", it is actually dispatching to
/dices/diceBoardPage.jsp
该模式映射到哪个servlet?根据您的部署描述符,每个以"/dices/*"开头的URL都映射到DiceBoard
. IE.相同的servlet.这是导致您的StackOverflowError的无限循环.
And what servlet is mapped this pattern to? According to your deployment descriptor, every URL beginning with "/dices/*" is mapped to DiceBoard
. I.E. the same servlet. Here comes the infinite loop that caused your StackOverflowError.
如果JSP必须保留在dices
uri内,则必须限制映射DiceBoard
servlet的URL模式.如果需要,请不要犹豫添加几个值:
If the JSP must stay within the dices
uri, you must restrict the URL pattern which maps the DiceBoard
servlet. Do not hesitate to add several values if needed:
<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>
这篇关于Tomcat在requestDispatcher#forward()上无限循环. StackOverflowError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!