Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think what you're looking for here is request, session or application data.</p> <p>In a servlet you can add an object as an attribute to the request object, session object or servlet context object:</p> <pre><code>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); } </code></pre> <p>If you put it in the request object it will be available to the servlet that is forwarded to until the request is finished:</p> <pre><code>request.getAttribute("sharedId"); </code></pre> <p>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:</p> <pre><code>request.getSession().getAttribute("sharedId"); </code></pre> <p>Until the session expires based on inactivity from the user.</p> <p>Is reset by you:</p> <pre><code>request.getSession().invalidate(); </code></pre> <p>Or one servlet removes it from scope:</p> <pre><code>request.getSession().removeAttribute("sharedId"); </code></pre> <p>If you put it in the servlet context it will be available while the application is running:</p> <pre><code>this.getServletConfig().getServletContext().getAttribute("sharedId"); </code></pre> <p>Until you remove it:</p> <pre><code>this.getServletConfig().getServletContext().removeAttribute("sharedId"); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload