Servlet data members keep session?
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:
- 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?
- Will every user/session have a different copy of this member or is i开发者_如何学Ct 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?
Thanks for your help
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
.
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;
}
}
精彩评论