Академический Документы
Профессиональный Документы
Культура Документы
Contents
1 Listeners
2 Types of Listeners
3 Request Listeners
4 Hierarchy of Servlet Request Event and Servlet Request AttributeEvent
5 The Servlet Request Listener Class
6 The Timer Class.
7 The First Servlet Which Is Reading The “time” Attribute.
8 Another Servlet In The Same Application Reading The “time” Attribute.
9 Declaration Of Listeners in DD
10 Context Listeners
Contents
11 Hierarchy of Servlet Context Event and Servlet Context AttributeEvent
12 A Simple Java Code
13 The ServletContextListener implementing class.
14 The web.xml
15 Session Listeners
16 Listener that is calculating the discount.
17 Servlet that generating the bill .
18 Heirarchy of HttpSessionEvent and HttpSessionBinding Event
19 Declaration in Deployment Descriptor
Know
• What Listeners are
• Types of Listeners
Be able to
• Use Listeners
Listeners
• Listeners are java files that are invoked by the
web container on occurrence of some event.
Request Listener
Context Listener
Session Listener
Request Listeners
EventObject
java.util
Object getSource()
ServletRequestEvent
ServletContext getServletContext()
ServletRequest getServletRequest()
ServletRequestAttributeEvent javax.servlet
String getName()
Object getValue()
Example
• Calculating the time a user spent in a web site.
The Servlet Request Listener Class.
import javax.servlet.*;
import javax.servlet.http.*;
HttpSession ses=req.getSession(false);
if(ses==null){
HttpSession sess=req.getSession();
Timer tm=new Timer();
Thread t=new Thread(tm);
t.setDaemon(true);
t.start();
sess.setAttribute("time",new
Integer(tm.sec));
sess.setAttribute("threadObj",tm);
}
else {
Timer
tt=(Timer)ses.getAttribute("threadObj");
ses.setAttribute("time",new
Integer(tt.sec));
System.out.println((Integer)ses.getAttribute
("time"));
}
public void
requestDestroyed(ServletRequestEvent event)
{
System.out.println("out");
}
}
The Timer Class.
public class Timer implements Runnable
{
int sec=1;
public void run()
{
try{
while(true)
{
Thread.sleep(1000);
sec++;
}
}catch(Exception e){System.out.println(e);}
}
}
The First Servlet Which Is Reading
The “time” Attribute.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
out.println("<br><a
href='index.html'>home</a></body></html>");
}
}
Another Servlet In The Same Application
Reading The “time” Attribute.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
<servlet>
//Declare the servlets
</servlet>
<servlet-mapping>
// Give the urls
</servlet-mapping>
<listener>
<listener-class>
TimeCounter
</listener-class>
</listener>
</web-app>
Context Listeners
• public void
attributeAdded(ServletContextAttributeEvent event) –
called when ever a new attribute is added to the
servlet context.
• public void
attributeRemoved(ServletContextAttributeEvent event)
– called whenever an attribute is removed from the
servlet context.
• public void
attributeReplaced(ServletContextAttributeEvent event)
– called whenever a context attribute is replaced
(calling setAttribute() for an attribute name already in
use by the servlet context).
Hierarchy of ServletContextEvent
and ServletContextAttributeEvent
EventObject
java.util
Object getSource()
ServletContextEvent
ServletContext getServletContext()
ServletContextAttributeEvent javax.servlet
String getName()
Object getValue()
Example
• Getting the JNDI for datasource from DD and
storing the DataSource object in the application
context .
A Simple Java Code
public class Conn
{
String driver;
String url;
String username;
String password;
public Conn(){}
System.out.println("Context initialized");
}
<listener>
<listener-class>
CreateConn
</listener-class>
</listener>
<context-param>
<param-name>driver</param-name>
<param-value>oracle.jdbc.OracleDriver</param
value>
</context-param>
<context-param>
<param-name>url</param-name>
<param
value>jdbc:oracle:thin:@scicom6:1521:xe</param-
value>
</context-param>
<context-param>
<param-name>username</param-name>
<param-value>SYSTEM</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>scicom</param-value>
</context-param>
</web-app>
// Write the imports statements.
out.println("<tr><td>"+rs.getInt(1)+"</td><td>"+rs.getStrin
g(2)+"</td>");
}
out.println("</table></center>");
out.println("</body></html>");
con.close();
}catch(Exception e){System.out.println(e);}
}
}
}
Session Listeners
• public void
sessionCreated(HttpSessionEvent event)
-called when a new session is created.
• public void
sessionDestroyed(HttpSessionEvent event)
-called at the moment a session is about to
be invalidated (but before the session
becomes invalid and unusable)
A class implementing HttpSessionAttributeListener
interface has three methods to implement.
HttpSession ses=event.getSession();
System.out.println("added");
ses.removeAttribute("DiscountAmt");
Double tot=(Double)ses.getAttribute("Total");
if(tot.doubleValue()>5000)
ses.setAttribute("DiscountAmt",new Double(20)) ;
}
public void
attributeRemoved(HttpSessionBindingEventevent
){ }
public void attributeReplaced(HttpSessionBindingEvent
event)
{
HttpSession ses=event.getSession();
ses.removeAttribute("DiscountAmt");
System.out.println("replaced");
Double tot=(Double)ses.getAttribute("Total");
if(tot.doubleValue()>5000)
ses.setAttribute("DiscountAmt",new
Double(20)) ;
}
}
Servlet that generating the bill .
// import statements
public class GenerateBill extends HttpServlet
{
public void doGet(HttpServletRequest
req,HttpServletResponse res) throws
ServletException,IOException
{
PrintWriter out=res.getWriter();
String []item=req.getParameterValues("chk1");
// Create the season and read the DiscountAmt
Attribute added by the attributeAdded() method.
........................................................
.........................................................
ses.setAttribute("Total",new
Double(totalamt));
Double
dis=(Double)ses.getAttribute("DiscountAmt
");
}
<web-app>
<display-name>Http Listener Test</display-
name>
<servlet>
<servlet-name>GenerateBill</servlet-
name>
<servlet-class>GenerateBill</servlet-
class>
</servlet>
<servlet-mapping>
<servlet-name>GenerateBill</servlet-
name>
<url-pattern>/payment.do</url-pattern>
</servlet-mapping>
This Discount part can easily taken
<listener> away after the festival without altering
<listener-class> other codes
DiscountCheck
</listener-class>
</listener>
</web-app>
A class implementing
HttpSessionBindingListener interface has two
methods to implement.
• public void
valueBound(HttpSessionBindingEvent event)
– called whenever the object implementing
the interface is the value object set as
attribute value to the session.
• public void
valueUnbound(HttpSessionBindingEvent
event) – called whenever the object
implementing the interface is the value object
removed from the session.
Example
• Library application- user browses the library
books and selects the book he want to borrow
and adds it to the cart. On adding a library book
object into cart(session) it is marked as
unavailable in the database in the valueBound().
If it is taken out of the cart then it is marked
available.
A class implementing
HttpSessionActivationListener interface has
two methods to implement.
• public void
sessionWillPassivate(HttpSessionEventeve
nt) – called just before a session is serialized to be
cloned to another JVM.
• public void
sessionDidActivate(HttpSessionEvent
event) – called just after a cloned session is
deserialized in a target JVM.
Heirarchy of HttpSessionEvent
and HttpSessionBinding Event
EventObject
java.util
Object getSource()
HttpSessionEvent
HttpSessionBindingEvent javax.servlet.http
HttpSessionBindingListener
And
HttpSessionActivationListener
are not declared in Deployment Descriptor!!
Declaration in Deployment
Descriptor