File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes ServletContext events Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "ServletContext events" Watch "ServletContext events" New topic
Author

ServletContext events

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi!
I have in the path C:\WINDOWS\jbproject\WebDeveloperUno\webapps\ROOT\WEB-INF\classes\webdeveloperuno
the below class:
package webdeveloperuno;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class ServletContextAttribListener
implements ServletContextAttributeListener {
//This method is invoked when an attribute
//is added to the ServletContext object
public void attributeAdded (ServletContextAttributeEvent scab)
{
System.out.println("An attribute was added to the " +
"ServletContext object");
}
//This method is invoked when an attribute
//is removed from the ServletContext object
public void attributeRemoved (ServletContextAttributeEvent scab)
{
System.out.println("An attribute was removed from " +
"the ServletContext object");
}
//This method is invoked when an attribute
//is replaced in the ServletContext object
public void attributeReplaced (ServletContextAttributeEvent scab)
{
System.out.println("An attribute was replaced in the " +
"ServletContext object");
}
}
And I have also in the path C:\WINDOWS\jbproject\WebDeveloperUno\webapps\ROOT\WEB-INF
the below file web.xml
<web-app>
<!-- Define application events listeners -->
<listener>
<listener-class>
webdeveloperuno.ServletContextAttribListener
</listener-class>
</listener>
<context-param>
<param-name>param1</param-name>
<param-value>value1</param-value>
</context-param>
<servlet>
<servlet-name>atrib</servlet-name>
<servlet-class>webdeveloperuno.ServletContextAttrib</servlet-class>
</servlet>
</web-app>
I add attributes with the servlet atrib correctly but "An attribute was added to the ServletContext object" is not write in the console. Why???
Thank you in advance.
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

Could you post the code for ServletContextAttrib also, if its not too much trouble...esp. the part of code where you add the attribute to the ServletContext.
Also, with what URL are you accessing the webdeveloperuno.ServletContextAttrib servlet?
regds.
- satya


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
This is the code:
package webdeveloperuno;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ServletContextAttrib extends HttpServlet
{
public void service(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
ServletOutputStream out = response.getOutputStream();
out.print("<?xml version='1.0' encoding='UTF-8'?>");
out.print("<!DOCTYPE html");
out.print("PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'");
out.print("'DTD/xhtml1-strict.dtd'>");
out.print("<html>");
out.print("<head>");
out.print("<title>ServletContext Attributes</title>");
out.print("</head>");
out.print("<body>");
ServletContext context = getServletContext();
String action = request.getParameter("action");
String name = request.getParameter("name");
String value = request.getParameter("value");
if (action == null) {}
else {
if (action.equals("add"))
{
String test = (String) context.getAttribute(name);
if (test == null)
{
context.setAttribute(name, value);
out.print("Added Item To ServletContext object");
} else {
context.setAttribute(name, value);
out.print("Replaced Item in ServletContext");
}
}
else if (action.equals("remove"))
{
String test = (String) context.getAttribute(name);
if (test == null) {
out.print("Item does not exist");
} else {
context.removeAttribute(name);
out.print("Removed Item From ServletContext");
}
}
else
{
String test = (String) context.getAttribute(name);
if (test == null)
{
context.setAttribute(name, value);
out.print("Added Item To ServletContext object");
} else {
context.setAttribute(name, value);
out.print("Replaced Item in ServletContext");
}
}
}
out.print("<center> <br /> <br />");
out.print("<a href='/eventservlets.htm'>");
out.print("Back To Home Page");
out.print("</a>");
out.print("</center>");
out.print("</body>");
out.print("</html>");
}
}
And I call de Servlet with <form name="MyForm" action="./servlet/atrib" method="POST">
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ServletContext events