• Post Reply Bookmark Topic Watch Topic
  • New Topic

how 2 servlets communicates

 
bhavesh bhatnagar
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am creating an application having 2 servlets::

1) WelcomeServlet : Prints Welcome Message along with the userName and have one Button., something like "Take a tour"

2) TourServlet : Executes when user clicks on the Button on WelcomeServlet and it Prints the Same UserName along with some message


i am able to code two different Servlets.. but dont know how should i pass the UserName from one servlet to another??

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Store user-specific long-running information in the session.
 
bhavesh bhatnagar
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
soory but i idnt got You..

isn't there any method or way through which i can pass values from one servlet to another
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The servlet API provides many methods for sharing information between servlets.

IF both servlets are in the same web application, the usual way to share user-specific information is the "session" - an instance of HttpSession managed by the servlet container and associated with a user's browser session.

This is all very basic stuff, perhaps you should locate a tutorial on servlets

Bill
 
bhavesh bhatnagar
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks i will do it


and nice site Bill
 
bhavesh bhatnagar
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have solved the problem using ServletContext i.e by setting its name and value.. from the Welcome Servlet ..

just wanted to conform is this approach is Right... for a Live Application.
Is there any disadvantages to use this approach.??
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ServletContext values are shared between all users, this is not the place for a user name if you ever have more than one user at a time.

Information unique to a user's interaction with a particular application belongs in the HttpSession if you want to use the standard API. Each user session gets a separate HttpSession object that is managed by the container for the application.

Again, this is basic stuff better covered in a tutorial.

Bill
 
chris kumar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If both the servlet are in the same webapplication then you can do it as usual with the session object, this is very well like you store some session info into the session and forward it to the JSP's, this will also act the same way you store the information in the session and then forwarded the request to another component that is a servlet or say a JSP, then the request that you got on that component will have the session, access it to get the information.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!