• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In closly following the strategy recommended at the following website for implementing validation: http://javaalmanac.com/egs/javax.servlet.jsp/myform.jsp.html , 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">
<table>
<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/>
<tr><td><input type="submit" name="action" value="register"/></td></tr>
</table>
<input type="hidden" name="process" value="true"/>
</form>


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="com.games.hilo.forms.RegisterForm" scope="request">
<jsp:setProperty name="form" property="errorMessages" value='<%= errorMap %>'/>
</jsp:useBean>


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());
request.getRequestDispatcher("/register").forward(request,response);
return;
}
}%>


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

if(userExists)
{
System.out.println("going back to the register.jsp...");
req.getRequestDispatcher("/register.jsp").forward(req,resp);
}


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?
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic