I have a .jsp file that receives a request and checks the request’s parameters. Within the same directory as this JSP file, there is error.jsp file that is supposed to do error processing. If the parameters are null I want to forward the request to error.jsp file as in:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="ISO-8859-1"%>
<html>
<head></head>
<body></body>
<%
request.setCharacterEncoding("UTF-8");
String uid = request.getParameter("uid");
String procName = request.getParameter("procName");
//param check
if(uid == null || procName == null){ %>
<jsp:forward page="error.jsp"/>
<% }%>
</html>
However, this does not work. After the forward line I have the request re-submitted to the same JSP instead of error.jsp. Eventually, I get a StackOverflowException due to the cycle at the server.
If instead of JSP:forward, I use response.sendRedirect() as in:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="ISO-8859-1"%>
<html>
<head></head>
<body></body>
<%
request.setCharacterEncoding("UTF-8");
String uid = request.getParameter("uid");
String procName = request.getParameter("procName");
//param check
if(uid == null || procName == null){
response.sendRedirect("error.jsp");
}%>
</html>
everything works fine. Why does the jsp:forward fail to work? According to this site and the API I have the correct tags in place.
PS.: The JSPs are deployed in a JBoss AS with version 4.0.5 GA.
Read the stacktrace of the
StackOverflowErrorand pay attention to the repeating pattern. Check if there isn’t any customFilterbeen involved which is repeatedly intercepting on the forwarded request and fix theFilter‘s code or configuration accordingly so that it isn’t doing that anymore. For example, remove<dispatcher>FORWARD</dispatcher>from the configuration or set some request attribute which indicates that theFilterhas already done the job.Last but not least, you should actually not be using a JSP file as a front controller, but a servlet. For new insights you may find the examples in our servlets wiki page helpful. It also covers basic validation and error handling.