Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validation and page back

Status
Not open for further replies.

calahans

Programmer
Jun 14, 1999
348
IE
Hi,

I have a jsp with a form. When the users has filled in his detail I want the jsp to forward to a servlet. I want the servlet to validate the data and to return back to the jsp if there is info missing BUT I WANT ALL THE VALUES STILL IN THE BOXES i.e. I dont the user to have to fill everything in.

Also I want to update the original page with details of the that boxes that need to be filled in. Something like:

** Name field is empty
** Bank code needs to be entered

How do I do this?

Many thanks

Cal Cal
 
This is how I solved the problem. I was new to JSP so there may be a better way.

First create an object to hold the Strings entered by the user (I called mine ContentBean) and store this object in the Session in your servlet:

ContentBean contentBean = (ContentBean) session.getValue("contentBean");
if (contentBean == null)
{
contentBean = new ContentBean();
session.putValue("contentBean", contentBean);
}

Create setters and getters for your text boxes. Set the user input in the contentBean:

contentBean.setText1( testString );

In your JSP, get the contentBean from the session and use a getter to set the initial value in the text box:

<jsp:useBean id=&quot;contentBean&quot; scope=&quot;page&quot; class=&quot;ContentBean&quot;/>
<%
try
{
contentBean = (ContentBean) session.getValue(&quot;contentBean&quot;);
if (contentBean == null)
{
contentBean = new ContentBean();
}
}
catch (Exception e)
{
// handle
}
%>

<input type=&quot;text&quot; name=&quot;textbox&quot; value= &quot;<%=contentBean.getText1()%>&quot; >

If, in your contentBean constructor, you set the initial value of the text to an empty string, then the first time your jsp is displayed the text field will be blank. Thereafter it will display whatever you load into it in the servlet.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top