doubt about servlets

C

chandu

hai
can i pass some values into a static html elemenets(like passing some
text into text field of a html file) from a servlet...a typical
situtaion is like this...
a user is creating new userid..when he submits the form server
checks for availability of userid in the database and gives dynamically
generated html file which looks same as the one user submitted...i dont
want servlet to create one html file by using printwriter..i want
server to return the same html file by keeping same valus user entered
before submission.how can i achieve this.
regards
chandu
 
A

Alex Hunsley

chandu said:
hai
can i pass some values into a static html elemenets(like passing some
text into text field of a html file) from a servlet...a typical
situtaion is like this...
a user is creating new userid..when he submits the form server
checks for availability of userid in the database and gives dynamically
generated html file which looks same as the one user submitted...i dont
want servlet to create one html file by using printwriter..i want
server to return the same html file by keeping same valus user entered
before submission.how can i achieve this.

A way you could do it is to have the servlet send back a redirect header
(or a brief HTML page containg a 'refresh' type redirect, if you want)
that sends the client browser back to the input form, but with the
username (or whatever else) provided as GET style parameters in the URL.
Then the actual input form page can be using Javascript which reads the
GET parameters in the URL and uses them to populate the form.
This way you can avoid having to return the HTML content from the
servlet itself.

More details here:
http://javascript.about.com/library/blqs.htm

To give a brief illustration of what I mean, suppose your create user
page is at this URL:

http://www.someSiteImade.com/createUser.html

.... this page contains an HTML form which POSTs or GETs the new user
info to the serlvet at:

http://www.someSiteImade.com/checkUser

This serlvet then in your situation presumably rejects the username as
already taken (or similar) and wants to return them to the create user
page. So it sends a redirect header which makes the client browser go to
this exact URL:

http://www.someSiteImade.com/createUser.html?username=Freddy

(where 'Freddy' is the username that already exists!)
Now, the createUser.html page has some clever javascript in it which
checks for the username parameter being provided as part of the URL. If
it was provided, the Javascript populates whatever field in the form
with 'Freddy' (or whatever was sent). Hey presto digitato.


Or, another way to do it - but the way you seem to want to avoid - is to
have the create user page at the server side, as a servlet that spews
out a template. If the servlet deems appropriate, it could insert the
username/whatever into the template it was returning.
 
J

Juha Laiho

Alex Hunsley said:
....
Or, another way to do it - but the way you seem to want to avoid - is to
have the create user page at the server side, as a servlet that spews
out a template. If the servlet deems appropriate, it could insert the
username/whatever into the template it was returning.

.... and continue from this with a bit better modeling:
- the form is a JSP
- submit action is a servlet

The JSP, when rendered, will take field default values from a set of
session attributes. The servlet, when called via the form, will set
these session attributes whenever the form wasn't properly completed
(f.ex. username conflict, or whatever), and will redirect back to the
JSP.

.... and to avoid hand-coding much of this, see the form validation
functionality in Struts framework (but then, a framework is yet
another thing to learn, and depending on your needs might be overkill).
On the other hand, Struts form validation has things like per-field error
messages to the user, and so on.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,164
Latest member
quinoxflush
Top