Tuesday, February 24, 2009

Spring MVC simple example : Spring Tutorial

This is very basic running example for spring MVC. I developed this project in IMB RAD (
Rational Software Development Platform 6.0) and WebSphere Application Server v6.0.

Step1: Create one Dynamic web project (File -> New -> Dynamic Web Project) and give some name (Say SpringTrans).
Step2: Add spring.jar to project and copy spring.jar to WEB-INF\lib folder
Step3: Edit web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app id="WebApp_ID" 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%22&gt;

Step 4: Create one xml file SpringTrans-servlet.xml
Note: Name must be SpringTrans-servlet.xml, since we have given the servlet name SpringTrans, so it is SpringTrans-servlet.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd%22&gt;
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<entry key="/loginCheck.htm" value-ref="userController"/>
<bean id="userController" class="com.controller.UserController">
<property name="methodNameResolver">
<bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
<property name="mappings">
<prop key="/loginCheck.htm">loginHandler</prop>

Step 5: In Java Resource, create one package com.controller
Step 6: Create one Java file in this package UserController.java

package com.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class UserController extends MultiActionController {
public ModelAndView loginHandler( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("*********** FROM CONTROLLER *************");
String userInfoName = request.getParameter("userName");
String pass = request.getParameter("password");
System.out.println("User Name :: "+userInfoName);
System.out.println("Password :: "+pass);
return new ModelAndView("/SucessPage.jsp","user",userInfoName);

Step 7: Create jsp file inside WebContent folder

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<TITLE>Login Page</TITLE>
<form action="loginCheck.htm">
<TD width="326" align="right">User Name</TD>
<TD width="311"><input type="text" name="userName"/></TD>
<TD width="326" align="right">Password</TD>
<TD width="311"><input type="text" name="password"/></TD>
<TD width="326" align="right"><input type="submit" value="OK"/></TD>
<TD width="311"><input type="button" value="Cancel"/></TD>

Step 8: SucessPage.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<TITLE>SucessPage Page</TITLE>
<P>Your Login is Sucessfull</P>
String userName = request.getParameter("userName");
try{ out.print(userName); }catch(Exception e){}

Run http://localhost:9080/SpringTrans/


  1. Great tutorial and thanks for the sharing your experience with us. Its really nice to read your post and learn so many thing. Keep posting like this.

  2. Really very good tutorial for start up....

  3. Thanks for sharing very clear tutorial for beginner.

  4. In login page , after giving username and password when I am clicking OK button "webpage not found" page is displayed. Can any one help me in solving this issue.

    Thanks in advance

  5. Thanks for sharing such a great blog..Keep posting..

    JAVA Institutes in Chennai

  6. Thank you, I really appreciate for the helpful tutorial


You can put your comments here (Either feedback or your Question related to blog)