本文发表在 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
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