Hi
here is the service function code.
It first check session, if no user information, the ask user to input and
then save to session.
else it'll go to a function based on user request, then generate a report
using user information.
I don't know if I should not use synchronized.
Thanks.
Ben
============================================================================
public synchronized void service(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException
{
base = req.getServletPath();
out = res.getOutputStream();
res.setContentType("text/html");
if (req.getParameter("option") != null)
option = new Integer(req.getParameter("option")).intValue();
else option=0;
HttpSession session = req.getSession(true);
if (session.getValue("sessiontest.traderid")==null) {
username = (String) req.getParameter("username");
password = (String) req.getParameter("password");
if (username == null) { // new login, send input form .....
htmlStart(out, "Login Page");
printInputForm();
htmlEnd(out);
return;
}
else { // get input, make connect, check user, save session
try {
connectDB();
query = "select traderID,password from accounts where username= ? "
+
" and password= ? ";
stmt = con.prepareStatement(query);
stmt.setString(1,username);
stmt.setString(2,password);
rs = stmt.executeQuery();
if (!rs.next()) {
out.print(" Illegal traderID or password !!!");
closeAll();
return;
} else { // write user and password to cookie, go to Rpt1
traderid=rs.getString(1);
session.putValue("sessiontest.traderid", traderid);
session.putValue("sessiontest.password", password);
}
} catch (SQLException e) {
con = null;
htmlStart(out, "JDBC connection error");
out.print("Couldn't connect to <b>" + dbUrl + "</b><p>\n\n");
printSQLException(e, out);
htmlEnd(out);
return;
}
}
}
try {
// ********************************************************
// go into report, check cookie and make report ....
connectDB();
switch (option) {
case 1:
if (checkCookie(session))
printRpt1();
break;
.................
case 6:
logOff(session);
break;
}
} catch (SQLException e) {
printSQLException(e, out);
} finally { // close db connection anyway
try { con.close(); con=null; } catch (SQLException e2) { }
}
}
=================================================================
----- Original Message -----
From: "Nic Ferrier" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, May 24, 2000 1:09 PM
Subject: Re: Session problem
> >>> Ben Zu <[EMAIL PROTECTED]> 24-May-00 5:49:46 PM >>>
>
> >I used httpsession object in service function of my servlet,
> >when user make connection, my servlet save username
> >password in the session. and then the user access other pages ,
> > my servlet can trace user. but it always mess up when multiuser
> > make connection at the same time.
>
> You'll need to post your service() method... what you're doing should
> work.
>
>
> >Seems the httpsession object is Static, but it's NOT!
>
> Well... it could be some synchronization problem but what you're
> describing is almost boringly standard and should therefore be ok.
>
>
>
> Nic Ferrier
>
>
___________________________________________________________________________
> 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
___________________________________________________________________________
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