Click to See Complete Forum and Search --> : new HttpSession


horea
06-01-2000, 04:41 PM
Can somebody tell me when and how ( based on what criteria ) the servlet container
decides to create a new HttpSession ?

Alex_dearmero
06-02-2000, 02:39 PM
"horea" <horea@hotmail.com> wrote:
>
>Can somebody tell me when and how ( based on what criteria ) the servlet
container
>decides to create a new HttpSession ?

In this example the jsp is invoking the user session bean and its labeling
it as session. If it doesn't exists the servlet engine will create the httpsession
with the values of the bean and automaticaly binds the session to the user

<%@page import = "jspcourse.UserSession" %>

<jsp:useBean id="userSession" class="jspcourse.UserSession" scope="session"/>


<html>
<head>
<title> Login Session Test </title>
</head>
<body>.....

User Id="<%=userSession.getUserId()%>" <br>

how does it look in servlets?...
if the parameter on getSession is true and the session does not exists yet
on the server, the servelt engine creates a new one.

HttpSession session = req.getSession(true);

String user = (String) session.getValue(catalog.USER_DATA);

if (user == null) {
username = req.getParameter(catalog.USER_DATA);
return;
}

bind the username to a object named catalog.USER_KEY on the server or update
the value.

session.putValue(catalog.USER_KEY, username);

to send the session to a next web component you should use URLencode method.

Hope this helps
Greetings

Alex