Pages

Wednesday, July 28, 2010

Spring Context Loader Servlet

(org.springframework.web.context.ContextLoaderServlet)
Context Loader Servlet start up Spring's root WebApplicationContext when web application is loaded.
This class has been deprecated for containers implementing Servlet API 2.4 or higher, in favor of ContextLoaderListener. According to Servlet 2.4, listeners must be initialized before load-on-startup servlets. Many Servlet 2.3 containers already enforce this behavior. If you use such a container, this servlet can be replaced with ContextLoaderListener. Else or if working with a Servlet 2.2 container, stick with this servlet.
Servlet 2.3 containers known to work with bootstrap listeners are:
  • Apache Tomcat 4.x+
  • Jetty 4.x+
  • Resin 2.1.8+
  • Orion 2.0.2+
  • BEA WebLogic 8.1 SP3
For working with any of them, ContextLoaderListener is recommended.
Servlet 2.3 containers known not to work with bootstrap listeners are:
  • BEA WebLogic up to 8.1 SP2
  • IBM WebSphere 5.x
  • Oracle OC4J 9.0.3
Step 1 : web.xml
<?xml version="1.0" encoding="UTF-8"?>


<web-app version="2.4" 
xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>TestServlet</servlet-name>
    <servlet-class>com.TestServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>TestServlet</servlet-name>
    <url-pattern>/servlet/TestServlet</url-pattern>
  </servlet-mapping>
  
   <context-param>  
    <param-name>contextConfigLocation</param-name>  
    <param-value>/WEB-INF/applicationContext.xml</param-value>  
  </context-param>  
    
  <listener>  
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  </listener> 
</web-app>

Step 2 : applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="employeeBean" class="com.Employee">
<property name="name" value="Vardhan"></property>
<property name="empId" value="31413"></property>
</bean>
</beans>

Step 3 : TestServlet
package com;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class TestServlet extends HttpServlet {

/**
* Destruction of the servlet.
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doGet method of the servlet.
*
* This method is called when a form has its tag value method equals to get.
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
ServletContext context = getServletContext();
WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(context);
Employee emp = (Employee)applicationContext.getBean("employeeBean");
System.out.println("Name >>>>>  "+ emp.getName());
}

/**
* Initialization of the servlet.
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}

}

Step 4 : invoke the Url

http://localhost:8080/servlet/TestServlet

check the tomcat console 

Output : Name >>>>>> Vardhan


ContextLoaderListner

Bootstrap listener to start up Spring's root WebApplicationContext. Simply delegates to ContextLoader.
This listener should be registered after Log4jConfigListener in web.xml, if the latter is used.
Note ***  For Servlet 2.2 containers and Servlet 2.3 ones that do not initalize listeners before servlets, use ContextLoaderServlet. See the latter's Javadoc for details.


<web-app>
<!-- Spring context Configuration Begins-->
    <context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<servlet>

 <servlet-name>context</servlet-name>
   <servlet-class>
     org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

(OR)
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>








No comments:

Post a Comment