• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Junilu Lacar
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst
  • Peter Rooke
  • Mikalai Zaikin
Bartenders:
  • Himai Minh

Pls clarify my SESSION problem, thanks (technical knowledge)

 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
}
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
 
Not looking good. I think this might be the end. Wait! Is that a tiny ad?
Master Gardener Program
https://coderanch.com/t/771761/Master-Gardener-Program
reply
    Bookmark Topic Watch Topic
  • New Topic