Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

doubt about servlets

Thread view: 
chandu - 26 Mar 2006 04:10 GMT
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
Alex Hunsley - 26 Mar 2006 04:41 GMT
> hai
>   can i pass some values into a static html elemenets(like passing some
[quoted text clipped - 6 lines]
> 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.
Juha Laiho - 26 Mar 2006 07:57 GMT
Alex Hunsley <lard@tardis.ed.ac.molar.uk> said:
>> hai
>>   can i pass some values into a static html elemenets(like passing some
[quoted text clipped - 6 lines]
>> server to return the same html file  by keeping same valus user entered
>> before submission.how can i achieve this.
...
>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.
Signature

Wolf  a.k.a.  Juha Laiho     Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
        PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.