Pls clarify my SESSION problem, thanks (technical knowledge)
Tazzmission
Greenhorn
Joined: Jan 16, 2002
Posts: 20
posted
0
I've created 2 servletjava: Userlogin.java(code below) -> create a session and user object (using UserLog.java), display "logined in" if user object found. UserLog.java (code below) ->user object storing simple username. I placed them in one directory called hello. Works fine. I copied Userlogin.java to s1.java and try to run s1.java servlet after I run Userlogin (which created a session and user object for me), then I run s1.java servlet, it SHOULD display "I've loginned" (because Userlogin.java created a session already), YES it WORKS fine. HOWEVER, if the s1.java servlet is NOT in the same directory (in here 'hello') and run in root, or other place, it CAN'T work AND just display Internal Server error. Please tell me if I am really CAN'T DO like this? Please tell me the reason why? THANKSX10000
HttpSession session = req.getSession(true); // We should always get a session back if (session == null) { out.println("ERROR: Internal servlet problem - no session"); out.flush(); out.close(); return; } // Get the current user info. If we get back a null, the // user is not currently logged in UserLog ulog = (UserLog) session.getAttribute(USER); // Get the requesting URI - that should be us // Get our URI String uri = req.getRequestURI();
try { // Figure out what was requested
if (ulog == null) { String user = "myname"; int no=1; ulog = new UserLog(); ulog.setUser(user); ulog.setLoginCount(no); String a=ulog.getUser(); int b=ulog.getLoginCount(); session.setAttribute(USER, ulog); out.println("<html>"); out.println("create user session success"); out.println(a); out.println(b); out.println("</html>"); } else { String a=ulog.getUser(); int b=ulog.getLoginCount(); out.println("<html>"); out.println("logined already"); out.println(a); out.println(b); out.println("</html>"); } } catch (Exception ex) { // Catch any exceptions and send the stack trace back // to the client ex.printStackTrace(out); } // Set the response header to force the browser to // load the html from the Web instead of it's cache resp.setHeader("Expires", "Tues, 01 Jan 1980 00:00:00 GMT");
-- package hello; public class UserLog { String m_user; // The user ID int m_loginCount; // Number of times that the user has logged in /** * <p>Sets the user ID * * @param user User ID */ public void setUser(String value) { m_user = value; } /** * <p>Gets the user ID * * @return User ID */ public String getUser() { return m_user; } /** * <p>Sets the login count * * @param count Login count */ public void setLoginCount(int value) { m_loginCount = value; } /** * <p>Gets the login count * * @return Login count */ public int getLoginCount() { return m_loginCount; } }
Ken Shamrock
Ranch Hand
Joined: Jan 23, 2002
Posts: 139
posted
0
sorry i post in wrong gp,i've post in Servlet group now