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.

 

 

Drop Us A Query

Trending Courses
  • Python and Django 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 Online 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.

  • MicroStrategy Online Training
  • Micro Strategy is one of the few independent and publicly trading Business Intelligence software provider in the market. The firm is operational in 27 Countries around the globe.

  • PEGA (PRPC) 7.2 Certification 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).

  • Workday Online Training
  • Workday specialises in providing Human Capital Management, Financial Management and payroll in online domain.It is a major web based ERP software vendor.

  • Power BI Online Training
  • Power BI is business analytics service by Microsoft. With Power BI, end users can develop reports and dashboards without depending on IT staff or Database Administrator.


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