request.setAttribute

In this section you will learn about the request.setAttribute. This section will describe you about the setAttribute method of request object.

request.setAttribute

In this section you will learn about the request.setAttribute. This section will describe you about the setAttribute method of request object.

request.setAttribute

request.setAttribute

In this section you will learn about the request.setAttribute.

This section will describe you about the setAttribute method of request object. setAttribute() method is used to set an attribute to a servlet request in a web application. Attributes set by this method can be reset between requests. This method can be used in Servlet and/or in JSP. However, request is the implicit object in JSP and it can be used directly to set an attribute using setAttribute method whereas, in Servlet you can use this method by creating a reference variable of ServletRequest or HttpServletRequest interface.

Syntax :

void setAttribute(java.lang.String name, java.lang.Object o)

This method has two parameters :

  • String name : By providing the value of this argument you can specify the name of attribute.
  • Object o : By providing the value of this argument you can specify the value of the named attribute. i.e. you can store the Object.

Example

Here I am giving example which will demonstrate you about how this method may be used or how you can set an attribute to a request object in web application. We will see the same example in two ways. In the first way I will use Servlet and in the second way I will use JSP. In the Sevlet example I will use the HttpServletRequest reference to set the attribute using setAttribute() method. HttpServletRequest is an interface and it extends the ServletRequest interface that's why the concept of inheritance we can use the setAttribute() method using reference of HttpServletRequest object. But, in the JSP example I will use the 'request' implicit object to set the attribute of a request.

request.setAttribute() using Servlet

ServletOne.java

package roseindia.net;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;

public class ServletOne extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
	{
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		request.setAttribute("name", "RoseIndia");
		RequestDispatcher rd = getServletContext().getRequestDispatcher("/servletTwo");
		rd.forward(request, response);
	}
}

ServletTwo.java

package roseindia.net;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;

public class ServletTwo extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Object name = request.getAttribute("name");
out.println("<html><head><title>request.setAttribute</title></head><body>");
out.println("<h2>request.setAttribute() Example</h2>");
out.println("Attribute Value : " +name);
out.println("</body></html>");
}
}

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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>setAttribute</display-name>
<servlet>
<servlet-name>ServletOne</servlet-name>
<servlet-class>roseindia.net.ServletOne</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletOne</servlet-name>
<url-pattern>/servletOne</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>ServletTwo</servlet-name>
<servlet-class>roseindia.net.ServletTwo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletTwo</servlet-name>
<url-pattern>/servletTwo</url-pattern>
</servlet-mapping>
</web-app>

request.setAttribute() using JSP

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Index Page</title>
</head>
<body>
<%
request.setAttribute("name", "RoseIndia");
%>
<jsp:forward page="request.jsp"/>
</body>
</html>

request.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>request.setAttribute</title>
</head>
<body>
<h2>request.setAttribute() Example</h2>
<%
Object name = request.getAttribute("name");
out.println("Attribute Value : " +name);
%>
</body>
</html>

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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>setAttribute</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

Output :

When you will run the both example the output will be same as below :

Download Source Code