Hi All,

I have a silly question,  aren't we suppose to share the same session
object among JSPs and Servlets.  I'm trying to set the value of a session
variable in a JSP page.  Then I call a servlet off of that page.  Inside
servlet I dont get the session variable value.  I thought I should get the
value.  I'm pasting sample code for a JSP page and Servlet.  Can some one
please help me and tell me what is wrong,

JSP Code
[SetSession.jsp]
<html>
<head>
<META name="GENERATOR" content="IBM WebSphere Studio">
</head>
<body>
<FORM ACTION="http://localhost:8080/servlet/GetSession";  METHOD="GET">

<%

session.setAttribute("username", "Test User");
String username = (String)session.getAttribute("username");
%>
Welcome , <b><%= username %></b>!
<p>
Your session ID is <% out.println( session.getId() ); %>
<p>

<INPUT type="submit" name="submit" value="Submit">

</form>

</body>

</html>

[Servlet Code]
[GetSession.java]
// Import Servlet Libraries
import javax.servlet.*;
import javax.servlet.http.*;

// Import Java Libraries
import java.io.*;
import java.util.*;


public class GetSession extends HttpServlet {

      public void doGet(HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException {

            res.setContentType("text/html");
            PrintWriter out = res.getWriter();
            StringBuffer buffer = new StringBuffer(1024);

            // Set the header for the page
            buffer.append("<HTML><HEAD><TITLE>");
            buffer.append("Test App</TITLE>");
            buffer.append("</HEAD>");

            buffer.append("<BODY>");

            HttpSession session = req.getSession(true);

            String username = (String) session.getAttribute("username");

            if (username==null) {
                  buffer.append("<h4>Username is null</h4>");
            } else
            {
                  buffer.append("<h4>UserName is </h4>" + username);
            }
            buffer.append("</BODY></HTML>");
            out.println(buffer.toString());
            out.close();
  } // doGet

}

Thanks,

- Rizwan

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

Reply via email to