3

what's the difference between get/setAttribute() when you call them from request and from getServletContext(). I noticed that you need

RequestDispatcher rd = request.getRequestDispatcher("/view.jsp");
rd.forward(request, response); 

for the request to work, but you just need to navigate to another jsp or servlet in the application to use getServletContext().getAttribute().

But i don't understand what is going on behind.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Evgeni Dimitrov
  • 21,976
  • 33
  • 120
  • 145
  • `but you just need to navigate to another jsp or servlet in the application to use getServletContext().getAttribute().` explain this more – jmj Mar 08 '11 at 13:06
  • @Evgeni : For Servlet 2.5, would the ServletContext.getInitParameter() return the same attribute set by ServletContext.setAttribute() ? – Gaurav Aug 22 '19 at 09:28

3 Answers3

10

The request.setAttribute() sets an attribute in the request scope and is thus only available within the same request/response cycle. The servletContext.setAttribute() sets an attribute in the application scope and is thus shared among all other requests/sessions. You don't want to do this when it concerns request-specific data, otherwise visitor Y would be able to see data of visitor X.

If you want some attribute to survive a redirect by response.sendRedirect() then the request scope is not suitable since a redirect basically instructs the client (webbrowser) to create a brand new HTTP request. You need to put the data in the session scope by session.setAttribute() rather than in the application scope (and preferably remove it in the subsequent request if it doen't need to be persistent during the whole session).

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • For Servlet 2.5, would the ServletContext.getInitParameter() return the same attribute set by ServletContext.setAttribute() ? – Gaurav Aug 22 '19 at 09:27
  • 1
    Absolutely not. The one returns a context init parameter and the other returns a context attribute. See also their javadocs. – BalusC Aug 22 '19 at 10:02
  • How would one set the context parameter programmatically in servlet 2.5? – Gaurav Aug 22 '19 at 13:26
  • Just upgrade to Servlet 3.0 or newer. It was released a decade ago already. – BalusC Aug 22 '19 at 15:03
3

The servlet context has a global "application" namespace which is maintained throughout the deployment of the application.

The request has a per-request namespace which is maintained for the lifetime of a single request.

So use servletContext.setAttribute() to store things that need to be global in scope, and shared between different requests (and therefore must be threadsafe), and request.setAttribute() to store things relating only to the current request (usually no need to worry about thread safety since a request is usually served by a single thread).

araqnid
  • 127,052
  • 24
  • 157
  • 134
  • For Servlet 2.5, would the ServletContext.getInitParameter() return the same attribute set by ServletContext.setAttribute() ? – Gaurav Aug 22 '19 at 09:27
  • 1
    @gaurav no, init parameters and context attributes are separate things (init parameters are specified in web.xml and are strings, context attributes are set by the application while it is running) – araqnid Sep 19 '19 at 08:58
0

First, you need to know some scopes. There are session scope, request scope and application scope, page scope. Attribute in different scope will be store in different length of time.

Just like your example, attribute stored in ServletContext is in application scope. In the server application just like tomcat, when the server start, there will be only one instance of ServletContext in memory for each web application. The instance will last untill the server stopped. So the attribute stored in this instance can be use all the life time of the web application. But the instance of request will be build when a http request from client, after server send the response message, it will not exist any longer. So attribute in request can only use in the life of the request.

I was just learning java web. If I have some wrong above, I'd like to have a criticize.

leo
  • 361
  • 1
  • 5
  • 11