#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Posts
    181
    Rep Power
    14

    3 question about cookie and session please.


    1. I know that session automatic use in JSP by default and save session id in cookie. I have code that show all cookie on my computer
    Code:
    <%
    	Cookie [ ] listcookie = request.getCookies();
    	Cookie mycookie = null;
    	for(int i=0; i<listcookie.length; i++)
    	{
    		mycookie = listcookie[i];
    		out.println("<tr><td>&nbsp;" + mycookie.getName() + "</td>");
    		out.println("<td>&nbsp;" + mycookie.getValue() + "</td></tr>");
    	}
    %>
    When I first run this code it now show cookie. But when I click refresh it show session cookie. Why it not show session cookie at first time?

    2. I use tomcat on window. Is session on server is save as file. Which directory it save?

    3. I use method getCreationTime() to check first create session time and method session.getLastAccessedTime() to check last access session time. I have to file

    showsession1.jsp
    Code:
    first time create is <%=session.getCreationTime()%>  <br>
    last time access is <%=session.getLastAccessedTime()%> <br>
    <a href="showsession2.jsp"> showsession2.jsp</a>
    showsession2.jsp
    Code:
    first time create is <%=session.getCreationTime()%>  <br>
    last time access is <%=session.getLastAccessedTime()%> <br>
    When I open browser and run showsession1.jsp first time create and last time access is equal when I click link to showsession2.jsp it also equal. Why it still equal because it is second time that I access JSP file? It change when I refresh file showsession2.jsp
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Location
    Boston, MA
    Posts
    30
    Rep Power
    57
    I think the answer to your first question is:

    When the request is sent from the client the first time, there is no session. If you use servlets at all, you'll see that there are 2 methods for getting the session:
    Code:
    1) request.getSession() // returns a session or null
    2 request.getSession(boolean create) // if the session is null, one will be created
    A session is created automatically by jsp by using the second method, but this is done for you automatically. When the session cookie is referenced by subsequent requests, it exists.

IMN logo majestic logo threadwatch logo seochat tools logo