Application, Request, Session and Page Scopes in Servlets and JSPs

Similar to scope and lifetime of variables in Java as you have seen in blocks-and-methods-in-java, parameters and attributes in a Java EE web application also have scope and lifetime in the context of the web application. The scope of a parameter/attribute denotes the availability of that parameter/attribute for use. A web application serves multiple requests from clients when it is up and running. These requests can be from same client or different clients. We have seen from the servlet life cycle that a servlet’s service() method is called every time a request comes.

Different scopes are request, session and application. JSP has an additional scope called page scope.

We have not covered topics required for some of the scopes mentioned here; you can come back and look into those scopes for which topics are not covered until now, once those topics are covered.

 

Application or context scope

  • Context scope or application scope starts from the point where a web application is put into service (started) till it is removed from service (shutdown) or the web application is reloaded. Parameters/attributes within the application scope will be available to all requests and sessions. 

  • Application scope is denoted by javax.servlet.ServletContext interface. 

  • Application object is available in a JSP page as an implicit object called application

  • In a servlet, you can get application object by calling getServletContext() from within the servlets code directly (the servlet itself implements the ServletConfig interface that contains this method) or by explicitly calling getServletConfig().getServletContext().

  • The web container provides one ServletContext object per web application per jvm.

 

Request scope

  • Request scope start from the moment an HTTP request hits a servlet in our web container and end when the servlet is done with delivering the HTTP response.

  • With respect to the servlet life cycle, the request scope begins on entry to a servlet’s service() method and ends on the exit from that method. 

  • A ‘request’ scope parameter/attribute can be accessed from any of servlets or jsps that are part of serving one request. For example, you call one servlet/jsp, it then calls another servlet/jsp and so on, and finally the response is sent back to the client. 

  • Request scope is denoted by javax.servlet.http.HttpServletRequest interface.

  • Container passes the request object as an argument of type HttpServletRequest to Servlet's service method.

  • Request object is available in a JSP page as an implicit object called request. You can set value for an attribute in request object from a servlet and get it from a JSP within the same request using the implicit request object.

 

Session scope

  • A session scope starts when a client (e.g. browser window) establishes connection with our web application till the point where the browser window is closed.

  • Session scope spans across multiple requests from the same client. 

  • A noteable feature of tabbed browsing is that session is shared between the tabs and hence you can requests from other tabs too during a session without logging in again. For instance, you can load your gmail inbox in another tab without logging in again. This also means browsing an unknown site and a secure site from different tabs from the same browser can expose your secure session ID to malicious applications. So always open a new browser window when you want to do secure transactions, especially financial transactions. 

  • Session scope is denoted by javax.servlet.http.HttpSession interface. 

  • Session object is available in a JSP page as an implicit object called session

  • In a servlet, you can get Session object by calling request.getSession().

 

JSP page scope

  • The page scope restricts the scpoe and lifetime of attributes to the same page where it was created.

  • Page scope is denoted by javax.servlet.jsp.PageContext abstract class.

  • It is available in a JSP page as an implicit object called pageScope .

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)