Introduction
- Attributes is an object that allows to store the data in inter Servlet communication.
- It is used to share the information in web application from one servlet to another.
- The attributes have scope and lifetime in web application.
- The scope of attribute indicates the availability of the parameter.
There are three types of attribute scope:
I) request
II) session
III) application
Request Scope
- The request scope is used when processing the results of a submitted form.
- This scope is denoted by javax.servlet.http.HttpServletRequest interface.
- It begins with the service( ) method and ends on the exit from that method.
Example
Request.setAttribute("username", user); //insert
Object obj = request.getAttribute("username"); //retrieve
Request.removeAttribute("username"); //remove
Session Scope
- A session is created by the web container when user visits the web page. After closing the browser window the session expires.
- The data in session scope is only available to a single client and is not shared.
Example
request.getSession( ).setAttribute("username", user);
Object obj = request.getSession( ).getAttribute("username");
request.getSession( ).removeAttribute("username");
Application Scope
- The application scope lives as long as the web application is deployed.
- A web application is collection of web pages, scripts, servlet etc and each web application has its own scope and accessibility.
Example
this.getServletContext( ).setAttribute("username", user);
Object obj = this.getServletContext().getAttribute("username");
this.getServletContext( ).removeAttribute(username");