How can JSP page get correct session object?

marcow (UserMarco)
本文发表在 rolia.net/zh 相约加拿大网上社区枫下论坛
I wrote a java servlet and I'd like to forward the request to an JSP page. Just like below:


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);

RequestDispatcher dispatcher
= getServletContext().getRequestDispatcher("myJsp.jsp");

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.

更多精彩文章及讨论,请光临枫下论坛. 网址: rolia.net/zh
2000-10-29 -04:00
This post has been archived. It cannot be replied.
Page address has been copied. To share, click to copy page address.
Share Online by QR Code

Back To Topic: How can JSP page get correct session object?

Back To Forum: HOME枫下论坛枫下论坛主坛工作学习IT技术讨论