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(“<a href=’servlet2′>visit</a>”);

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

web.xml:

<web-app>

<servlet>

<servlet-name>s1</servlet-name>

<servlet-class>DemoServlet1</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>s1</servlet-name>

<url-pattern>/servlet1</url-pattern>

</servlet-mapping>

<servlet>

<servlet-name>s2</servlet-name>

<servlet-class>DemoServlet2</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>s2</servlet-name>

<url-pattern>/servlet2</url-pattern>

</servlet-mapping>

</web-app>

Attributes :

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.

 

 

Subscribe
to our newsletter

Drop Us A Query

Trending Courses
  • PEGA Testing Training
  • Today application testing is the deciding factor to launch the application into the market. And people do not launch the application unless it goes true.

  • Blockchain Online Course
  • Today many people were enthusiastic, to know the exact details of things happening around him. This can get the proper knowledge on Blockchain.

  • Ethical Hacking Online Course
  • Zeal to learn ethical hacking is common among college students and IT professionals. Because everybody wants to secure their system from cyber attacks.

  • Python Online Training
  • Python is a dynamic interrupted language which is used in wide varieties of applications. It is very interactive object oriented and high-level programming language.

  • Tableau Training
  • Tableau is a Software company that caters interactive data visualization products that provide Business Intelligence services. The company’s Head Quarters is in Seattle, USA.

  • PEGA Online Training
  • Pega Systems Inc. is a Cambridge, Massachusetts based Software Company. It is known for developing software for Customer Relationship Management (CRM) and Business process Management (BPM).


100% Secure Payments. All major credit & debit cards accepted.