1

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!??

Rebooting
  • 2,762
  • 11
  • 47
  • 70

3 Answers3

5

you can obtain and check using pageContext in EL

<c:if test="${pageContext.session['new']}">Session is new</c:if>
TheWhiteRabbit
  • 15,480
  • 4
  • 33
  • 57
  • so .. thats the right way ? i mean i shouldn't create a seperate action class to check if session exists and then transfer the control to jsp ? Using struts 2 framework! – Rebooting Jan 28 '13 at 06:49
  • pageContext is an implicit object containing reqeust, session and many things to provide JSP page with instant information to help cases like the one you mentioned! – TheWhiteRabbit Jan 28 '13 at 06:54
0

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();
}
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
0

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

Nirdesh Sharma
  • 734
  • 5
  • 14