Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
    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
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

Pls clarify my SESSION problem, thanks (technical knowledge)

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
}
}
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
sorry i post in wrong gp,i've post in Servlet group now
 
when your children are suffering from your punishment, tell your them it will help them write good poetry when they are older. Like this tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
    Bookmark Topic Watch Topic
  • New Topic