Chapter 5 Attribute and listeners

Servlet Init Parameter : available only for that servlet. Defined within the specific servlet.
DD

<servlet>
	<servlet-name>Servlet Name</servlet-name>
	<servlet-class>com.test.ServletClass</servlet-class>
	<init-param>
		<param-name>Email</param-name>
		<param-value>admin@email.com</param-value>
	</init-param>
</servlet>

In servlet

ServletConfig sc = getServletConfig();
String email = sc.getInitParameter("EMAIL");
  • You can use getInitParameterNames() to go through all init parameters
  • Also note ServletConfig will not be available in forwarding JSP. The ServletConfig init parameters must be passed to JSP as other scope’s Attributes.

Context Init Parameter: available for all the servlets in the application
Note that the <context-param> element does not reside within <servlet> element

<servlet> ... </servlet>
<context-param>
	<param-name>AlertEmail</param-name>
	<param-value>alertall@email.com</param-value>
</context-param>

In servlet

ServletContext scx = getServletContext();
String email = scx.getInitParameter("AlertEmail");
  • Context Param can be accessed from any Servlet and JSP within the application


Listener Interfaces
ServletContextListener : Listner is a event handler class. Usually separate class from servlet
How to set it up.
1. Create Class implements ServletContextListener interface

import javax.servlet.*;
public class MyServletContextListener implements ServletContextListener {
	public void contextInitialized(ServletContextEvent event) { ... }
	public void contextDestroyed(ServletContextEvent event) { ... }
}

2. Put the class file into /WEB-INF/classes
3. update DD web.xml. Each listener needs separate

<listener>
	<listener-class>com.test.MyServletContextListener</listener-class>
</listener>
Listener Name Event Type method arg Description
ServletContextAttributeListener ServletContextAttributeEvent
ServletContextListener
contextInitialized()
contextDestroyed()
ServletContextEvent
HttpSessionListener
sessionCreated()
sessionDestroyed()
HttpSessionEvent
HttpSessionAttributeListener HttpSessionBindingEvent
HttpSessionBindingListener
valueBound()
valueUnbound()
HttpSessionBindingEvent used on the class used as Attribute object
HttpSessionActivationListener
sessionDidActivate()
sessionWillPassivate()
HttpSessionEvent Session is migrated to and from another JVM
ServletRequestListener
requestInitialized()
requestDestroyed()
ServletRequestEvent For each request
ServletRequestAttributeListener ServletRequestAttributeEvent

All attribute listener has the same methods
attributeAdded()
attributeRemoved()
attributeReplaced()

Attributes Parameters
Application/context , Request, Session Application/context init, Request , Servlet init (no Session)
setAttributes(string name, object value) No set method
getAttributes returns object getInitParameter & request.getParameter returns string

Attribute methods

Object getAttribute(String name)
void setAttribute(String name, Object value)
void removeAttribute(String name)
Enumeration getAttributeNames()

Thread Safety on Context and Session object: Do not synchronize service method (such as doPost, doGet) . Synchronize on the Context or Session object.

synchronized (getServletContext()) {
	getServletContext().setAttribute("foo","22");}

SingleThreadModel interface is designed to protect instant variables.
Only Request Attribute & method local variables are thread safe.

RequestDispatcher object :

  • You can get it from both ServletRequest and ServletContext object. If called from ServletContext object, the arg must start with “/” absolute path
  • Two functions : forward(), include()
  • You can’t forward the request if you’ve already committed the response. Once write flush() occurs, forward () call will cause IllegalStateException.

Next is Chapter 6 Session Management

(*) Next: [SCWCD] Cram Sheet 3 – session management

Share and Enjoy:
  • DZone
  • Twitter
  • Technorati
  • Reddit
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Diigo


One Response to “[SCWCD] Cram Sheet 2 – attribute and listeners”

  1.   Remember the code? » Blog Archive » [SCWCD] Cram Sheet 1 - request and response Says:

    [...] Next: [SCWCD] Cram Sheet 2 – attribute and listeners Posted by HanaDaddy Filed in java Tags: SCWCD [...]

Leave a Reply