Servlets – infinite loop when forwarding requests in Java servlets
I hope you can help me solve the problem I face:
I created a simple web application using NetBeans Up to now, this is very basic
>The servlet receives requests in the / verificon / * URL mode. > It extracts any string set after / verificon /, that is, if the web address is http: / / domain / context / verificon / blahblah, it extracts blahblah. > It checks whether such a string is a known string and displays only the JSP with the result (true / false)
However, despite its simplicity, the following error occurred when running the application using the test string:
javax.servlet.ServletException: The server side component of the HTTP Monitor has detected a java.lang.StackOverflowError. This happens when there is an infinite loop in the web module. Correct the cause of the infinite loop before running the web module again. org.netbeans.modules.web.monitor.server.MonitorFilter.rethrow(MonitorFilter.java:1648) org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:473) mx.tegu.kdor.web.iu.ServletVerificon.processRequest(ServletVerificon.java:51) mx.tegu.kdor.web.iu.ServletVerificon.doGet(ServletVerificon.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:621) javax.servlet.http.HttpServlet.service(HttpServlet.java:722) org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393) mx.tegu.kdor.web.iu.ServletVerificon.processRequest(ServletVerificon.java:51) mx.tegu.kdor.web.iu.ServletVerificon.doGet(ServletVerificon.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:621) javax.servlet.http.HttpServlet.service(HttpServlet.java:722) org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393) mx.tegu.kdor.web.iu.ServletVerificon.processRequest(ServletVerificon.java:51) mx.tegu.kdor.web.iu.ServletVerificon.doGet(ServletVerificon.java:70) ...
Then it repeats itself
The processrequest method of my servlet is as follows Testdata is just an auxiliary class. If the string is known, it returns a mapeo object. If it is not known, it returns null
protected void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { request.setCharacterEncoding("UTF-8"); String h = request.getRequestURI().replaceFirst(request.getContextPath() + "/verificon/",""); TestData td = TestData.getInstance(); Mapeo m = td.getMapeo(h); boolean valido = false; if(m != null) { valido = true; } request.setAttribute("valido",valido); /* PrintWriter out = response.getWriter(); out.write("Válido?: " + valido); out.close(); */ String respuesta = "WEB-INF/jsp/resultado.jsp"; // Como regla general,forward se utiliza para los GET y sendRedirect para los POST RequestDispatcher rd = request.getRequestDispatcher(respuesta); rd.forward(request,response);
}
Any help would be appreciated
If you need any other information, please let me know
thank you!
Note 1: Line 51 of the servlet calls rd.forward() at the end of the processrequest method, and line 70 just calls processrequest() from the doget method Note 2: if I comment on the forward section and uncomment the printwriter section, everything works as expected Note 3: Resultado JSP is a pure HTML page with correct DOCTYPE, def, HTML, head and body tags. This: <% Boolean Valido = (Boolean) request getAttribute(“valido”);%> … <% If (Valido) {% > < p is h. Valido < / P is h. <%} else {% > < p is h. inv á Lido < / P is h. <%}% >
Solution
Look here,
String respuesta = "WEB-INF/jsp/resultado.jsp"; RequestDispatcher rd = request.getRequestDispatcher(respuesta); // ...
You use relative path forwarding It is forwarded to http://domain/context/verificon/blahblah/web-inf/jsp/resultado.com JSP, which again matches the servlet Then use the relative path to forward to http://domain/context/verificon/blahblah/web-inf/jsp/resultado.com jsp/WEB-INF/jsp/resultado. JSP, which again matches the servlet wait. If you have debugged / logged the incoming request URI, it will make everything clearer
You need to use absolute path forwarding Use / prefix
String respuesta = "/WEB-INF/jsp/resultado.jsp"; // ...
It has nothing to do with the specific problem. The way you check values in JSP is very clumsy and old-fashioned Just use el (it's been around for more than a decade to make sure you're reading the right JSP / servlet books / tutorials):
<p>${valido ? 'Válido' : 'Inválido'}</p>