Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ServletContext events

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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">
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic