开发者

How do I share data between a jsp page and servlet

I have several .jsp pages and several servlets.

I need to save some information to a session variable. In the jsp page I simply refer to

session.get...()

or

session.set...(开发者_Python百科)

Without explicitly declaring an HttpSession object.

But in the servlet the proper rules of programming apply and I have to create an object first.

My concern is if I create an object like this

HttpSession session = new HttpSession();

and then write to it using something like this

session.setAttribute("files",fileList);

my concern is that I am not writing to the same session object that was being referenced in the .jsp file.

What do I do so that I can write to the same object in any jsp or servlet.


You need to obtain the HttpSession by calling the HttpServletRequest.getSession() method.

The HttpServletRequest is passed in to your doGet() method. If no session was already in place for this request, then getSession() will create one. If a session is already in place and associated with this request, then getSession() will retrieve the existing one instead. If you use this standard mechanism, then you will automatically share the same session between your JSPs and servlets.

You should never be trying to construct an HttpSession directly, as it is managed by the container. Indeed, you cannot create a new one by calling new HttpSesion() because HttpSession is merely an interface and cannot be instantiated.


To preprocess data, use Servlet's doGet() method.

Data data = dataDAO.load();
request.setAttribute("data", data);
request.getRequestDispatcher("page.jsp").forward(request, response);

To access data in JSP, use EL (which searches in page, request, session and application scope in this order for attributes with the given name).

<br>Plain object: ${data}
<br>A property: ${data.property}
<br>Explicitly search in request scope: ${requestScope.data}

To send data from JSP to servlet you normally use request parameters which are controlled by the client side. Most commonly HTML forms are been used for this. Alternatively you can also use Javascript to fire an asynchronous request to the server side.

Anything in a certain scope is accessible for anything which lives in the same scope. The request scope lives from the moment that the client initiated a request (by clicking a link, button, bookmark or by entering the URL in address bar) until the moment that the server has sent the last bit of the response. You normally store request specific data in there like form data. The session scope lives from the moment that the client requested the webpage for the first time and the HttpSession hasn't been created yet until the time that the HttpSession times out after not been used for a time which is configureable as in web.xml, or when the code explicitly times out it using HttpSession#invalidate(). You normally store user-specific data in there, like the logged in user and user preferences and so on. The application scope lives from the moment that the server starts up until the moment that the server shutdowns (or restarts). You normally store applicationwide data in there, like static dropdown data, the DAO factory, webapp configuration data, etcetera.

The request is accessible by HttpServletRequest argument in Servlet class.
The session is accessible by HttpServletRequest#getSession() in Servlet class.
The application is accessible by inherited getServletContext() method in Servlet class.
They all have a get/setAttribute() method.

To learn more about JSP/Servlet/EL I can recommend you the Sun Java EE 5 tutorial part II chapters 1-8.

Good luck.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