Servlets - SPLessons

Servlet Attribute

Chapter 17

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Servlet Attribute

Servlet Attribute

shape Description

Servlet Attribute, Attribute is a special logical name that can hold a java object as a value. Attribute provides more visibility and accessibility to the data of the application while comparing to the variable declared in servlet program. Attribute allows to store the only object. If any value is given to an Attribute then Attribute converts into wrapper class object and will be stored as Attribute values.

Request Attribute

shape Description

Servlet Attribute, Request Attribute is created in one servlet program of servlet chaining that first request of servlet1 is visible in other servlet programs of same servlet chaining because all servlet chainings will use same request and response object.

shape Conceptual
figure

In the above diagram, the request attribute created in the servlet1 program is visible and also accessible in servlet2 and servlet 3 but not in servlet4 because, servlet 1, 2, 3 are used in same request and response object because, they three in servlet chaining and servlet4 is not in the servlet chaining. Following is the syntax to Create the Request Attribute.

request.setAttribute("attribute_name", "attribute_value");

The setAttribute() can create new request attribute or modify the existing request attribute value. Following is the syntax to read the values from the Request Attribute.

datatype attribute_value = request.getAttribute("attribute_name");

Following is the syntax to delete/remove Request Attribute.

request.removeAttribute("attribute_name");

Session Attribute

shape Description

Servlet Attribute, Session means certain interval of time same as the Session Attribute which allocates the memory in the form of the HttpSession object for a certain interval of time on the server. Session default time interval is 30 minutes. After completion of 30 minutes, the session closes. Session Attribute is created one per browser window. So the session request is visible and accessible in all web resource programs of a web application. If session attribute is created, one must get a request from the browser window.

shape Conceptual
figure

Session Attribute is created in servlet1 program by getting request from browser window which is visible and accessible in all servlets which are in web application. But all servlets must get the same request which got from browser window. Following is the syntax to create/access HttpSession object for browser window on servlet.

HttpSession sessionObject = request.getSession();
sessionObject.setAttribute("attribute_name", "attribute_value");

The getSession() method create the locate HttpSession object. This request.getSession() method check the availability of session object on the server. If available, it uses same session object. Otherwise, it can create new session object (HttpSession) for browser window on the server. Following is the syntax to Read Session Attribute Values.

datatype attribute_value (datatype) session.getAttribute("attribute_name");

Following is the syntax to remove Session Attribute.

sessionObject.removeAttribute("attribute_name");

ServletContext Attribute

shape Description

Servlet Attribute, ServletContext Attribute allocates memory in ServletContext object. This ServletContext object is one per web application and global memory for all servlets which are in the web application. So ServletContext Attribute is visible and accessible in all web resource program of web application.

shape Conceptual
figure

shape Example

web.xml

<web-app>
  <servlet>
    <servlet-name>DemoServletAttribute</servlet-name>
    <servlet-class>servletattribute.DemoServletAttribute</servlet-class>
  </servlet>
     <servlet>
    <servlet-name>ForwardRequest</servlet-name>
   <servlet-class>servletattribute.ForwardAttributes</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>DemoServletAttribute</servlet-name>
    <url-pattern>/welcome</url-pattern>
  </servlet-mapping>
   
  <servlet-mapping>
    <servlet-name>ForwardRequest</servlet-name>
    <url-pattern>/servlet2</url-pattern>
 </servlet-mapping>
    
</web-app>

Make sure that while giving name of class name also give package name, here the developer has taken two classes.

DemoServletAttribute.java

package servletattribute;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
 
public class DemoServletAttribute extends HttpServlet {
 
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
 
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
         
        request.setAttribute("name", "SPlesson");//Create request attribute
         
        //Create session attribute
        HttpSession sessionObject = request.getSession();//Create session object
         
        sessionObject.setAttribute("tutorial", "yes");//create set attribute
         
        //Create ServletContext attribute
        ServletContext servletContextObject = getServletContext();//create sessionContext object
        servletContextObject.setAttribute("contact to", "SPlesson@info.com");
         
        //forward request to servlet2 program
        RequestDispatcher rd = request.getRequestDispatcher("servlet2");
        rd.forward(request, response);
         
        out.flush();
        out.close();
    }
 
}

forwardAttributes.java

package servletattribute;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
public class ForwardAttributes extends HttpServlet {
 
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //general settings
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
         
        //reading request attribute value
        out.println("servlet2 request attribute value::"+request.getAttribute("name")+"");
         
        //reading session attribute value
        HttpSession sessionObject = request.getSession();
        out.println("servlet session attribute value::"+sessionObject.getAttribute("tutorial")+"");
         
        //reading servlet context value
         
        ServletContext contextObject = getServletContext();
        out.println("servlet session attribute value::"+contextObject.getAttribute("contact to")+"");
         
        out.flush();
        out.close();
    }
 
}

Here the purpose of setContentType(“text/html”) is that, It basically tells the client what content type it is so that it knows what to do with it. Where the developer created an object to read the request object, session object, context object to read the values as follows.

HttpSession sessionObject = request.getSession();
ServletContext contextObject = getServletContext();

Output

Result will be generated as follows.

Difference Between Session Attributes And ServletContext Attributes

ServletContext
A ServletContext attribute is a protest bound into a context through ServletContext.setAttribute() technique and which is accessible to all servlets in that unique circumstance, or to different settings by means of the getContext() strategy. By definition a setting property exists locally in the JVM where they were characterized. In this way, these are inaccessible on disseminated applications.

Session Attributes
Session attributes are bound to a session, as an intend to give state to an arrangement of related HTTP requests. Session attributes are accessible ONLY to those servlets which join the session. These are likewise inaccessible to various JVMs in dispersed situations. Items can be advised when bound/unbound to the session actualizing the HttpSessionBindingListener interface.

Summary

shape Key Points

  • Attributes are utilized to fetch the data from servlet to servlet.
  • PrintWriter is utilized to display character information of an output.
  • The doGet() method is utilized to send parameters to an URL along with the header information.
  • The doPost() method is utilized to send huge information to an URL.