java - JSP session is not null -
i'm newbie jsp , on particular page of webapp i'm trying determine whether user logged in. this, i'm checking session existance. if session not exist redirect user login page.
i'm confused fact following code
<% if (null == session) out.println("session null"); else out.println("session not null"); if (null == request.getsession(false)) out.println("request.getsession() null"); else out.println("request.getsession() not null"); %>
under circumstances produces following output:
session not null request.getsession() not null
i don't understand why session exists when didn't create it. how check whether user logged in or not?
thanks in advance.
by default, session created container. jsp use
<%@ page session="false" %>
to disable session creation on jsp.
in order check if user exists, need put token/key in session on server-side after user logged in. like
request.getsession().setattribute("usertoken","authenticated");
and on jsp check attribute exists , not null
if ("authenticated".equals(session.getattribute("usertoken")) { // }
Comments
Post a Comment