简体   繁体   English

如何在jsp页面和servlet之间共享数据

[英]How do I share data between a jsp page and servlet

I have several .jsp pages and several servlets. 我有几个.jsp页面和几个servlet。

I need to save some information to a session variable. 我需要将一些信息保存到会话变量中。 In the jsp page I simply refer to 在jsp页面中,我只是指

session.get...()

or 要么

session.set...()

Without explicitly declaring an HttpSession object. 无需显式声明HttpSession对象。

But in the servlet the proper rules of programming apply and I have to create an object first. 但是在servlet中,适用正确的编程规则,因此我必须首先创建一个对象。

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. 我担心的是,我没有写入.jsp文件中引用的同一会话对象。

What do I do so that I can write to the same object in any jsp or servlet. 我该怎么做,以便可以在任何jsp或servlet中写入同一对象。

You need to obtain the HttpSession by calling the HttpServletRequest.getSession() method. 您需要通过调用HttpServletRequest.getSession()方法来获取HttpSession。

The HttpServletRequest is passed in to your doGet() method. HttpServletRequest传递给您的doGet()方法。 If no session was already in place for this request, then getSession() will create one. 如果没有针对该请求的会话,则getSession()将创建一个会话。 If a session is already in place and associated with this request, then getSession() will retrieve the existing one instead. 如果一个会话已经存在并与此请求相关联,则getSession()将改为检索现有会话。 If you use this standard mechanism, then you will automatically share the same session between your JSPs and servlets. 如果使用此标准机制,那么您将自动在JSP和Servlet之间共享同一会话。

You should never be trying to construct an HttpSession directly, as it is managed by the container. 您永远不要尝试直接构造HttpSession,因为它是由容器管理的。 Indeed, you cannot create a new one by calling new HttpSesion() because HttpSession is merely an interface and cannot be instantiated. 实际上,您不能通过调用new HttpSesion()创建一个新的对象,因为HttpSession只是一个接口,无法实例化。

To preprocess data, use Servlet's doGet() method. 要预处理数据,请使用Servlet的doGet()方法。

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). 要访问JSP中的数据,请使用EL(它将在页面,请求,会话和应用程序范围中按此顺序搜索具有给定名称的属性)。

<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. 要将数据从JSP发送到Servlet,通常使用由客户端控制的请求参数。 Most commonly HTML forms are been used for this. 最常见的HTML表单已用于此目的。 Alternatively you can also use Javascript to fire an asynchronous request to the server side. 另外,您也可以使用Javascript向服务器端触发异步请求。

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. 从客户端发起请求(通过单击链接,按钮,书签或在地址栏中输入URL)开始到服务器发送响应的最后一位为止,请求范围一直存在。 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() . 会话作用域从客户端第一次请求网页并且尚未创建HttpSession的那一刻起一直存在,直到HttpSession在一段未使用的时间(在web.xml中可配置)之后超时为止,或当代码使用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. 通常,您在其中存储应用程序范围的数据,例如静态下拉数据,DAO工厂,webapp配置数据等。

The request is accessible by HttpServletRequest argument in Servlet class. 该请求可由Servlet类中的HttpServletRequest参数访问。
The session is accessible by HttpServletRequest#getSession() in Servlet class. Servlet类中的HttpServletRequest#getSession()可访问该会话。
The application is accessible by inherited getServletContext() method in Servlet class. Servlet类中的继承的getServletContext()方法可访问该应用程序。
They all have a get/setAttribute() method. 它们都有一个get / setAttribute()方法。

To learn more about JSP/Servlet/EL I can recommend you the Sun Java EE 5 tutorial part II chapters 1-8 . 要了解有关JSP / Servlet / EL的更多信息,我可以向您推荐Sun Java EE 5教程第二部分第1-8章

Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM