0

My problem is: I want to use sessions on my JSP pages, without using servlets (like php for example)

The problem is: how to do that? I read that session is implicite like request in jsp, but I don't actually know how to use it.

I've got a main page, who passes itself some variables on the url (like index.jsp?id=1).

Thank you for your help.

Rais Alam
  • 6,970
  • 12
  • 53
  • 84
abierto
  • 1,447
  • 7
  • 29
  • 57

3 Answers3

3

You can use JSTL <c:set> and <c:remove> to manage session attributes.

The following does effectively a session.setAttribute("foo", "bar"):

<c:set var="foo" value="bar" scope="session" />

And the following does effectively a session.removeAttribute("foo"):

<c:remove var="foo" scope="session" />

Of course, you can use the usual EL way of accessing attributes, the following prints session.getAttribute("foo").

${foo}

Unrelated to the concrete problem, you should work on your aversion against servlets. This is not a good practice. JSTL doesn't offer everything making business logic a breeze, which would force you to fall back to legacy scriptlets. I hope that it's just ignorance. Carefully read our servlets wiki page to learn how it can easily be created and used: https://stackoverflow.com/tags/servlets/info Also this answer might be somewhat enlightening: How to avoid Java code in JSP files?

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I appreciate your comment, but the main problem is that I'm working on something not created by me: this mean that I've found this "mess", and I don't think I have enough time to renew it with a correct code. As I read on those links, you are REALLY right, so thank you anyway. – abierto Dec 20 '12 at 13:14
2

Assume you have two jsp

  1. index.jsp = get the parameter in index.jsp and add the value to session (implicit objects)

    <%
    String empId = request.getParameter("id");
    session.setAttribute("empId", empId);
    response.sendRedirect("homePage.jsp");

    %>

  2. Now in homePage.jsp = Write code to retrieve the value previously added with key empId

    <%

    String employeId = (String)session.getAttribute("empId");

    %>

Rais Alam
  • 6,970
  • 12
  • 53
  • 84
1

you have to use this in jsp code

       <%

     HttpSession sess = request.getSession();

     if (sess==null)
     {
         RequestDispatcher dispatcher = request.getRequestDispatcher("/login.jsp");
         dispatcher.forward(request, response);                        
     }

      %>

you can get session and its attribute

and for index.jsp?id=1 you can use

         request.getParameter("id")
Manish Nagar
  • 1,038
  • 7
  • 12
  • The problem is that I don't redirect to other JSP, I handle everything in my index.jsp page. I've tried to use before 'HttpSession sess = request.getSession();' but when i submit something, and the page reload itself, I get an error because there are multiple declaration of sess variable. – abierto Dec 20 '12 at 11:28
  • please check session object before creation if it is already exist then do not create otherwise create it simply – Manish Nagar Dec 20 '12 at 11:37