Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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

Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
Posts: 67425
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • 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.
Heroic work plunger man. Please allow me to introduce you to this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic