Rebooting
Rebooting

Reputation: 2938

How to check the existence of a session in a jsp page

I am building a web app and i need to check whether a session exists. I want to check that in a jsp page. I have created my session in an action class(Struts 2 framework). I don't want to use script-lets. Is there any way to do this using EL or anything else?

This is what i want to implement in the jsp without using script-let

<% HttpSession hs=request.getSession(false);
if(hs.isNew())
{

}
%>

Is this the right way to check a session or should i do the same in a seperate action class and then map the success to the jsp!??

Upvotes: 1

Views: 4763

Answers (3)

Nirdesh Sharma
Nirdesh Sharma

Reputation: 732

If you are using Struts 2, it’s better to manage a session through Interceptor. Sooner or letter you need to handle this session in java code as well. So,

  1. Create an Interceptor, where you check whether a session is created or not (You can ignore the home page).

  2. Create your own package with this inceptor on top in struts.xml file

You are done.

No matter of how many jsp,*.java page are created, this Interceptor will handle the session on new pages as well.

You can see http://www.vitarara.org/cms/struts_2_cookbook/creating_a_login_interceptor for how to work with custom interceptor

Upvotes: 0

TheWhiteRabbit
TheWhiteRabbit

Reputation: 15758

you can obtain and check using pageContext in EL

<c:if test="${pageContext.session['new']}">Session is new</c:if>

Upvotes: 5

Daniel Kaplan
Daniel Kaplan

Reputation: 67310

It should be pretty easy to do this. All you have to do is put this code inside the servlet, but make it accessible through a getter method. Then you can access it through EL.

public boolean isNewSession() {
    HttpSession hs=request.getSession(false);
    return hs.isNew();
}

Upvotes: 0

Related Questions