session

Hi for all,plz i need to know Are there a method to destroy the session in servlet?i need to destroy the session when admin press logout link , to prevet him go back to his homepage unless he retype his user name and password.Regards.
[262 byte] By [baby_developera] at [2007-11-26 17:12:06]
# 1
I think the session is created per browser, as soon as the browser is closed the session is automatically removed.Use HttpSession's removeAttribute(attributeName); , to explicitly remove an object from the session inside the code you call when the user logs out.
appy77a at 2007-7-8 23:39:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Use session.removeAttribute()thensession.invalidate()// it expires ur session
java_usera at 2007-7-8 23:39:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Hi, I did use:1)session.setAttribute("model", null);2)session.removeAttribute("model");3)session.invalidate(); this three methods but it still cannot clear the session. hmmm anyone can help me thanks.
Angelinea at 2007-7-8 23:39:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Does the code to disable the session execute after the user hits the log out button?On the log-in page you can check session == null || session.getAttribute("model") == null ,if it is null then log the user out.
appy77a at 2007-7-8 23:39:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

HTTP requests do not preserve state by themselves. The HTTP protocol provides a mechanism for returning a single response to s single request. J2EE provides a mechanism for tying individual requests together into a session with the apprriately named "Session" object. The mechanism it uses is a small id code usually called a JSessionID that exists in the URL or in a cookie which gets sent with every browser request.

When do Session end? When they "timeout". Remember that configuration item in the web.xml;

<session-config>

<session-timeout>20</session-timeout>

</session-config>

The J2EE container checks all of its Sessions and decides whether to remove them based on whether any requests have come in for a particular session for the designated time period(In this case: 20 minutes).

So I would guess a reasonably effective way to do this would be to call the method:

session.setMaxInactiveInterval(1);

This would tell it to timeout in 1 second.

Having said that... I have to admit I have never called this method myself. If I need to log someone out of a password protected area of a website, I just remove the session attribute token that tells me they are logged in.

john.m.pagea at 2007-7-8 23:39:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
Thinking out of the box a little bit here, is using sessions to authenticate a user really secure?I've heard of JAAS - Java Authentication and Authorization - but never used it. Perhaps it has a framework that allows one to easily handle user authentication.
appy77a at 2007-7-8 23:39:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

Using the Session object is reasonably secure. However, there are other steps that need to be taken to ensure that security.

1.) The password needs to be sent over a secure https request.

2.) All subsequent requests need to go over secure https requests.

3.) Steps need to be taken to stop the browser from caching the pages

(For example: you could use javascript;window.location.replace() instead of ordinary URLs)

If the pages are cached, a user can hit the "Back" button and view old pages.

4.) Of course, the application has to check for the signed-in-token with every request.

(A good place to use a J2ee Filter that maps to "/*" all requests.)

john.m.pagea at 2007-7-8 23:39:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
JAAS is only a way to authenticate the user, how you maintain the state is a different thing.
beradriana at 2007-7-8 23:39:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...