Infinite loop and stackoverflow error in Servlet Call




Asked on August 22, 2014

My Controller servlet is going through the infinite loop and sometimes it ends with the stackoverflow error..

<servlet>
<servlet-name>jsphandler</servlet-name>
<servlet-class>com.JspHandler</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>jsphandler</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>

public void doGet(HttpServletRequest req,HttpServletResponse res){

String url= req.getRequestURI().substring((req.getContextPath()).length()+1);

System.out.println("Requested Resource Is:"+url); 
res.sendRedirect(url); 
}
Output When i Requested for "index.jsp" is:

Requested Resource Is: index.jsp
Requested Resource Is: index.jsp
Requested Resource Is: index.jsp
Requested Resource Is: index.jsp
Requested Resource Is: index.jsp
Requested Resource Is: index.jsp
Requested Resource Is: index.jsp
Requested Resource Is: index.jsp ...............so on....




Replied on August 25, 2014
Check your Servlet mapping.

<servlet-mapping>
<servlet-name>jsphandler</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>

It says that for every *.jsp, Servlet  JspHandler is being called. So when you are writing the line

res.sendRedirect(url); i.e res.sendRedirect("index.jsp"); then again servlet is being called because of *.jsp  extension.

Solution

Change URL pattern like

<servlet-mapping>
<servlet-name>jsphandler</servlet-name>
<url-pattern>/JspHandler </url-pattern>
</servlet-mapping>




Write Answer










©2024 concretepage.com | Privacy Policy | Contact Us