如何在两个或多个Servlet之间共享变量或对象?

时间:2022-04-29 21:13:16

I would like to know if there is some way to share a variable or an object between two or more Servlets, I mean some "standard" way. I suppose that this is not a good practice but is a easier way to build a prototype.

我想知道是否有一些方法可以在两个或更多Servlet之间共享变量或对象,我的意思是一些“标准”方式。我认为这不是一个好习惯,但是构建原型是一种更简单的方法。

I don't know if it depends on the technologies used, but I'll use Tomcat 5.5

我不知道它是否取决于所使用的技术,但我将使用Tomcat 5.5


I want to share a Vector of objects of a simple class (just public attributes, strings, ints, etc). My intention is to have a static data like in a DB, obviously it will be lost when the Tomcat is stopped. (it's just for Testing)

我想分享一个简单类的对象Vector(只是公共属性,字符串,整数等)。我的意图是拥有像DB一样的静态数据,显然它会在Tomcat停止时丢失。 (它只是用于测试)

6 个解决方案

#1


75  

I think what you're looking for here is request, session or application data.

我认为您在这里寻找的是请求,会话或应用程序数据。

In a servlet you can add an object as an attribute to the request object, session object or servlet context object:

在servlet中,您可以将对象作为属性添加到请求对象,会话对象或servlet上下文对象:

protected void doGet(HttpServletRequest request, HttpServletResponse response) {
    String shared = "shared";
    request.setAttribute("sharedId", shared); // add to request
    request.getSession().setAttribute("sharedId", shared); // add to session
    this.getServletConfig().getServletContext().setAttribute("sharedId", shared); // add to application context
    request.getRequestDispatcher("/URLofOtherServlet").forward(request, response);
}

If you put it in the request object it will be available to the servlet that is forwarded to until the request is finished:

如果将它放在请求对象中,它将可用于转发到的servlet,直到请求完成:

request.getAttribute("sharedId");

If you put it in the session it will be available to all the servlets going forward but the value will be tied to the user:

如果你将它放在会话中,它将可供所有servlet继续使用,但该值将与用户绑定:

request.getSession().getAttribute("sharedId");

Until the session expires based on inactivity from the user.

直到会话根据用户的不活动而到期。

Is reset by you:

由您重置:

request.getSession().invalidate();

Or one servlet removes it from scope:

或者一个servlet将其从作用域中删除:

request.getSession().removeAttribute("sharedId");

If you put it in the servlet context it will be available while the application is running:

如果将它放在servlet上下文中,它将在应用程序运行时可用:

this.getServletConfig().getServletContext().getAttribute("sharedId");

Until you remove it:

直到你删除它:

this.getServletConfig().getServletContext().removeAttribute("sharedId");

#2


8  

Put it in one of the 3 different scopes.

把它放在3个不同的范围之一。

request - lasts life of request

请求 - 持续请求

session - lasts life of user's session

session - 持续用户会话的生命

application - lasts until applciation is shut down

应用程序 - 持续到applciation关闭

You can access all of these scopes via the HttpServletRequest variable that is passed in to the methods that extend from the HttpServlet class

您可以通过HttpServletRequest变量访问所有这些范围,该变量传递给从HttpServlet类扩展的方法

#3


7  

Depends on the scope of the intended use of the data.

取决于数据的预期用途范围。

If the data is only used on a per-user basis, like user login info, page hit count, etc. use the session object (httpServletRequest.getSession().get/setAttribute(String [,Object]))

如果数据仅用于每个用户,如用户登录信息,页面命中计数等,请使用会话对象(httpServletRequest.getSession()。get / setAttribute(String [,Object]))

If it is the same data across multiple users (total web page hits, worker threads, etc) use the ServletContext attributes. servlet.getServletCongfig().getServletContext().get/setAttribute(String [,Object])). This will only work within the same war file/web applicaiton. Note that this data is not persisted across restarts either.

如果跨多个用户(总网页命中,工作线程等)的数据相同,则使用ServletContext属性。 servlet.getServletCongfig()。getServletContext()。get / setAttribute(String [,Object]))。这只能在同一个war文件/ web应用程序中运行。请注意,此数据也不会在重新启动时保留。

#4


2  

Another option, share data betwheen contexts...

另一种选择,在上下文之间共享数据......

share-data-between-servlets-on-tomcat

共享数据之间-的servlet-上的Tomcat

  <Context path="/myApp1" docBase="myApp1" crossContext="true"/>
  <Context path="/myApp2" docBase="myApp2" crossContext="true"/>

On myApp1:

在myApp1上:

  ServletContext sc = getServletContext();
  sc.setAttribute("attribute", "value");

On myApp2:

在myApp2上:

  ServletContext sc = getServletContext("/myApp1");
  String anwser = (String)sc.getAttribute("attribute");

#5


1  

Couldn't you just put the object in the HttpSession and then refer to it by its attribute name in each of the servlets?

难道你不能把对象放在HttpSession中,然后通过每个servlet中的属性名称引用它吗?

e.g:

例如:

getSession().setAttribute("thing", object);

...then in another servlet:

...然后在另一个servlet中:

Object obj = getSession.getAttribute("thing");

#6


0  

Here's how I do this with Jetty.

以下是我如何使用Jetty执行此操作。

https://*.com/a/46968645/1287091

