Servlet Tutorial

Attribute Scopes

Attribute scopes:

Life time or visibility of a variable or attribute is known as scope. In servlet programming environment we have 3 types of attribute scopes, they are HttpServletRequest scope, ServletContext scope and Session scope.

HttpServletRequest object scope:

The request data is available in an object of HttpServletRequest or ServletRequest in the form of (key, value) form. Since we are using HttpServletRequest object only either in service or doGet or doPost methods, hence we cannot access this object in other methods of same servlet. Therefore the scope of HttpServletRequest object is limited to only service or doGet or doPost methods of a particular servlet.

If the group of Servlets is involving in processing a single client request then all the Servlets can use the HttpServletRequest object in their respective service or doGet or doPost methods.

Note:

Whenever we want to send a temporary result or local data to another servlet to be accessed in service or doGet or doPost methods, it is highly recommended to add the temporary data to the request object in the form of (key, value).

Methods:

public void setAttribute (String, Object); - 1 
public Object getAttribute (String); - 2 
public void removeAttribute (String); - 3 
public Enumeration getAttributeNames (); - 4

Method-1 is used for inserting or adding the data to HttpServletRequest object in the form of (key, value) pair. The parameter String represents key and the parameter Object represents value for the key. If the key value is not present in HttpServletRequest object then the data will be added as a new entry. If the key value already present in a HttpServletRequest object then key value remains same and whose value will be modified with the new value.

For example:

req.setAttribute ("v1", new Integer (10));
req.setAttribute ("v2", "abc");

Method-2 is used for getting or obtaining the value of value by passing value of key. If the value of key is not found in HttpServletRequest object then its return type (i.e., value of object) is null.

For example:

Object obj=req.getAttribute ("uname");

Method-3 is used for removing or deleting the (key, value) pair from HttpServletRequest object by passing value of key. If the value of key is not founded in the HttpServletRequest object then nothing is removed from HttpServletRequest object.

For example:

req.removeAttribute (uname);

Method-4 is used for obtaining the names of keys which are available in HttpServletRequest object.

For example:

get attribute names
Enumeration en=req.getAttributeNames ();
While (en.hasMoreElements ())
{
Object kname=en.nextElement (); 
String key= (String) kname;
Object val=req.getAttribute (key);
}

Implement the following diagram:

implement diagram

Answer:

deptdetails.html:

<html>
    <title>Retrieve department details</title>
    <body bgcolor="#EEE8AA">
    <center>
        <form name="deptdetails" action="./gdserv" method="post">
            <table border="1" bgcolor="#FFE4E1">
                <tr>
                    <th>Enter department number : </th>
                    <td><input type="text" name="deptdetails_no" value=""></td>
                </tr>
                <tr>
                    <td align="center"><input type="submit" name="deptdetails_details" value="Details"></td>
                    <td align="center"><input type="reset" name="deptdetails_reset" value="Reset"></td>
                </tr>
            </table>
        </form>
    </center>
    </body>
</html>

web.xml:

<web-app>
    <servlet>
        <servlet-name>abc</servlet-name>
        <servlet-class>GetDataServ</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>pqr</servlet-name>
        <servlet-class>Ddserv</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>abc</servlet-name>
        <url-pattern>/gdserv</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>pqr</servlet-name>
        <url-pattern>/ddserv</url-pattern>
    </servlet-mapping>
</web-app>

GetDataServ.java:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;

public class GetDataServ extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        res.setContentType("text/html");
        PrintWriter pw = res.getWriter();
        String dno1 = req.getParameter("deptdetails_no");
        int dno = Integer.parseInt(dno1);
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:Hanuman", "scott", "tiger");
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("select * from dept where deptno=" + dno);
            rs.next();
            String vdno = rs.getString(1);
            String vname = rs.getString(2);
            String vloc = rs.getString(3);
            req.setAttribute("sdno", vdno);
            req.setAttribute("sdname", vname);
            req.setAttribute("sdloc", vloc);
            ServletContext ctx = getServletContext();
            RequestDispatcher rd = ctx.getRequestDispatcher("/ddserv");
            rd.forward(req, res);
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        doGet(req, res);
    }
};

Ddserv.java:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class Ddserv extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        res.setContentType("text/html");
        PrintWriter pw = res.getWriter();
        String rdno = (String) req.getAttribute("sdno");
        String rdname = (String) req.getAttribute("sdname");
        String rdloc = (String) req.getAttribute("sdloc");
        pw.println("<h3>" + rdno + "</h3>");
        pw.println("<h3>" + rdname + "</h3>");
        pw.println("<h3>" + rdloc + "</h3>");
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        doGet(req, res);
    }
};