aspose file tools*
The moose likes Servlets and the fly likes Pls clarify my SESSION problem, thanks (technical knowledge) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Pls clarify my SESSION problem, thanks (technical knowledge) " Watch "Pls clarify my SESSION problem, thanks (technical knowledge) " New topic
Author

Pls clarify my SESSION problem, thanks (technical knowledge)

Ken Shamrock
Ranch Hand

Joined: Jan 23, 2002
Posts: 139
I've created 2 servlet java:
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

package hello;
import javax.servlet.*;
import javax.servlet.http.*;
public class Userlogin extends HttpServlet
{
// The session constant for the user key
String USER = "USER";
public void doGet(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, java.io.IOException
{
resp.setContentType("text/html");
java.io.PrintWriter out =
new java.io.PrintWriter(resp.getOutputStream());
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");
// Wrap up
out.flush();
out.close();
}
public void doPost(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, java.io.IOException
{
// Same as get
doGet(req, resp);
}
public void init(ServletConfig cfg)
throws ServletException
{
super.init(cfg);
}
public void destroy()
{
super.destroy();
}

}

--
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;
}
}


Thanks everyone who helped me
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
Thats the way the servlet API is supposed to work - sessions are not shared between two "web applications", and having two different base URLs makes them different applications.
Download the servlet API specs from java.sun.com and read up on web application rules.
Bill


Java Resources at www.wbrogden.com
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

No you can't really do that.

It's because of packaging.

Your s1.java servlet is in the package hello. So that's where it needs to be.. in the hello directory under /WEB-INF/classes
Ken Shamrock
Ranch Hand

Joined: Jan 23, 2002
Posts: 139
Thanks all:
to William Brogden:
So all servlet in the SAME directory is viewed as "Same Application"?

to Mike:
my s1.java have erase out the "package hello" already when compile,while s1.java still using the UserLog.java in hello directory
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
"So all servlet in the SAME directory is viewed as "Same Application"?
Right. At the risk of repeating myself, download the servlet API documentation from java.sun.com and read up on web applications. There is no excuse for anybody working with servlets and JSP and not having this documentation.
Sun has gone to a LOT of effort to create independence between web applications - the goal is to make it possible to take a WAR file, stick it in a server, and have it run first time without any nasty interactions with other applications in the same box.
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pls clarify my SESSION problem, thanks (technical knowledge)
 
Similar Threads
whilst fooling around with SessionId and cookies...
Pls clarify my SESSION problem, thanks (technical knowledge)
Servlet User Roles
Strange Problem : Redirect to other Servlet using dispatch
session problem --======urgent plz======--