0

Please help me. Is there a way how I can use a collection which is shared between all the Servlet instances?

I know I can save a string in HttpRequestContext but how can I store a collection? I know I could use a database but I'd prefer to store everything in memory

public class Servlet extends HttpServlet {

    public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
            throws ServletException, IOException
  {
      context.setAttribute("someValue", "aValue");
      //  Want to use collection here
  }

}

Thank you for your answers.

spandei
  • 209
  • 2
  • 14

3 Answers3

3

You can save any object in the request, response or in the session. When you retrieve it, you just have to cast it back the correct class.

wvdz
  • 15,266
  • 3
  • 43
  • 82
1

You can store it in the session - quite common. But also in some public static Collection in an external class - for the sake of example Util.myCollection. Be careful though, servlets should be thread-safe and stateless, so the latter can be easily considered as an anti-patttern - you can get into lot of trouble doing this.

bad_habit
  • 359
  • 3
  • 10
  • The problem is I need to store data from different sessions. From what I've seen from other questions and servlets spec - if I don't use SingleThreadModel - it means there will be only one instance of the Servlet in the Servlet container. So it means - I can store this collection in a field which will be shared amongst all threads processing request (I need to care only about synchronization). Is this correct? I'm sorry for asking stupid questions but I'm quite new to Java and Servlets especially. Thank you for your answer! – spandei Jul 07 '15 at 22:22
  • That's right - servlets are singletons in the container scope if not explicitly defined otherwise. There might be a lot of threads using this servlet instance (each thread handling single HTTP request), so you should take really good care of what you're doing inside it. Synchronizing the access to the shared collection is the solution for thread-safety, but it will decrease performance (incoming threads will wait for their turn). If you want to share/update a single value, try java.util.concurrent.atomic package depending on type you want to store and the use case you want to deliver. – bad_habit Jul 08 '15 at 00:15
  • 1
    A fat downvote for recommending to duplicate application scoped data over all sessions. – BalusC Jul 08 '15 at 09:01
  • Sure. Thanks for pointing that out - session shouldn't be used like that. But again.. final solution will be chosen depending on the use case. – bad_habit Jul 09 '15 at 22:09
0

It depends on where do you want to store:

  • ServletContext.setAttribute(): for lifetime of application.
  • HttpServletRequest.setAttribute(): for lifetime of request. This will not be available for further requests.
  • HttpServletSession.setAttribute(): for lifetime of user session scope. This will be available only with user session.
Ramesh PVK
  • 14,700
  • 2
  • 43
  • 49