Endless loop while forwarding a request in Java Servlet

Hope you can help me with this problem that I am facing:

I created a simple web application using NetBeans. At the moment it is very simple.

  • The servlet receives requests using the url /verificon/* pattern.
  • It extracts any line after /verificon/ , i.e. if url was http://domain/context/verificon/blahblah , it will extract blahblah .
  • It checks if such a string is a known string and simply displays jsp with the result (true / false).

However, be that as it may, I get the following error when starting the application with a test line:

 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 just keeps repeating itself.

My servlet handling method is as follows. TestData is nothing more than a helper class that returns a Mapeo object if the string is known, or null if it is not.

 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 really appreciated.

If you need more information, please let me know.

Thanks!

Note 1: Line 51 of the servlet is a call to the rd.forward () method at the end of the processRequest method, and line 70 is just a call to the processRequest () method from the doGet method. Note 2: Everything works as expected if I comment the section forward and uncomment the PrintWriter section. Note 3: resultado.jsp is a simple HTML page with the corresponding doctype def, html, head and body tags, and this is: <%boolean valido = (boolean)request.getAttribute("valido");%> ... <% if(valido) {%> <p>Válido</p> <% } else {%> <p>Inválido</p> <% }%>

+6
source share
1 answer

Look at here,

 String respuesta = "WEB-INF/jsp/resultado.jsp"; RequestDispatcher rd = request.getRequestDispatcher(respuesta); // ... 

You forward using the relative path. It forwards http://domain/context/verificon/blahblah/WEB-INF/jsp/resultado.jsp , which again corresponds to servlets. This, in turn, is forwarding using the relative path to http://domain/context/verificon/blahblah/WEB-INF/jsp/resultado.jsp/WEB-INF/jsp/resultado.jsp , which again corresponds to servlets. Etcetera. It would be much better if you debug / log the URI of the incoming request.

You need to forward using the absolute path. Prefix: / .

 String respuesta = "/WEB-INF/jsp/resultado.jsp"; // ... 

Unrelated to the specific issue, the way you test the value of the JSP is very awkward and old school. Just use EL (which has been around for more than a decade, make sure you read the right JSP / Servlet books / tutorials):

 <p>${valido ? 'Válido' : 'Inválido'}</p> 
+10
source

Source: https://habr.com/ru/post/896366/


All Articles