Double Bonanza Offer - Upto 30% Off + 1 Self Paced Course Free | OFFER ENDING IN: 0 D 0 H 0 M 0 S

Log In to start Learning

Login via

Post By Admin Last Updated At 2020-06-15
Attributes in Servlet
Attributes in Servlet:
  1. A quality in servlet is a protest that can be set, get or expelled from one of the accompanying extensions request scope
  2. session scope
  3. application scope

The servlet software engineer can pass informations starting with one servlet then onto the next utilizing properties. It is much the same as passing item starting with one class then onto the next so we can reuse a similar question over and over.

         Methods:
  1. public void setAttribute(String name,Object object)
  2. public Object getAttribute(String name)
  3. public Enumeration getInitParameterNames()
  4. public void removeAttribute(String name)
Example:DemoServlet1.java :import java.io.*;import javax.servlet.*;import javax.servlet.http.*;public class DemoServlet1 extends HttpServlet{public void doGet(HttpServletRequest req,HttpServletResponse res){try{res.setContentType("text/html");PrintWriter out=res.getWriter();ServletContext context=getServletContext();context.setAttribute("company","IBM");out.println("Welcome to first servlet");out.println("visit");out.close();}catch(Exception e){out.println(e);}}}DemoServlet2.java :imort java.io.*;import javax.servlet.*;import javax.servlet.http.*;public class DemoServlet2 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("company");out.println("Welcome to "+n);out.close();}catch(Exception e){out.println(e);}}}

JAVA Tutorial Video

[embed]https://www.youtube.com/watch?v=zr1RxswTahU&t=642s [/embed]web.xml:s1class>DemoServlet1class>s1/servlet1s2class>DemoServlet2class>s2/servlet2Attributes :

sets the given protest in the application scope.

open Object getAttribute(String name):Returns the trait for the predetermined name.

open Enumeration getInitParameterNames():Returns the names of the setting's introduction parameters as an Enumeration of String objects.

open void removeAttribute(String name):Removes the trait with the given name from the servlet setting.