aspose file tools*
The moose likes Servlets and the fly likes Passing value from one servlet to another Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Passing value from one servlet to another" Watch "Passing value from one servlet to another" New topic
Author

Passing value from one servlet to another

Swetha Mohan
Greenhorn

Joined: Dec 23, 2005
Posts: 15
Hello friends
I have two servlets
[login servlet]
I am new to servlet programming.I am working on a simple login page.As soon as the user name and password are entered the login servlet goes and checks in the database and redirects to the next page once the username and password is correct.
[entry servlet]
In the next page the user is asked to enter his/her name and address.on clicking the save button on this page the values name and address along with the login name (the name which he used to login) should be added on to the database.
Now please tell me how to retrive the login name from the first page to this second page.
how can we pass the value form [login servlet] to [entry servlet]?
Please help

Thanks in advance
Swetha
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
There are many ways.

- URL re-writing
- store login name to your request object and get that in your second page in a hidden field
- you can go for use session (not the best choice in your case)
Himanshu Bisht
Ranch Hand

Joined: Aug 27, 2004
Posts: 96
Hi,
Well you have many ways to do this.

a) You can get the value in second servlet by requesting the parameter.

String username=request.getParameter("name");

b) You can put the username in session and then retrieve it anywhere in your application.

c) You can pass the username in queryString and then retrieve it in second servlet.

there are other was also like setting it in cookies, taking hidden fields (in JSP) but I am sure one of the above methods will fullfill your requirement.

thanks

[ December 28, 2005: Message edited by: Himanshu Bisht ]
[ December 28, 2005: Message edited by: Himanshu Bisht ]
Swetha Mohan
Greenhorn

Joined: Dec 23, 2005
Posts: 15
Hello

Can you please explain the three methods briefly.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by Swetha Mohan:
Hello

Can you please explain the three methods briefly.



Do this for now...


Write this code in LoginServlet


Write this code in nextPage.jsp



Hope it helps.
Swetha Mohan
Greenhorn

Joined: Dec 23, 2005
Posts: 15
Hello rathi

The session method worked...

Thankyou for that
Tirupathi Rao Chukka
Greenhorn

Joined: Dec 31, 2005
Posts: 2
Wishing you and your families a Happy And a Prosporous New Year 2006 for all Javaranch members


Regards,<br />Tirupathi.
Tirupathi Rao Chukka
Greenhorn

Joined: Dec 31, 2005
Posts: 2
you want to mentain the objects through your applications means u need to follow the any one of the technique

You can do it by storing object in requset (ServletRequest)

setAttribute(key,value); now you can get this object by using the following method getAttribute(key);

or you can store it in the session same methods are also availabe.

you need to read the session tracking 4 mechanisims that will help you lot
 
Consider Paul's rocket mass heater.
 
subject: Passing value from one servlet to another