Setting Attributes into request object

Setting Attributes into request object


Posted in : Servlet Posted on : November 20, 2010 at 4:21 PM Comments : [ 0 ]

In this tutorial you will learn how to set and get attribute in request object

Setting And Getting Attributes Into Request

Request attributes are bounded to specific request and are forwarded to servlet to servlet. HttpServletRequest interface provides method to manage the request. You can add any number of attributes to the request and forward this request to the other Servlet/JSP in the same application. An example on request attribute is given below.

login.html

<HTML>
<HEAD>
<TITLE> Login Page </TITLE>
</HEAD>
<BODY>
<BODY bgcolor=lightblue>
<form name=login method="get" 
action="http://localhost:8080/requestattribute/attribute">
<center>
<h1>Enter your Name and Pasword </h1><br>
<table border=1>
<tr><td>Enter Your Name :</td>
<td><input type="text" name="userName" value=""></td>
<tr><td>Enter Your Password :</td>
<td><input type="password" name="password" value=""></td>
<tr><td align=center><input type="submit" name="Submit" value="Submit" ></td>
<td align=center><input type="reset" name="reset" value="Refresh"></td>
<table>
</center>
</form>
</BODY> 
</HTML>
RequestAttributeExample.java
package roseindia.net;

import java.awt.List;
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;

public class RequestAttributeExample extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
PrintWriter out = response.getWriter();
response.setContentType("text/html");
// getting Request parameters
String name = request.getParameter("userName");
String password = request.getParameter("password");
// Getting Request Info Path
request.setAttribute("loggedUserName", name);
request.setAttribute("loggedUserPassword", password);
request.setAttribute("requestURI", request.getRequestURL());
// forwarding Request
ServletContext context = getServletContext();
RequestDispatcher requestDispatcher = context
.getRequestDispatcher("/display");
requestDispatcher.forward(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
doGet(request, response);
}
}
RequestAttributeDisplay.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;

public class RequestAttributeDisplay extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
PrintWriter out = response.getWriter();
response.setContentType("text/html");
out.println("<body bgcolor=purple>");
out.println("<center><h2>");
out.println(" <TABLE border=1><TR><TD>");
out.println("<br>Logged User Name -</td><td>"
+ request.getAttribute("loggedUserName") + "</td>");
out.println("<br><TR><TD>Logged User Password -</TD><TD>"
+ request.getAttribute("loggedUserPassword") + "</TD></TR>");
out.println("<br><TR><TD>Request URl -</TD><TD>"
+ request.getAttribute("requestURI") + "</TD></TR>");
out.println("</h2></center>");
out.println("</TABLE></body>");
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
doGet(request, response);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="
http://java.sun.com/xml/ns/j2ee" xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>CreateSession</display-name>
<!-- Servlet -->

<servlet>
<servlet-name>RequestAttributeExample</servlet-name>
<servlet-class>roseindia.net.RequestAttributeExample</servlet-class>
</servlet>

<servlet>
<servlet-name>RequestAttributeDisplay</servlet-name>
<servlet-class>roseindia.net.RequestAttributeDisplay</servlet-class>
</servlet>

<!-- Servlet Mapping-->

<servlet-mapping>
<servlet-name>RequestAttributeExample</servlet-name>
<url-pattern>/attribute</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>RequestAttributeDisplay</servlet-name>
<url-pattern>/display</url-pattern>
</servlet-mapping>

<!-- Setting Session Time Out-->

</web-app>
When you run this application it will display message as shown below:

 


Download this example code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics