Discussions

Web tier: servlets, JSP, Web frameworks: Session cannot be got with putValue

  1. Session cannot be got with putValue (4 messages)

    I have a quick question regarding the session
    tracking.
    In the first servlet, I did
        HttpSession session = req.getSession(true);
        Image img = new Image("ABC");
        session.putValue("sss", img);
    There is no problem in getting object img with
    getValue("sss");
    The first servlet also generate a simple page with a
    button to trigger the second servlet.
    My problem is that I could not get the value for the
    img object in the second servlet. Here is what I did
    in the second servlet
        HttpSession session = req.getSession(false);
        Object obj = session.getValue("sss");
        Image s = null;
        s = (Image)obj;

    This question will be so simple to you. But I just
    cannot retrive the img object in the second servlet.
    I will have ClassCastException at s = (Image)obj, but
    if I print obj, I get Image@678fa (which means it know
    that it is from Image)

    However, if I pass a String around, there is no
    problem in getting the value. Image is nothing but a
    class contains a String implement Serializable.

    I appreciate your time if you take a look at this
    issue.

    Also I attach the two servlet with Image class in this
    email. They are very short.

    Thanks so much.


    public class SessionServlet extends HttpServlet {
    public void doGet(HttpServletRequest req,HttpServletResponse resp)
              throws ServletException, IOException {
       //doPost(req, resp);
       HttpSession session = req.getSession(true);
        //Image img = new Image("ABC", 999);
        Image img = new Image("ABC");
        String abc = "ABC";
        session.putValue("sss", img);
       resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        out.println("<html> <body>");
        out.println("<form action=\"/servlet/CheckSessionServlet\" method=get>");
        out.println("<input type=submit value=PRESS1 size=19>");

        out.println("</form>");

        out.println("</body></html> ");

      }
    }



    public class CheckSessionServlet extends HttpServlet {
    public void doGet(HttpServletRequest req,HttpServletResponse resp)
              throws ServletException, IOException {
       //doPost(req, resp);
       HttpSession session = req.getSession(false);

        Object obj = session.getValue("sss");
        Image s = (Image)obj;

    resp.setContentType("text/html");
         PrintWriter out = resp.getWriter();

        //HttpSession session = req.getSession(false);
        //resp.setContentType("text/html");
        //PrintWriter out = resp.getWriter();
        out.println("<html> <body>");
        //String abc = "ABC";
        //session.putValue("sss", abc);

        //Image s = null;
        //if(obj instanceof Image)
        //String s = (String)obj;
        // s = (Image)obj;
        //else
         out.println("I am not really a Image Obj<BR>");
        out.println("The string is "+ obj.toString() +"<BR>");
        out.println("The string is "+ s.a +"<BR>");

        out.println("</body></html> ");
      }


    public class Image implements Serializable {
    String a;
    Image(String a) {
    this.a = a;
    }
    }
  2. Hi!

    If you get a ClassCastException,
    I can only see its a problem if you
    does not IMPORT the right Image class.
    Depending on which servlet engine you are using
    you might want to look at: setAttribute(string, object),
    getAttribute(string) instead if you are using a Servlet 2.2
    compatible servlet engine.

    Lars Borup
  3. Dude,

    You have to use req.getSession(true) in the second servlet and not false as you have it right now.
  4. Session cannot be got with putValue[ Go to top ]

    Hi even i have a similar situation but am not able to figure out why.

    Really beat me .

    Try getAttribute & setAttribute

    as getValue & putValue are deprecated

    I use JSP & servlet eith a bean in between.

    Even i get ClassCastException

    for this line

    Order myOrder = (Order) session.getAttribute("frompage1");

    in Servlet i set

    session.setAttribute("frompage1",oldOrder);

    where oldOrder id reference to class.

    Bye
    Krish


  5. Session cannot be got with putValue[ Go to top ]

    Try clearing ur browser cache.

    It worked for me

    Bye
    Krish