Tuesday, 29 September 2015

Attribute in Servlet

Attribute in Servlet

An attribute in servlet is an object that can be set, get or removed from one of the following scopes:
  1. request scope
  2. session scope
  3. application scope
The servlet programmer can pass informations from one servlet to another using attributes. It is just like passing object from one class to another so that we can reuse the same object again and again.

Attribute specific methods of ServletRequest, HttpSession and ServletContext interface

There are following 4 attribute specific methods. They are as follows:
  1. public void setAttribute(String name,Object object):sets the given object in the application scope.
  2. public Object getAttribute(String name):Returns the attribute for the specified name.
  3. public Enumeration getInitParameterNames():Returns the names of the context's initialization parameters as an Enumeration of String objects.
  4. public void removeAttribute(String name):Removes the attribute with the given name from the servlet context.

Example of ServletContext to set and get attribute

In this example, we are setting the attribute in the application scope and getting that value from another servlet.

DemoServlet1.java

  1. import java.io.*;  
  2. import javax.servlet.*;  
  3. import javax.servlet.http.*;  
  4.   
  5.   
  6. public class DemoServlet1 extends HttpServlet{  
  7. public void doGet(HttpServletRequest req,HttpServletResponse res)  
  8. {  
  9. try{  
  10.   
  11. res.setContentType("text/html");  
  12. PrintWriter out=res.getWriter();  
  13.   
  14. ServletContext context=getServletContext();  
  15. context.setAttribute("company","IBM");  
  16.   
  17. out.println("Welcome to first servlet");  
  18. out.println("<a href='servlet2'>visit</a>");  
  19. out.close();  
  20.   
  21. }catch(Exception e){out.println(e);}  
  22.   
  23. }}  

DemoServlet2.java

  1. import java.io.*;  
  2. import javax.servlet.*;  
  3. import javax.servlet.http.*;  
  4.   
  5.   
  6. public class DemoServlet2 extends HttpServlet{  
  7. public void doGet(HttpServletRequest req,HttpServletResponse res)  
  8. {  
  9. try{  
  10.   
  11. res.setContentType("text/html");  
  12. PrintWriter out=res.getWriter();  
  13.   
  14. ServletContext context=getServletContext();  
  15. String n=(String)context.getAttribute("company");  
  16.   
  17. out.println("Welcome to "+n);  
  18. out.close();  
  19.   
  20. }catch(Exception e){out.println(e);}  
  21.   
  22. }}  

web.xml


  1. <web-app>  
  2.   
  3. <servlet>  
  4. <servlet-name>s1</servlet-name>  
  5. <servlet-class>DemoServlet1</servlet-class>  
  6. </servlet>  
  7.   
  8. <servlet-mapping>  
  9. <servlet-name>s1</servlet-name>  
  10. <url-pattern>/servlet1</url-pattern>  
  11. </servlet-mapping>  
  12.   
  13. <servlet>  
  14. <servlet-name>s2</servlet-name>  
  15. <servlet-class>DemoServlet2</servlet-class>  
  16. </servlet>  
  17.   
  18. <servlet-mapping>  
  19. <servlet-name>s2</servlet-name>  
  20. <url-pattern>/servlet2</url-pattern>  
  21. </servlet-mapping>  
  22.   
  23. </web-app>

No comments:

Post a Comment