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