-1

In jsp we can create the cookies and session.We can see the cookies since they are stored at client side.Session variables are stored at server side and how can we see the session variables?I want full informatin about session and cookies.

user1286481
  • 81
  • 1
  • 1
  • 4

2 Answers2

1

In JSP, you can iterate through all cookies and session attributes as follows:

<h2>Cookies</h2>
<dl>
    <c:forEach items="${cookie}" var="cookieEntry">
        <dt><c:out value="${cookieEntry.key}" /></dt>
        <dd><c:out value="${cookieEntry.value.value}" /></dd>
    </c:forEach>
</dl>

<h2>Session attributes</h2>
<dl>
    <c:forEach items="${sessionScope}" var="sessionEntry">
        <dt><c:out value="${sessionEntry.key}" /></dt>
        <dd><c:out value="${sessionEntry.value}" /></dd>
    </c:forEach>
</dl>

(yes, ${cookieEntry.value.value} is correct; the ${cookie} is a Map<String, Cookie>, every iteration gives a Map.Entry instance back which has getKey() and getValue() methods, the ${cookieEntry.value}thus returns aCookieobject which in turn also has agetValue()` method for the actual cookie value)

Note that the information about session attributes is not available in the client side. Only cookie information is available in the client side (by HTTP response headers). So make sure that you don't store sensitive information in cookies, but only in the session. To learn more about how it all works, see also How do servlets work? Instantiation, sessions, shared variables and multithreading.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I understand the code .When client send the session using cookies server checks the session id whether session id exists or not.From where session is retrieving this session id.Next question is How can we see the cookie information? – user1286481 Mar 23 '12 at 16:00
  • It's the cookie with the name `JSESSIONID`. The servlet container handles this all transparently for you whenever `request.getSession()` is called. The container will return the right `HttpSession` instance from server's memory based on the value of the `JSESSIONID` cookie. See also the link at the bottom of the answer for a more detailed explanation. – BalusC Mar 23 '12 at 16:06
0

You can learn Jsp debugging techniques from here: http://www.tutorialspoint.com/jsp/jsp_debugging.htm

Basically, I think the most simple and straight method is using System.out.print().

D_S_toowhite
  • 643
  • 5
  • 17