Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I share a variable or object between two or more Servlets?

I would like to know if there is some way to share a variable or an object between two or more Servlets, I mean some "standard" way. I suppose that this is not a good practice but is a easier way to build a prototype.

I don't know if it depends on the technologies used, but I'll use Tomcat 5.5


I want to share a Vector of objects of a simple class (just public attributes, strings, ints, etc). My intention is to have a static data like in a DB, obviously it will be lost when the Tomcat is stopped. (it's just for Testing)

like image 512
David Ameller Avatar asked Sep 23 '08 20:09

David Ameller


People also ask

How do you pass values between servlets?

The only mechanism to call a servlet is by issuing an HTTP request. Also, only the servlet container is allowed to call your servlets, handling their lifecycle. This means you can set some servlet context attribute in one servlet and then wait for user to call the second servlet - which will see the global value.

Which interface is used to pass data between two servlets?

Usage of ServletContext Interface The object of ServletContext provides an interface between the container and servlet. The ServletContext object can be used to get configuration information from the web. xml file.

Which object would you use to share user specific information between servlets Jsps A request response C Session D application?

Ans: javax. servlet. ServletConfig is used to pass configuration information to Servlet. Every servlet has its own ServletConfig object and servlet container is responsible for instantiating this object.


2 Answers

I think what you're looking for here is request, session or application data.

In a servlet you can add an object as an attribute to the request object, session object or servlet context object:

protected void doGet(HttpServletRequest request, HttpServletResponse response) {     String shared = "shared";     request.setAttribute("sharedId", shared); // add to request     request.getSession().setAttribute("sharedId", shared); // add to session     this.getServletConfig().getServletContext().setAttribute("sharedId", shared); // add to application context     request.getRequestDispatcher("/URLofOtherServlet").forward(request, response); } 

If you put it in the request object it will be available to the servlet that is forwarded to until the request is finished:

request.getAttribute("sharedId"); 

If you put it in the session it will be available to all the servlets going forward but the value will be tied to the user:

request.getSession().getAttribute("sharedId"); 

Until the session expires based on inactivity from the user.

Is reset by you:

request.getSession().invalidate(); 

Or one servlet removes it from scope:

request.getSession().removeAttribute("sharedId"); 

If you put it in the servlet context it will be available while the application is running:

this.getServletConfig().getServletContext().getAttribute("sharedId"); 

Until you remove it:

this.getServletConfig().getServletContext().removeAttribute("sharedId"); 
like image 111
William Avatar answered Nov 05 '22 17:11

William


Put it in one of the 3 different scopes.

request - lasts life of request

session - lasts life of user's session

application - lasts until applciation is shut down

You can access all of these scopes via the HttpServletRequest variable that is passed in to the methods that extend from the HttpServlet class

like image 33
bpapa Avatar answered Nov 05 '22 15:11

bpapa