Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

sessions

  Asked By: Rose    Date: Mar 14    Category: Java    Views: 672
  

i want the examples based on session.I am creating one website in
which i want to use sessions,but i don't know how to use it.So will
you please help me to get some examples over it.I want an
application in which user login in the website and then using
web.xml his session will be invalidated after 30 minutes.
So how should i proceed?how and where i should use HttpSession in
my java programs.

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Ujala Hashmi     Answered On: Mar 14

I think if you place an specifiec object for example a String into the session  first time a user  called your front servlet or your front/home JSP

by this snippet: (in the doGet method of your front servlet)


String userName = null;

userName = request.getParameter("user_name");

if (userName == null) {

final HttpSession session = request.getSession();


if (session.getAttribute("USER_LOGIN") != null) {
/** do normal forwarding to other parts of system **/
} else {
/** forward to a page which asks the user name/password from user and then sends them to this front servlet again **/
request.getRequestDipatcher("login.jsp").forward(req, res);
}
return;
}


session.setAttribute("USER_LOGIN", userName);

and then:

Thread thread = new Thread() {
public void run() {

sleep(30*60*1000);
session.invalidate();
}
}

thread.start();

 
Didn't find what you were looking for? Find more on sessions Or get search suggestion and latest updates.




Tagged: