JAVA - Spring MVC application step-by-step tutorial


JAVA - Spring MVC application step-by-step tutorial

Software Required :

1.Spring tool suite3.6.0- Framework used
2.apache-tomcat-7.0-Server required

Spring Tool Suite :

The Spring Tool Suite is an Eclipse-based development environment that is customized for developing Spring applications.
You can download the same from Here : Download from here 

Installing and setting up STS :

1.Download Spring Tool Suite latest version. Current download link is: Download from here
Click to download ‘SPRING TOOL SUITE’.

2.Once downloaded, double click to install. Click next, Accept License, Click Next, Select Installation Path, Click Next.

3.Select defaults including tc server and click next. STS includes its own version of Tomcat called tc-server.

4.Select a JDK (not JRE) and click Next.

5.Continue to Click Next leaving defaults until the last page, where you can select Launch STS and click finish.

6.Double click the STS icon shown below to start OR Right click and select Open .

JAVA-Spring MVC application

Tomcat :

Apache Tomcat,  is an open-source web server developed by the Apache Software Foundation (ASF). Tomcat implements several Java EE specifications including Java Servlet, JavaServer Pages (JSP), Java EL, and WebSocket, and provides a "pure Java" HTTP web server environment in which Java code can run.
Tomcat is developed and maintained by an open community of developers under the auspices of the Apache Software Foundation, released under the Apache License 2.0 license, and is open-source software.
Download the same:- Download from here

Extract the zip folder can copy the path up to the folder containing bin.
 Wherever the tomcat path is required provide the same:-
For eg:- C:\Users \Documents \Applications\apache-tomcat-7.0.54 .

Lets try to create a simple MVC application :

Open STS – Spring tool Suite
While starting, STS will ask for workspace, like eclipse.
Create Workspace :

JAVA-Spring MVC application

Create new Dynamic Web Project by following steps:-
File->New->Click “Dynamic Web Project”.
Provide  the Project  Name :

Provide  the Project  Name-spring MVC

For Adding Tomcat server Select New Runtime:

Tomcat server Runtime-spring MVC

Click Next

Click Browse for the path of Tomcat Sever (Browser till the path where tomcat folder is located )

TOMCAT server path- spring MVC

After Adding Path Click Finish :

TOMCAT server added : Spring MVC
Tomcat Server got Added .Click Next

Added TomCat Server
Click Next :

Adding Context root
Click Finish ..
Project added successfully.

Now Add the same project to the server added
Right click the server panel
Click New-> Server

Adding project to server - spring MVC

Select tomcat 7 and Click next

Configure resource on server

Select resource which we want to configure on server, here in our case only one resource is there, so we can select “AddAll”.

select resource for configuration on server

Click Finish.

spring tool set window

Create a Package under the src folder .Right click on the src folder -> new ->Package. Give  a specific name to package
(Follow specific name standard)

Path for creating pacakge

Naming Your Package

Create a class  under the created package. Right click on the package created -> new -> Class

Path for creating Class

Give Specific name to class you are creating :

Specific name to class

Here for example purpose lets create a class for Login Details:

LoginDetails is the bean class where we declare the variables and add getter and setter of it to access the value easily . We have one more method validate(), which is used to validate a user by validating the user entered password with the defined one. Method will return a boolean value.

Login Details Class

Enter below code in class :

publicclassLoginDetails {
        private String name,password;

        public String getName() {

        publicvoidsetName(String name) {
       = name;

        public String getPassword() {

        publicvoidsetPassword(String password) {
                this.password = password;
        publicboolean validate(){ 

Create controller class similarly as we created LoginDetails :

CLASS DETAILS class extends HttpServlet which has doGet and doPost methods
Controlller will receive the user entered details by request.getParameter() method . This method willl have parameters same as the name we specify in the jsp file.
Setting those user entered values to the bean “LoginDetails ” by using setters
Validate() method will provide us with the data ,which will specify whether the user is valid or not
Using RequestDispatcher object the response is shown according to the request.

Main Controller class

Code for Main Controller Class :


        protectedvoiddoPost(HttpServletRequestrequest, HttpServletResponseresponse
        throwsServletException, IOException { 
                String name=request.getParameter("name"); 
                String password=request.getParameter("password"); 
        rd.forward(request, response); 
        rd.forward(request, response); 
        protectedvoiddoGet(HttpServletRequestreq, HttpServletResponseresp
        throwsServletException, IOException { 
        doPost(req, resp); 

Create index.jsp :

Creating JSP page

Name jsp file as Index.jsp :

Naming JSP file

Click Finish.

Index.jsp will help to take inputs from user.


Enter following Code in Index.jsp :

<%@pagelanguage="java"contentType="text/html; charset=ISO-8859-1"
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""">
<metahttp-equiv="Content-Type"content="text/html; charset=ISO-8859-1">
<title>Conflate Java MVC Demo</title>

Similarly Create success.jsp :

Success.jsp creation

JAVA-Spring MVC application

Enter following Code in Success.jsp :

<%@pagelanguage="java"contentType="text/html; charset=ISO-8859-1"
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""">
<metahttp-equiv="Content-Type"content="text/html; charset=ISO-8859-1">
<title>Conflate Java MVC Demo</title>
<p>You are successfully logged in!</p>
LoginDetails bean=(LoginDetails)request.getAttribute("bean"); 
out.print("Welcome, "+bean.getName()); 

Now Create error.jsp :

Error.jsp creation

JAVA-Spring MVC application

Enter following Code in Error.jsp :

<p>Sorry! username or password error</p>

Next step is to Create web.xml :

creation on xml file

Go to => package created -> WebContent -> WEB-INF 
                and right click, then click on New and select Others 

JAVA-Spring MVC application

Select XML File from  xml folder in drop down and click on Next.
JAVA-Spring MVC application

Name file as “web”.it will get saved in in WEB-INF folder.

JAVA-Spring MVC application

Web.xml file will get on Source tab to write code inside this file.

JAVA-Spring MVC application

Add below code in Web.xml file :


Run the application by following steps:-

Right click the index.jsp and Select Run as-> Run On Server

JAVA-Spring MVC application

Enter Your Name and Password .

If you enter correct name and password present in Validate method present in  class,you will get messaged succefully Loged into Application from success.jsp.

JAVA-Spring MVC application

If you enter Wrong name and password then you will get error message from Error.jsp and redirect to login page(index.jsp).

Comment below if face any issue we are ready to help you.
If You Enjoyed This, Take 5 Seconds To Share It


  1. Thank you for sharing this blog. This blog will help to improve my knowledge.
    Core Spring Training | Spring and Hibernate Training | Spring source Training

  2. I feel happy to find your post, excellent way of writing and also I would like to share with my colleagues so that they also get the opportunity to read such an informative blog.
    JAVA J2EE Training in Chennai
    JAVA J2EE Training Institutes in Chennai