简体   繁体   English

意外的Servlet缓存

[英]Unexpected Servlet caching

I have a Tomcat7 web App consisting of an HTML file with form, as well as a Servlet and a Java bean. 我有一个Tomcat7 Web应用程序,该应用程序由带有表单的HTML文件,Servlet和Java Bean组成。 The HTML form calls the Servlet, which takes the request parameters, does some preparations, creates an instance of the bean and calls it's String doSearch(String arg1, int arg2) method. HTML表单调用Servlet,该Servlet接受请求参数,进行一些准备,创建bean的实例并调用它的String doSearch(String arg1,int arg2)方法。 Each time the Servlet is called, it sets the bean to null and creates a new Instance. 每次调用Servlet时,它将Bean设置为null并创建一个新的Instance。

The problem is that in each session the former results (created by the bean) stay visible (which should not appear). 问题在于,在每个会话中,以前的结果(由Bean创建)都保持可见(不应显示)。 I don't use any static variables or classes. 我不使用任何静态变量或类。

When I run the code as Java Program it works as expected. 当我将代码作为Java程序运行时,它可以按预期工作。 The only way to get rid of the old results for now is restarting Tomcat 暂时摆脱旧结果的唯一方法是重新启动Tomcat

Any help is greatly appreciated. 任何帮助是极大的赞赏。

Thanks. 谢谢。

The war File can be downloaded from https://drive.google.com/open?id=0B5P_61PgIb6gc0VfRC1sbzdhUjQ 战争文件可以从https://drive.google.com/open?id=0B5P_61PgIb6gc0VfRC1sbzdhUjQ下载

Odds are you are storing the information, but the wrong location. 您存储的信息很奇怪,但是位置错误。

Java servlets support four storage areas (Application, Session, Request, and Page). Java servlet支持四个存储区域(应用程序,会话,请求和页面)。

The application will hold information until you explicitly remove it or restart the application. 该应用程序将保留信息,直到您明确删除它或重新启动该应用程序为止。

The Session will hold information until you explicitly remove it, you restart the application, or a session timeout cleans the information from the application. 会话将保留信息,直到您明确删除它,重新启动应用程序或会话超时清除应用程序中的信息为止。 Sessions are not really persistent things (http is connectionless), but are simulated by associating a series of similar requests (same web browser, originating IP address, originating operating system, etc.). 会话并不是真正的持久性对象(http是无连接的),而是通过关联一系列类似的请求(相同的Web浏览器,原始IP地址,原始操作系统等)来模拟的。 Future connections with a full set of matching criteria will be able to access the information stored in the Session created with the matching criteria. 具有全套匹配条件的将来的连接将能够访问使用匹配条件创建的会话中存储的信息。 This permits Tomcat to pretend to have a persistent conversation despite http not permitting a real continuous connection. 尽管http不允许真正的连续连接,这仍允许Tomcat假装具有持久性对话。

The Request will hold information for the duration of a single request. 该请求将在单个请求期间保存信息。 This is useful if you partition your web site's logic into page-less operations (sometimes called commands or actions) and then attach the information to the Request to be pulled out by formatting logic (like a jsp presentation layer). 如果将网站的逻辑划分为无页面的操作(有时称为命令或动作),然后将信息附加到要通过格式化逻辑提取的请求(如jsp表示层),则这很有用。

The Page will hold information for the duration of a single page. 该页面将在单个页面的持续时间内保存信息。 For example, if one were in a specific web page (as noted by a specific URL) and due to some logic the request was redirected to a different page, the information would be lost before the other page could access it. 例如,如果一个人位于特定的网页中(如特定的URL所示),并且由于某种逻辑,请求被重定向到另一个页面,则该信息将在另一个页面访问之前丢失。

I imagine that you've either stored your information in the Application or the Session, and have been testing with a single web browser. 我想您已经将信息存储在应用程序或会话中,并且正在使用单个Web浏览器进行测试。 If you connect with a different web browser and you don't see the information created with a prior connection on a different web browser, you are likely storing the information in the Session. 如果您使用其他Web浏览器连接,而在其他Web浏览器上看不到先前连接创建的信息,则可能会将信息存储在Session中。 Odds are you wish to store this information in the Request. 您可能希望将此信息存储在请求中。

To store something in a request, get the ServletRequest object (or a subclass of it) and use the .setAttribute(String name, Object o) method. 要将某些内容存储在请求中,请获取ServletRequest对象(或其子类),然后使用.setAttribute(String name, Object o)方法。 To access it at a later time, use the .getAttribute(String name) method which will return the stored object. 要在以后访问它,请使用.getAttribute(String name)方法,该方法将返回存储的对象。

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

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