blue-sky
blue-sky

Reputation: 53806

Storing different session values for multiple users on servlet

If user1 enters below servlet the current time in milliseconds is added to a session variable. If user2 instantiates this servlet some time after user1 the session variable will be set to the new time. When user1 & user2 requests this session value each will have their own unique value? Will user2 overwrite user1 session val ?

    public void doGet(HttpServletRequest request, HttpServletResponse response) {

        request.getSession().setAttribute("time", System.currentTimeMillis());

        try {
            response.getWriter().print("Time is set");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

Upvotes: 0

Views: 2518

Answers (4)

Buhake Sindi
Buhake Sindi

Reputation: 89169

The Servlet Container manages user sessions. Each user receives a JSESSIONID which is a session ID that the browser (or user-agent) that sends the session ID to the servlet container for retrieval of the particular user.

Upvotes: 0

isobar
isobar

Reputation: 1175

Both will have separate values. There will be 2 sessions for user1 & user2. User2 will not overwrite

Upvotes: 0

Boris Pavlović
Boris Pavlović

Reputation: 64632

No, user2 will not overwrite user1's session value, because it has its own instance of the session. Wikipedia has a pretty good section on web sessions.

Upvotes: 1

Jesper
Jesper

Reputation: 206796

Each user will have their own session. Users will not be able to see each other's sessions (if they can, there is something seriously wrong with the system).

Edit (thanks to Richard H): The servlet container will automatically find the session for the current user by looking at the request; the request will contain a header or a cookie with the session ID, so that the container knows which user is doing the request.

Upvotes: 3

Related Questions