aspose file tools
The moose likes JSP and the fly likes Infinate loop between JSP and Servlet - getParameter scope appears to be indefinate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Infinate loop between JSP and Servlet - getParameter scope appears to be indefinate" Watch "Infinate loop between JSP and Servlet - getParameter scope appears to be indefinate" New topic

Infinate loop between JSP and Servlet - getParameter scope appears to be indefinate

Mark Brownengland

Joined: Apr 30, 2006
Posts: 26
In closly following the strategy recommended at the following website for implementing validation: , I am running into problems when a servlet redirects the request/response back to a JSP.

The exact details are basically, I have a JSP page containing 3 fields which when the user clicks on the submit button, the form forwards to itself (the same JSP page):

<form action="<%= request.getRequestURI() %>" method="post">
<tr><td><b>Username: </b></td><td><input type="text" name="username" value="<%= form.getUsername() %>"/> <font color=red><%= form.getErrorMessage("username") %></font></td></tr>
<tr><td><b>Password: </b></td><td><input type="password" name="password" value="<%= form.getPassword() %>"/> <font color=red><%= form.getErrorMessage("password") %></font></td></tr>
<tr><td><b>Age: </b></td><td><input type="text" name="age" value="<%= form.getAge() %>"/></td></tr>
<tr><td><input type="submit" name="action" value="register"/></td></tr>
<input type="hidden" name="process" value="true"/>

When the JSP is re-loaded, a backing bean ("RegisterForm") is populated with the 3 fields from the form submission (discounting the hidden field):

<jsp:useBean id="form" class="" scope="request">
<jsp:setProperty name="form" property="errorMessages" value='<%= errorMap %>'/>

A scriplet then picks up the hidden field ("process") and calls the process() method on the form to check whether all the fields pass the validation constraints placed in the RegisterForm bean. If it does, the requestDispatcher forwards to a servlet (RegisterServlet) and the flow of control breaks out of the JSP (return . If validation fails, as shown in the above code for the form, the validation errors a printed alongside the text boxes. The contents of the scriplet is as follows:

<% if (request.getParameter("process") != null && request.getParameter("process").equals("true")) {%>
<jsp:setProperty name="form" property="*" />
<% // Attempt to process the form
if (form.process()) {
// Send off to login servlet
request.setAttribute("registerform", form);
System.out.println("username: " + form.getUsername() + ", password: " + form.getPassword() + ", age: " + form.getAge());

When the control passes to the RegisterServlet, a check is carried out to verfiy that the username does not already exist in the database. If it does, control passes back to the original jsp:

// code in RegisterServlet

System.out.println("going back to the register.jsp...");

At this point, what is supposed to happen is that the jsp page reports to the user that the username already exists and prompt for registration details once more. Instead, when control passes back to the jsp, the getParameter("process") conditional statement (seen in the above scriplet) evaluates to true and so immediately the code inside the statement executes and immediately forwards back to the servlet, causing an infinate loop to occur between the RegisterServlet and register.jsp!

Why is that when you first create a hidden field in a form, set the action of the form to submit to itself, check for the field upon re-loading (getParameter()) and then forward to a servlet which forwards back to the JSP that the original parameters of the form still exist? I would have thought when the form was first submitted, the "process" parameter would be available to the JSP, but when the JSP then forwarded to the servlet, the parameter(s) would no longer exist (out of request scope). By the time the servlet forwards back to the JSP and the scriplet checks for the "Process" parameter, I would have thought it would definately no longer exist??

I would appreciate if someone can shed some light as to why the parameters (and their values) in the form seem to be lingering around for longer than one single request?
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 62317

A forward does not initiate a new request -- it 'forwards' the current request to the next resource. Request parameters et al will thusly be preserved.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: Infinate loop between JSP and Servlet - getParameter scope appears to be indefinate