By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,612 Members | 1,632 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,612 IT Pros & Developers. It's quick & easy.

understanding session tracking in a jsp- servlet - jsp-servlet transition

P: 25
Could some one please explain why the session is not being maintained when I am doing a forward in a servlet after setting a

cookie. I am even unable to set session attributes or parameters and do a forward from a servlet to another servlet via a JSP

page, the attributes seize to exist. I still do not understand the concept of session tracking.

First page JSP:
_______________________________________________
Expand|Select|Wrap|Line Numbers
  1. <%@ page language="java"%>
  2. <html>
  3. <head>
  4. <title>Cookie Input Form</title>
  5. </head>
  6. <body>
  7.  
  8. <!-- <form method="post" action="setcookie.jsp">
  9.  -->
  10. <form method="get" action="/UndServ/FwJSPCookie">
  11.  
  12. <p><b>Enter Your Name: </b><input type="text" name="username"><br>
  13. <input type="submit" value="Submit">
  14. </form>
  15.  
  16. </body>
  17. _______________________________________________
  18. Servlet: FwJSPCookie.java
  19. _______________________________________________
  20.  
  21. package cookieServlet;
  22.  
  23. import java.io.IOException;
  24. import java.util.Date;
  25.  
  26. import javax.servlet.RequestDispatcher;
  27. import javax.servlet.ServletException;
  28. import javax.servlet.http.Cookie;
  29. import javax.servlet.http.HttpServletRequest;
  30. import javax.servlet.http.HttpServletResponse;
  31. import javax.servlet.http.HttpSession;
  32.  
  33. public class FwJSPCookie extends javax.servlet.http.HttpServlet implements
  34.         javax.servlet.Servlet {
  35.     public FwJSPCookie() {
  36.         super();
  37.     }
  38.  
  39.     protected void doGet(HttpServletRequest request,
  40.             HttpServletResponse response) throws ServletException, IOException {
  41.  
  42.         String username = request.getParameter("username");
  43.         if (username == null)
  44.             username = "";
  45.  
  46.         Cookie cookie = new Cookie("username", username);
  47.         cookie.setMaxAge(60);
  48.         response.addCookie(cookie);
  49.  
  50.         RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(
  51.                     "/jspCookies/showcookievalue.jsp");
  52.         dispatcher.forward(request,response);
  53.     }
  54.  
  55.     protected void doPost(HttpServletRequest request,
  56.             HttpServletResponse response) throws ServletException, IOException {
  57.         // TODO Auto-generated method stub
  58.     }
  59. }
  60. __________________________________________________
  61. JSP page: showcookievalue.jsp
  62. __________________________________________________
  63.  
  64. <%@ page language="java"%>
  65. <%String cookieName = "username";
  66.             Cookie cookies[] = request.getCookies();
  67.             Cookie myCookie = null;
  68.             if (cookies != null) {
  69.                 for (int i = 0; i < cookies.length; i++) {
  70.                     if (cookies[i].getName().equals(cookieName)) {
  71.                         myCookie = cookies[i];
  72.                         break;
  73.                     }
  74.                 }
  75.             }
  76. %>
  77.  
  78. <html>
  79. <head>
  80. <title>Show Saved Cookie</title>
  81. </head>
  82. <body>
  83.  
  84. <%if (myCookie == null) {
  85. %>
  86. No Cookie found with the name
  87. <%=cookieName%>
  88. <%} else {
  89. %>
  90. <p>Welcome: <%=myCookie.getValue()%>. <%}
  91.  
  92.         %>
  93. </body>
  94.  
_____________________________________________

If I do the same thing using a JSP in between, I am able to access the new cookie added in the final JSP page.

Even session.setAttribute("paramname",paramvalue) parameters seize to exist in the next servlet that I want to access in.

There seems to be a big confusion about saving session here. Can anybody pleaaase help me!
Nov 12 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.