5

I need to share information while the applicaiton is running; if I have:

public class example extends HttpServlet
{
    Object globalObject;

    doGet...
    doPost....
}

A user is using the aplication through server and the object globalObject; if another user use the application, will share the object with the first user?

Dave G
  • 9,639
  • 36
  • 41
user3550529
  • 135
  • 2
  • 3
  • 14
  • thanks for the answers. I ask this because I need submit a form and save it temporality for later the user submit another form and then save 2 forms on database; any suggestion for do that? – user3550529 Jun 29 '14 at 03:58
  • Using instance variables in Servlet is not the way to do that. Store the form in temp table when it is receive in the request. When you have two forms in the temp table call stored proc to do the processing and store two forms in your correct table. – Aniket Thakur Jun 29 '14 at 04:03

5 Answers5

3

A user is using the aplication through server and the object globalObject; if another user use the application, will share the object with the first user?

Yes! Different threads might be used to render request for different users but the same servlet instance is used.So yes the variable will be common to all requests. In-fact this is why it is said we should not have global variables to ensure thread safety.

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
2

The same instance of the variable will be used by all requests handled by the servlet. Servlets are not thread safe since only one instance of the servlet is created.

This will cause two users to use the same instance of globalObject.

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
2

with HttpSession your variable will be related to each users session, not the application itself

you can do this as follow

ServletContext application = getServletConfig().getServletContext();  

String data = "test";  
application.setAttribute("variable", data);  

String data_rtrvd= (String) application.getAttribute("variable"); 

Is JSP code you can do:

<jsp:useBean id="obj" class="my.package.name.MyClass" scope="application" />
2

That depends on how your application server allocates servlets.

If your application server allocates only one servlet instance, then yes, all requests will share access to the global variable and you must account for that in your design (unless you choose to implement the deprecated SingleThreadModel interface, which will guarantee that, while all requests will have access to the global variable, they won't access it concurrently. Don't do it. Find another way).

If your application server allocates more than one servlet instance, then the answer is "not necessarily".

Obviously, you are often masked from the server's decisions (as to whether to instantiate multiple instances or not), so you must design for safety.

Isaac
  • 16,458
  • 5
  • 57
  • 81
1

Generally speaking, threads are singletons, so the answer to your question is yes. However, if you want to share data between different users, you should use a true Singleton implementation. Also consider concurrency, because you'll surely have many threads executing at the same time (One for each request that is received by the server)

Andres
  • 10,561
  • 4
  • 45
  • 63