Here's an example:
I have 2 servlets, 1 is used to login to the site & create a
session id. Notice that this ID is returned to the user
as an applet parameter, because my 2nd servlet is called by
an applet.
1. Here is part of the doGet method for the 1st servlet
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
.
.
.
//generate session id for user
Random r = new Random();
Integer uid = new Integer(
Math.round((float)Math.random()*100000000) );
String uip = request.getRemoteAddr();
getServletContext().setAttribute("login."+uip,uid);
response.setContentType("text/html");
PrintWriter out = new PrintWriter (response.getOutputStream());
out.println("<html>");
out.println("<head>");
out.println("<title>Session test</title>");
out.println("<meta http-equiv=\"Content-Type\" content=\"text/html;
charset=iso-8859-1\">");
out.println("</head>");
out.println("<body>");
out.println("<applet code=simplebasemap archive=/bm.jar width=650
height=400>");
out.println(" <param name=\"sessid\" value="+uid+">");
.
.
.
}
2. the applet always sends the session ID back to the 2nd servlet as a
parameter called "sid".
Here is part of the doGet method for the 2nd servlet
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
//authenticate
String id = request.getParameter("sid");
if ( id == null || id.equals("null") ) {
deny(response);
return;
}
int sid = (new Integer(id)).intValue();
String uip = request.getRemoteAddr();
Integer uid =
(Integer)getServletContext().getAttribute("login."+uip);
if ( ( uid == null) ||
( uid.intValue() != sid )
) {
deny(response);
return;
}
//id OK, do some work...
.
.
.
}
Ali Yildirim wrote:
>
> Can someone please post some example code to show how sessions work? I think this
>would help a lot in understanding
> sessions.
--
Raj R. Singh
Syncline, Inc.
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html