https://*.com/a/46968645/1287091

Uses the server context, where a singleton is written to during startup of an embedded Jetty server and shared among all webapps for the life of the server. Can also be used to share objects/data between webapps assuming there is only one writer to the context - otherwise you need to be mindful of concurrency.

使用服务器上下文,其中在嵌入式Jetty服务器启动期间写入单例,并在服务器生命周期内在所有Web应用程序之间共享。也可以用来在webapps之间共享对象/数据,假设上下文只有一个编写器 - 否则你需要注意并发性。

#1


75  

I think what you're looking for here is request, session or application data.

我认为您在这里寻找的是请求,会话或应用程序数据。

In a servlet you can add an object as an attribute to the request object, session object or servlet context object:

在servlet中,您可以将对象作为属性添加到请求对象,会话对象或servlet上下文对象:

protected void doGet(HttpServletRequest request, HttpServletResponse response) {
    String shared = "shared";
    request.setAttribute("sharedId", shared); // add to request
    request.getSession().setAttribute("sharedId", shared); // add to session
    this.getServletConfig().getServletContext().setAttribute("sharedId", shared); // add to application context
    request.getRequestDispatcher("/URLofOtherServlet").forward(request, response);
}

If you put it in the request object it will be available to the servlet that is forwarded to until the request is finished:

如果将它放在请求对象中,它将可用于转发到的servlet,直到请求完成:

request.getAttribute("sharedId");

If you put it in the session it will be available to all the servlets going forward but the value will be tied to the user:

如果你将它放在会话中,它将可供所有servlet继续使用,但该值将与用户绑定:

request.getSession().getAttribute("sharedId");

Until the session expires based on inactivity from the user.

直到会话根据用户的不活动而到期。

Is reset by you:

由您重置:

request.getSession().invalidate();

Or one servlet removes it from scope:

或者一个servlet将其从作用域中删除:

request.getSession().removeAttribute("sharedId");

If you put it in the servlet context it will be available while the application is running:

如果将它放在servlet上下文中,它将在应用程序运行时可用:

this.getServletConfig().getServletContext().getAttribute("sharedId");

Until you remove it:

直到你删除它:

this.getServletConfig().getServletContext().removeAttribute("sharedId");

#2


8  

Put it in one of the 3 different scopes.

把它放在3个不同的范围之一。

request - lasts life of request

请求 - 持续请求

session - lasts life of user's session

session - 持续用户会话的生命

application - lasts until applciation is shut down

应用程序 - 持续到applciation关闭

You can access all of these scopes via the HttpServletRequest variable that is passed in to the methods that extend from the HttpServlet class

您可以通过HttpServletRequest变量访问所有这些范围,该变量传递给从HttpServlet类扩展的方法

#3


7  

Depends on the scope of the intended use of the data.

取决于数据的预期用途范围。

If the data is only used on a per-user basis, like user login info, page hit count, etc. use the session object (httpServletRequest.getSession().get/setAttribute(String [,Object]))

如果数据仅用于每个用户,如用户登录信息,页面命中计数等,请使用会话对象(httpServletRequest.getSession()。get / setAttribute(String [,Object]))

If it is the same data across multiple users (total web page hits, worker threads, etc) use the ServletContext attributes. servlet.getServletCongfig().getServletContext().get/setAttribute(String [,Object])). This will only work within the same war file/web applicaiton. Note that this data is not persisted across restarts either.

如果跨多个用户(总网页命中,工作线程等)的数据相同,则使用ServletContext属性。 servlet.getServletCongfig()。getServletContext()。get / setAttribute(String [,Object]))。这只能在同一个war文件/ web应用程序中运行。请注意,此数据也不会在重新启动时保留。

#4


2  

Another option, share data betwheen contexts...

另一种选择,在上下文之间共享数据......

share-data-between-servlets-on-tomcat

共享数据之间-的servlet-上的Tomcat

  <Context path="/myApp1" docBase="myApp1" crossContext="true"/>
  <Context path="/myApp2" docBase="myApp2" crossContext="true"/>

On myApp1:

在myApp1上:

  ServletContext sc = getServletContext();
  sc.setAttribute("attribute", "value");

On myApp2:

在myApp2上:

  ServletContext sc = getServletContext("/myApp1");
  String anwser = (String)sc.getAttribute("attribute");

#5


1  

Couldn't you just put the object in the HttpSession and then refer to it by its attribute name in each of the servlets?

难道你不能把对象放在HttpSession中,然后通过每个servlet中的属性名称引用它吗?

e.g:

例如:

getSession().setAttribute("thing", object);

...then in another servlet:

...然后在另一个servlet中:

Object obj = getSession.getAttribute("thing");

#6


0  

Here's how I do this with Jetty.

以下是我如何使用Jetty执行此操作。

https://*.com/a/46968645/1287091

https://*.com/a/46968645/1287091

Uses the server context, where a singleton is written to during startup of an embedded Jetty server and shared among all webapps for the life of the server. Can also be used to share objects/data between webapps assuming there is only one writer to the context - otherwise you need to be mindful of concurrency.

使用服务器上下文,其中在嵌入式Jetty服务器启动期间写入单例,并在服务器生命周期内在所有Web应用程序之间共享。也可以用来在webapps之间共享对象/数据,假设上下文只有一个编写器 - 否则你需要注意并发性。