Professional Documents
Culture Documents
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
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
<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