0% found this document useful (0 votes)
126 views5 pages

Practical - 6: Listener File

The document describes creating a ServletContextAttributeListener that displays a message whenever an attribute is added, removed, or replaced in the servlet context. It includes code for a Listener class that implements the listener interface and overrides the necessary methods, a Servlet class that adds/removes attributes to trigger the listener, and an XML file mapping the servlet and registering the listener.

Uploaded by

Dhrumil Dancer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
126 views5 pages

Practical - 6: Listener File

The document describes creating a ServletContextAttributeListener that displays a message whenever an attribute is added, removed, or replaced in the servlet context. It includes code for a Listener class that implements the listener interface and overrides the necessary methods, a Servlet class that adds/removes attributes to trigger the listener, and an XML file mapping the servlet and registering the listener.

Uploaded by

Dhrumil Dancer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

18BECE30063 AJP Practical - 6

Practical – 6
Aim:- Create a Servlet that implements ServletContextAttributeListener
interface such that a message dialog is displayed whenever an attribute is
added or removed or replaced.

Listener File:

package servletContext;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
* Application Lifecycle Listener implementation class L1
*
*/
@WebListener
public class L1 implements ServletContextListener, ServletContextAttributeListener {

/**
* Default constructor.
*/
public L1() {
// TODO Auto-generated constructor stub
}

/**
* @see ServletContextAttributeListener#attributeAdded(ServletContextAttributeEvent)
*/
public void attributeAdded(ServletContextAttributeEvent scae) {
// TODO Auto-generated method stub
System.out.println("Attribute Added from class L1...."+scae.getName()
+":"+scae.getValue());
}

/**
* @see ServletContextAttributeListener#attributeRemoved(ServletContextAttributeEvent)
*/
public void attributeRemoved(ServletContextAttributeEvent scae) {
// TODO Auto-generated method stub
System.out.println("Attribute Removed from class L1...."+scae.getName()
+":"+scae.getValue());
}
1
Page

/**
18BECE30063 AJP Practical - 6

* @see ServletContextListener#contextDestroyed(ServletContextEvent)
*/
public void contextDestroyed(ServletContextEvent sce) {
// TODO Auto-generated method stub
System.out.println("Context
Destroyed...."+sce.getServletContext().getServletContextName());
}

/**
* @see ServletContextAttributeListener#attributeReplaced(ServletContextAttributeEvent)
*/
public void attributeReplaced(ServletContextAttributeEvent scae) {
// TODO Auto-generated method stub
System.out.println("Attribute Replaced from class L1...."+scae.getName()
+":"+scae.getValue());
}

/**
* @see ServletContextListener#contextInitialized(ServletContextEvent)
*/
public void contextInitialized(ServletContextEvent sce) {
// TODO Auto-generated method stub
System.out.println("Context
Initialized....."+sce.getServletContext().getServletContextName());
}

Servlet File:-

package servletContext;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class S1
*/
@WebServlet("/S1")
public class S1 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void processRequest(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
2

try(PrintWriter out = response.getWriter()){


Page

out.print("HEllO WORLD...");
18BECE30063 AJP Practical - 6

ServletContext sc = getServletContext();
sc.setAttribute("name", "name");
sc.setAttribute("value", "ANKIT");
sc.setAttribute("name","SMIT");
sc.removeAttribute("name");
sc.getAttribute("value");

}
}

/**
* @see HttpServlet#HttpServlet()
*/
public S1() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at:
").append(request.getContextPath());
processRequest(request , response);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
//doGet(request, response);
processRequest(request , response);
}
public String getServletInfo() {
return "Short description";
}

XML File:-
3

<?xml version="1.0" encoding="UTF-8"?>


Page

<element>
18BECE30063 AJP Practical - 6

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>prectical6</display-name>
<servlet>
<servlet-name>attributel1</servlet-name>
<servlet-class>servletContext.S1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>attributel1</servlet-name>
<url-pattern>/S1</url-pattern>
</servlet-mapping>
<listener>
<listener-class>L1</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
</element>

OUTPUT:-
1) Eclipse Browser.

4
Page
18BECE30063 AJP Practical - 6

2) External Browser

3) Console

5
Page

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy