In this tutorial, we will discuss how getAttribute() method works.
In this tutorial, we will discuss how getAttribute() method works.In this tutorial, we will discuss how getAttribute() method works.
getAttribute(String name) :
This method is used for fetching value which you set in setAttribute() method. It returns the value of the named attribute in form of object. If no attribute found of specified name then it will return null.
Parameters:
name - It is of String type which represent the the name of the attribute
Returns: an object that containing value of the attribute or null in case specified attribute doesn't exist.
Example : In this example we are showing how to use getAttribute() method.
SetAttributeExample.java - This is one servlet in which we are setting attribute name and forward to another servlet.
package net.roseindia; import java.io.IOException; import javax.servlet.*; import javax.servlet.http.*; public class SetAttributeExample extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = "Roseindia"; request.setAttribute("Name", name); ServletContext context = getServletContext(); RequestDispatcher rd = context .getRequestDispatcher("/GetAttributeExample"); rd.forward(request, response); } }
GetAttributeExample.java - In this servlet we are displaying the value by fetching getAttribute().
package net.roseindia; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.*; import javax.servlet.http.*; public class GetAttributeExample extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String name = (String) request.getAttribute("Name"); if (name.equals("Roseindia")) { out.println("<b> Valid data : " + name + "</b>"); } else { out.println("Invalid"); } } }
web.xml -
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>ServletExample</display-name> <servlet> <display-name>SetAttributeExample</display-name> <servlet-name>SetAttributeExample</servlet-name> <servlet-class>net.roseindia.SetAttributeExample</servlet-class> </servlet> <servlet-mapping> <servlet-name>SetAttributeExample</servlet-name> <url-pattern>/SetAttributeExample</url-pattern> </servlet-mapping> <servlet> <display-name>GetAttributeExample</display-name> <servlet-name>GetAttributeExample</servlet-name> <servlet-class>net.roseindia.GetAttributeExample</servlet-class> </servlet> <servlet-mapping> <servlet-name>GetAttributeExample</servlet-name> <url-pattern>/GetAttributeExample</url-pattern> </servlet-mapping> </web-app>
Output :