JaiswalTraining

Get the online training

Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







Spring Web Module

Introduction

The web module is the module that enables you to write an web application using the spring container without xml.

To use it you just need to configure the web.xml.

Configuring web.xml

just add  the following lines in web.xml file

<web-app>
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/SpringConfiguration.xml</param-value>
    </context-param>
   
    <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
   
    <!-- <servlet>
        <servlet-name>SpringContainerCreatorServlet</servlet-name>
   <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
    </servlet>-->
</web-app>

There two ways of creating spring context in web application
  • Via Listener(ContextLoaderListener)
  • via Servlet(ContextLoaderServlet)

Simple "Hello World"  Web Application

  • In this example the object of class HelloWorld is created through spring container
  • In the servlet the reference of spring container is taken through servlet context object .
  • At last we are accessing the method of HelloWorld Class from servlet via spring container.
HelloWorld.java

package mypack;

public class HelloWorld {

  private String message;

  public HelloWorld() {
    System.out.println("Object of HelloWorld created.");
  }

  public void setMessage(String message) {
    this.message = message;
  }

  public String display() {
    return "Hello World";
  }
}

HelloServlet.java

package myservlet;

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 mypack.HelloWorld;

import org.springframework.web.
context.WebApplicationContext;

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


public class HelloServlet extends HttpServlet {

  protected void doGet(HttpServletRequest request,
      HttpServletResponse responsethrows
 
ServletException, IOException {

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    ServletContext sctx = getServletContext();
    WebApplicationContext springcontainer =
WebApplicationContextUtils
.getRequiredWebApplicationContext(sctx);

    HelloWorld helloWorld = (HelloWorldspringcontainer
        .getBean("helloWorld");
    out.println("<h2>" + helloWorld.display() "</h2>");

  }

}


web.xml


<web-app>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/SpringConfiguration.xml</param-value>
  </context-param>

  <listener>
    <listener-class>org.springframework.web.context.
ContextLoaderListener</listener-
class>
  </listener>

  <servlet>

    <servlet-name>HelloServlet</servlet-name>
    <servlet-class>myservlet.HelloServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>HelloServlet</servlet-name>
    <url-pattern>/HelloServlet</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>HelloServlet</welcome-file>
  </welcome-file-list>

</web-app>


SpringConfiguration.xml

<beans>
<bean id="helloWorld" class="mypack.HelloWorld">
  <property name="message" value="Hello World!"></property>
</bean>

</beans>


Output



Download Zip Code