How can I share a variable or object between two or more Servlets?

后端 未结 6 1178
星月不相逢
星月不相逢 2020-11-28 21:32

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 prac

相关标签:
6条回答
  • 2020-11-28 21:41

    Another option, share data betwheen contexts...

    share-data-between-servlets-on-tomcat

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

    On myApp1:

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

    On myApp2:

      ServletContext sc = getServletContext("/myApp1");
      String anwser = (String)sc.getAttribute("attribute");
    
    0 讨论(0)
  • 2020-11-28 21:44

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

    e.g:

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

    ...then in another servlet:

    Object obj = getSession.getAttribute("thing");
    
    0 讨论(0)
  • 2020-11-28 21:49

    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]))

    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.

    0 讨论(0)
  • 2020-11-28 21:53

    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:

    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:

    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:

    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:

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

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

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

    Until you remove it:

    this.getServletConfig().getServletContext().removeAttribute("sharedId");
    
    0 讨论(0)
  • 2020-11-28 22:00

    Here's how I do this with Jetty.

    https://stackoverflow.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.

    0 讨论(0)
  • 2020-11-28 22:07

    Put it in one of the 3 different scopes.

    request - lasts life of request

    session - lasts life of user's session

    application - lasts until applciation is shut down

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

    0 讨论(0)
提交回复
热议问题