0

I'm building a web application from an existing project. In the existing project I have a class that contains all my objects and the things I can do with them. I was wondering what will happen if I had an instance of this class to a servlet as a data member:

  1. When the same user with same session is directed to the servlet that contains this class will it keep it's data or will it regenerate every time?
  2. Will every user/session have a different copy of this member or is it shared?
  3. If data members in servlets don't keep thir state for the same session, then what do you recommend? Maybe activly adding it to the session?

Thanks for your help

2 Answers 2

3

Servlets - thus their data members - are shared between all sessions on the server. Thus

When the same user with same session is directed to the servlet that contains this class will it keep it's data or will it regenerate every time?

The data will be kept around (for all users) until you restart the web application.

Will every user/session have a different copy of this member or is it shared?

It is shared.

If data members in servlets don't keep thir state for the same session, then what do you recommend? Maybe activly adding it to the session?

Session specific data should be stored in an HttpSession.

Sign up to request clarification or add additional context in comments.

2 Comments

just to make sure I understand you correctly: if I have a servlet data member of type String that keeps errors text for one user, if another one will connect from a different browser he will get the same error text? I was under the impression that since servlets are used for dynamic pages, each user gets his own version...
@Ran, yes, the other user will get the same error text. There is only one servlet instance per web app, serving all requests (in different threads), and servlets are not even thread safe. See e.g. this thread for more details.
0

To be sure of this behavior, I wrote a little TestingServlet - I will show you the lifecycle of a servlet and its members. Also supplied; How to work with session variables

import java.io.IOException; import java.io.PrintWriter; import java.util.logging.Level; import java.util.logging.Logger; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Basic servlet for lifecycle testing * * @author powermicha * */ public class TestingServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 4020575563844924588L; private Logger logger; private int requestCounter; @Override public void init() throws ServletException { logger = Logger.getLogger("TestingServlet_" + System.currentTimeMillis()); logger.log(Level.INFO, "TestingServlet initialized"); requestCounter = 0; } @Override public void destroy() { logger.log(Level.INFO, "TestingServlet destroyed"); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { int requestsPerSession = incrementRequestsPerSession(req); String logMessage = "TestingServlet was called " + (++requestCounter) + " times. " + requestsPerSession + " times from the same session (ID:" + req.getSession().getId() + ")"; logger.log(Level.INFO, logMessage); // send it to the browser PrintWriter writer = resp.getWriter(); writer.write(logMessage); writer.close(); } private int incrementRequestsPerSession(HttpServletRequest req) { Integer counter = (Integer) req.getSession().getAttribute("requestsPerSession"); if (counter == null) { counter = 1; } else { counter++; } req.getSession().setAttribute("requestsPerSession", counter); return counter; } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.