In my web.xml, I set up a servlet like
<servlet>
<servlet-name>forward</servlet-name>
<servlet-class>test.Forward</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>forward</servlet-name>
<url-pattern>/f/*</url-pattern>
</servlet-mapping>
test.Forward#doGet is implemented as
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.getRequestDispatcher("target.jsp").forward(req, resp);
}
When visiting localhost:8080/project/f/anypath, server crashes due to a stack overflow error.
It seems that RequestDispatcher#forward sets up another request to /f/target.jsp, and then test.Forward#doGet is called again, and then again. I somehow misunderstood RequestDispatcher#forward that I thought it as just render a page.
So, are there any methods that just render JSP?
Your servlet path is /f/*, and you’re redirecting to a file that is on that path, most likely unintentionally:
As a result, this creates an endless loop as your servlet keeps calling itself again and again and again.
If you want to redirect to the JSP, I’m assuming it’s located in another folder like a /jsp/target.jsp location? If so, then make sure you include the full path:
When you leave out the
/, the request is forwarded relative to the current requestURI.