Servlet setAttribute & getAttribute method example

In this tutorial you will learn about how to use the setAttribute() & getAttribute() method in servlet.

Servlet setAttribute & getAttribute method example

In this tutorial you will learn about how to use the setAttribute() & getAttribute() method in servlet.

In the example given below I want to carry some information from one servlet to another servlet. For this solution first I created two servlet in one of which I used the setAttribute() method to set the value which I have to carry on an another servlet. In the other servlet I have used the getAttribute() method which will fetch the value set in the first servlet using setAttribute() method. These methods can be used with ServletConfig, HttpSession, ServletRequest etc. references.

Syntax :

setAttribute("String str", Object obj);

Value in setAttribute() method is set in the key-value pair as the value is set in Map. The first argument is a string which act as a 'key' value and the second argument is an Object type variable which act as 'value' of that 'key'.

getAttribute("String str");

getAttribute() method fetches the value set in setAttribute() method. Argument of this method is act as a 'key' which contains the value and the variable should be the same as of the first argument of setAttribute() method.

Example :

loginSetAttribute.html

<!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>Login Page</title>
</head>
<body>
<form method="get" action="SetAttributeExample">
<p>Give the following details</p>
Name <input type="text" name="name"/><br>
<input type="submit" value="submit"/>
</form>
</body>
</html>

SetAttributeExample.java

package roseindia.setAttribute;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/SetAttributeExample")
public class SetAttributeExample extends HttpServlet {
private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String name= request.getParameter("name");
request.setAttribute("Name", name);
ServletContext context= getServletContext();
RequestDispatcher rd= context.getRequestDispatcher("/GetAttributeExample");
rd.forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doGet(request, response);
}
}

GetAttributeExample.java

package roseindia.setAttribute;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/GetAttributeExample")
public class GetAttributeExample extends HttpServlet {
private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out= response.getWriter();
if(request.getAttribute("Name").equals("bipul"))
{
out.println("Valid user");
}
else
{
out.println("Invalid user");
}
}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doGet(request, response);
}

}

Output :

When you will execute the above example you will get the output as :

When you will enter the value in the textfield "bipul" because I have manually fix the value the output will be as :

After that when you will click on submit button you will get the output as :

If you will enter the value in textfield other than the "bipul" you will get the output as :

Download Source Code

Advertisements

Share on Google+Share on Google+

Servlet setAttribute & getAttribute method example

Posted on: December 21, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Servlet setAttribute & getAttribute method example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Shamira
September 6, 2012
Servlet attributes forward to Java Classes and JSP

Does this process work the same when trying to call JSP's. I noticed you used PrintWriter in this example. How do you send A login JSP to another JSP. Instead of text/html

Ads

 

Ads