×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

How can JSP page get correct session object?

本文发表在 rolia.net 枫下论坛I wrote a java servlet and I'd like to forward the request to an JSP page. Just like below:

Servlet:

public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
HttpSession session = Request.getSession (true);
ObjA myObj = (ObjA) session.getAttribute ("MyObj");
if (myObj == null) {
myObj = new ObjA ();
session.setAttribute ("MyObj", myObj);
}

try{
RequestDispatcher dispatcher
= getServletContext().getRequestDispatcher("myJsp.jsp");
dispatcher.forward(request,response);
}
}

In myJsp.jsp, I used
<%ObjA myObj = (ObjA) session.getAttribute ("MyObj")%> to retrieve the same myObj instance.

When the Servlet is invoked first time, it create myObj and store it in session object. Then request is forwarded to JSP page, and then return output to client. Everything works well.

But how this JSP page can get same myObj from session? JSP should know the sessin id of course, sesssion id can be retrieved from cookie or URL rewriting, but at this moment, the response is not return to client broswer yet, so there is not cookie or other thing set on broswer side. JSP page should get the correct session id from applicatin server. But how can JSP page get it? Where does JSP get it?

Thanks in advance.

Marco更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 枫下家园 / 护照签证 / How can JSP page get correct session object?
    本文发表在 rolia.net 枫下论坛I wrote a java servlet and I'd like to forward the request to an JSP page. Just like below:

    Servlet:

    public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
    HttpSession session = Request.getSession (true);
    ObjA myObj = (ObjA) session.getAttribute ("MyObj");
    if (myObj == null) {
    myObj = new ObjA ();
    session.setAttribute ("MyObj", myObj);
    }

    try{
    RequestDispatcher dispatcher
    = getServletContext().getRequestDispatcher("myJsp.jsp");
    dispatcher.forward(request,response);
    }
    }

    In myJsp.jsp, I used
    <%ObjA myObj = (ObjA) session.getAttribute ("MyObj")%> to retrieve the same myObj instance.

    When the Servlet is invoked first time, it create myObj and store it in session object. Then request is forwarded to JSP page, and then return output to client. Everything works well.

    But how this JSP page can get same myObj from session? JSP should know the sessin id of course, sesssion id can be retrieved from cookie or URL rewriting, but at this moment, the response is not return to client broswer yet, so there is not cookie or other thing set on broswer side. JSP page should get the correct session id from applicatin server. But how can JSP page get it? Where does JSP get it?

    Thanks in advance.

    Marco更多精彩文章及讨论,请光临枫下论坛 rolia.net
    • Regarding session management and dispatcher
      First of all, it seems there is a typo in your servlet code. "request," instead of "Request".

      As I used the dispatcher, I found I must be extremely careful in
      putting URL for the destination page. "myjsp.jsp" or "/myjsp.jsp"? This is a place where the problem can arise.
      I hope you won't get trouble here. Actually, I cannot remember which one is the corect syntax!

      In the Servlet code, as you used dispatcher, you have forwarded request and response to the JSP page. In turn, session is associated with request.... so you have luxury in the JSP page to retrieve the items you have bound in session some time earlier.
      • Jabber, How do you know that?
        Jabber, the codes I wrote can run smoothly, but I don't know why JSP page can get correct session? You said:
        "In turn, session is ASSOCIATED with REQUEST.... "

        How can you know that? I can't find any specification or documents mention that.
        • Of course, It SHOULD be, but nobody tells me it MUST be
          • Come in, Marco
            From business perspective, a sequence of requests and responses construct a session. For each client, the servlet engine manages a session for it using cookie or URLRewriting.

            In the design of Servlet API, however, Sun chose request and response as central points and attached HttpSession to request object. This is opposed to our normal common sense, but this is also a kind of pattern(Sorry I cannot remember the name of this pattern.)

            I have ever read a paper in which the author criticised this design.

            Dispatcher just provides us with an API. to make things work, Servlet engine needs to support it INTERNALLY(or INVISIBLY).
            • Jabber, about session again
              Yes, I believe session is associated with request object. But Servlet API is a specification, different application server vendors implement it. If this idea is not mentioned in specification, how can we know whether it is implemented in a application server?
              By the way, Do you still remember the name of the article you read about it? Is it online paper? Where can I get it?
              • Such things are speficied by Servlet API documentation.
                If I remember correctly, dispatcher was not supported in Servlet 1.0.
                Vendor usually tell us which version of whcih API they support. How to implement API is a black box.

                I will try to get back the article title. It is on the Internet. Once I get some idea, I will let you know.