In this tutorial you will learn about the Servlet Attribute and its application with practical example.
Servlet Attribute
An attribute in servlet is an object that is used to share information in a web application. Attributes is also allow user to share information among the Servlet themselves, so that we can reuse the same object again and again. It can be SET and GET one of the following scope:
Request
Session
Application
Servlet Attribute Methods
public void setAttibute(String name, Object object):- This method is used to set the attribute.
public void getAttribut(String name):- This method is used to get the value of given attribute.
public void removeAttribute(String name):- This method is used to remove the attribute with the given name of attribute.
Example:-
Let’s understand this thing by an example, we have to create Two servlet file. From first We will set the attribute and in second we will get that attribute by it’s parameter.
ServletContextAtt1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ import java.io.PrintWriter; import javax.servlet.ServletContext; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletContextAtt1 extends HttpServlet{ public void doGet(HttpServletRequest req,HttpServletResponse res) { try{ res.setContentType("text/html"); PrintWriter out=res.getWriter(); ServletContext context=getServletContext(); context.setAttribute("websiteName","w3adda.com"); out.println("Welcome to W3adda.com"); out.close(); }catch(Exception e){System.out.println(e);} }} |
ServletContextAtt2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ import java.io.IOException; import java.io.PrintWriter; 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 ServletContextAtt2 extends HttpServlet{ public void doGet(HttpServletRequest req,HttpServletResponse res) { try{ res.setContentType("text/html"); PrintWriter out=res.getWriter(); ServletContext context=getServletContext(); String n=(String)context.getAttribute("websiteName"); out.println("Welcome to our website : "+n); out.close(); }catch(Exception e){System.out.println(e);} }} |
Output:-
Output of above java files will be like two images.
1. Output of first servlet will be like below image. Here we have set the attribute.
2. Output of the second servlet will be like below image.In which we used the getAttribute() method.