Skip to main content

Simple Login Application Using Spring MVC and Hibernate – Part 1

 I hope developers working in web application development might hear about MVC architecture. Almost all technologies provide support for MVC based web application development, but the success is based on many factors like reusable of the code, maintenance of the code, future adaption of the code, etc..,
 The success of the Spring MVC is “Open for extension and closed for modification” principle. Using Spring MVC the developers can easily develop MVC based web application. We don’t need any steep learning curve at the same time we need to know the basics of spring framework and MVC architecture. The Spring MVC consists of following important components.
1. Dispatcher servlet
2. Controller
3. View Resolver
4. Model

Spring MVC - Overview

 The overall architecture of Spring MVC is shown here.
 1. When “Dispatcher Servlet” gets any request from client, it finds the corresponding mapped controller for the request and just dispatches the request to the corresponding controller. The requests and corresponding controller information are loaded into the context when application starts based on xml configuration or Meta data. We have many way for loading this information, we will discuss at this one at later point of time.

 2. We know, the controllers are used for connecting business and service layer. The controller get inputs from request, process the inputs, finally it returns the view and model information.

 3. The view resolver gets those information, find the corresponding view, it updates the view with model data and finally it send the response back to the “Dispatcher Servlet”.

 4. Finally the “Dispatcher Servlet” sends this response back to the client.

 Ok, let’s start developing our application. I have created the dynamic web project using STS and included all necessary jar files into class path of the application.

Step 1: Defining Dispatcher Servlet into web.xml file.


 Here “DispatcherServlet” class is configured with name as “dispatcher” and the url pattern as “*.spring”.

Step 2: Creating login.html page.


 Here I have created the html form with action as “Login.spring”. If user submits the page, the request is send to Login controller. Let’s create a login controller for handling the request.

Step 3: Creating Login Controller.


 @Controller annotation is very important because when context loads, the spring will find all controller class using this annotation and it's maps to the corresponding view (URL).

 @RequestMapping annotation is used for specifying action of the request.

 So now the dispatcher servlet gets any request from “Login.spring” that automatically redirects the request to “LoginController”.

 @RequestParam annotation is used for getting value from request parameters. It’s equivalent to request.getParameter() method in servlet.

  In my login controller, the processCredentials method just gets the username, password details from parameters and check whether both are equal. If it’s equal, it just returns the object of “ModelAndView” class with next redirecting page as a parameter.

 Hey In my login form, I have given action as “Login.spring” only. How that action automatically trigger my processCredentials method?


 Red indicated “results” is indicates redirecting page, this is resolved by “View resolver”, and another one is request attribute key and value pair. Now we need to configure our view resolver.

Step 4: Configuring view resolver.

 For configuring view resolver, we need to create one more xml file into inside the WEB-INF folder. The name of the xml file is very important, it’s is based on our dispatcher servlet name configured in web.xml file. The name should be “dispatcher servlet name-servlet.xml”. In our case, the “dispatcher” is our servlet name, so the name of the file is “dispatcher-servlet.xml”.


 Spring have lot of view resolver classes, for example ResourceBundleViewResolver, InternalResourceViewResolver, XsltViewResaolver, etc.., but by default spring use InternalResourceViewResolver class.

 In this XML file I have configured InternalResoureViewResolver class properties. Here I have given prefix value as “/” and suffix value as “.jsp”. In this situation, the view resolver get the view name from ModelAndView object (“results”) and append the prefix and suffix value (/results.jsp). So in our login controller we have verified user name, password values and redirected our request to results.jsp page. So we need to create results.jsp page.

 Before going to create results.jsp, I have to explain about the purpose of context:component-scan tag. This tag automatically scans all the controller class presents in base-package and registered into context. In other words, it’s just looks what are all the classes have @Controller annotation and map the controller into corresponding view. This information is maintained in context of the application.

 Ok let’s create a results.jsp page.

Step 5: Creating result.jsp file.

 This is my result.jsp page; here I have just get the message value from request attributes and displayed the message.


Step 5: Creating applicationContext.xml file.

 Spring MVC application needs applicationContext.xml file. Using this file we can configure our web related beans. As of now we don’t use any beans, so this file don’t have any bean definition.

 Ok. Let’s deploy our application into web server and will check the result.



 As of now, we didn’t integrate hibernate. In my next part, I will explain the stpes for integrating Hibernate ORM framework into our application. The part1 application source code is uploaded into my GitHub repository for your reference.

For integrating into hibernate, refer the below link.

http://www.pointerunits.com/2012/11/simple-login-application-using-spring_12.html

Comments

Giacomo said…
Hello Gnanasuriyan,
I'm following your tutorial and I've an issue that I can't resolve.
When I add the lines related to
org.springframework.web.context.ContextLoaderListener
to web.xml, my tomcat app crashes.
In stderr logs I've this generic error: "Grave: Error listenerStart" and I can't understand what is the problem.

thanks
Giacomo
Gnanasuriyan said…
Hi Giacome,
I hope, you have some problem in your class path setting and included JARS. So remove your all unwanted JARS included in your class path and then try. Here I have included some more reference links.

http://forum.springsource.org/showthread.php?39809-Spring-app-deployment-problem-(SEVERE-Error-listenerStart)

-Gnanasuriyan.
latha said…
hi sir,

i am new to spring, when ever i am running u r application getting bellow error.

HTTP Status 404 - /WebApplication1/

not displayed index.html page.

RAMKI said…
Hi Gnanasuriyan,

I created a sample application as said in the above tutorial and ran the same. but Am getting Null Pointer Exception at 'List userObjs = hibernateTemplate.find("from user_data u where u.userName=? and u.password=?",userName,password);". That is the HibernateTemplate is null. I don't know what mistake i have done.

Please help me in resolving it.

Thanks.
Unknown said…
thanks so much, you helped me a lot with part 1 and 2 of this tutorial!
Anonymous said…
Incomplete example which doesnt work. Looks like it was not tested.
alex said…

Do you want to play in an online casino? Try with us, do not be shy. perfect internet casino gambling There is such a stream of money that even your grandchildren will be enough)
Vicky Ram said…
Nice post. I learned some new information. Thanks for sharing.

imedicalassistants
Guest posting sites
pavithra dass said…
Thanks for sharing such a nice info.I hope you will share more information like this. please keep on sharing!
RPA course in Chennai
Robotics Courses in Bangalore
RPA Course in Bangalore
Robotics Courses in Coimbatore
RPA Training in Coimbatore
praveen said…
Nice! you are sharing such helpful and easy to understandable blog. i have no words for say i just say thanks because it is helpful for me.





Dot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery

Qode said…
Escribiste esta publicación con cautela, lo que es razonable para mí y, además, para otras personas que buscan este tipo de publicación comparativa. Una deuda de gratitud es por compartirlo. desarrolla aplicaciones moviles
Yamini said…
There is so much useful information in your post, I like it all the time. This is very useful.
Peoplesoft Admin Training
saivenkat said…
Great blog.you put Good stuff.All the topics were explained briefly.so quickly understand for me.I am waiting for your next fantastic blog.Thanks for sharing.Any coures related details learn...

Cloud Business Management Software Suite
Technogeekscs said…
This is most informative and also this post most user-friendly and super navigation to all posts.
Best RPA Training in Pune
AWS Training in Pune
Alayalegal said…
Very easy to understand your article. Thanks for taking the time to share this article.
Top Business Lawyers in Delhi
Information Technology Lawyers in Delhi
Best Corporate Lawyers in Delhi
oshin. said…
Your blog post is really amazing and informative. Thank you for sharing this valuable information.
If you are interested in learning digital marketing, here is a complete list of the best online digital marketing courses with certifications. In this article, you will learn about digital marketing and its different strategies, the need for doing digital marketing, the scope of digital marketing, career opportunities after doing online digital marketing, and many more.
Visit-
Online Digital Marketing Courses

oshin99 said…
Hi, I really appreciate you for sharing this interesting and informative blog. If you are interested in learning digital marketing, here is a list of the top 13 digital marketing courses in Ahmedabad with placements. This article will help you decide which institute is best for you to learn digital marketing and will help you to become an efficient and productive digital marketer.
Visit- Digital Marketing Courses in Ahmedabad
asley said…
informative article, keep sharing, thanks. Content Writing Course in Bangalore
Mousa Khan said…
It can use total bandwidth. It has recovery and resumes capabilities to restore the interrupted downloads due to lost connection, network issues, and power outages. Internet Download Manager Crack 2022
Pakistan Independence Day Quotes — Pakistan will remain free, free from every obstruction only if it remains to be the land of the brave. Independence Day Quotes Of Pakistan
riona said…
Thanks for posting such an informative article. Keep sharing.
Do check out into the Digital Marketing Courses in Delhi to know more about the topic.
Digital Marketing Courses in Delhi
Anonymous said…
The point on Spring MVC and Hibernate is proves the content to be excellent. Digital Marketing courses in Bahamas
Melys said…
Good work and thanks for sharing your knowledge about Simple Login Application Using Spring MVC and Hibernate. This will be helpful for many learners. Keep it up.
A Guide with Tips and Steps you need before Starting your Freelancing Journey.
In our blog we shared a guide with Tips and Steps about What is Freelancing and How does it work? This will help people to take a good decision before starting a Freelancing Journey. Read now:
What is Freelancing
Tanya Louis said…
Your blog is very informational and useful. I love visiting your blog. If you are looking for the best home tutors for your children in Mumbai, then you have come to the right place. Varni Home Education provides home-to-home tutors for all the standards, including all the subjects, all mediums, and all boards, giving personal attention to every child. It has a team of intellectual tutors who are proficient in their field of teaching. They not only address your child’s doubts and queries but will also provide unique and personalized study methods. Enroll now!
Visit- home tuitions in mumbai
shanaya said…
Amazing content. Keep sharing more contents.
Visit- Anchor ias coaching in Hyderabad
DMC Australia said…
Great tutorial for creating a simple login application using Spring MVC and Hibernate. It is well written and easy to follow. Thank you for sharing! Digital Marketing Courses in Australia
DMC Vancouver said…
Such an informative and excellent blog post. Additionally this is a very well written and comprehensive guide to creating a login application using the Spring framework. thanks for sharing with us. Digital Marketing Courses in Vancouver
DAC Mumbai said…
Fascinating and a great post about Simple Login Application Using Spring MVC and Hibernate! This is a really good example of how to use Spring MVC and Hibernate to create a simple login application. Thank you! Data Analytics Courses in Mumbai
DAC Gurgaon said…
This is an amazing blog post! You have explained the process of creating a simple login application using Spring MVC and Hibernate so well. I am very impressed with the technical knowledge and witting skills you have. Keep up the good work. Data Analytics Courses in Gurgaon
DAC Coimbatore said…
This is a great tutorial for anyone looking to create a simple login application using Spring MVC and Hibernate. The author does a great job of explaining each step along the way, and the code examples are very helpful. I would definitely recommend this tutorial to anyone looking to create a similar application. Thanks for sharing! Data Analytics Courses In Coimbatore
Hema R said…
Awesome blog. This a very well-written article about "MVC architecture." I enjoyed reading your article. Your explanation of Spring MVC is quite impressive. I appreciate the time and effort you've spent on this piece. Thanks for sharing it. Keep writing other informative posts in the coming months and also.
Digital marketing courses in Nagpur
DAC Ghana said…
This is an incredible instructional exercise for anybody looking to create a simple login application using Spring MVC and Hibernate. The creator works effectively of making sense of each step en route, and the code models are extremely useful. I would prescribe this instructional exercise to anybody looking to make a comparable application. Thanks for sharing!
Data Analytics Courses in Ghana
RHema said…
This genuinely great explanation on " Simple login application using MVC architecture and hibernate." Thanks for the in-depth explanation of developing the application. Glad I came across this article; it is easy to follow. Appreciating the time you have put into creating the post. Keep writing more informative posts.
Courses after bcom
Kasivishwa said…
Excellent blog. Nice article about " Simple login application using MVC architecture and hibernate." The way you have given the detailed developing application description is easy to follow. Thanks for sharing it. Keep writing other informative posts. Looking forward to learn more from your upcoming blogs. Digital marketing courses in patna
FMC Kenya said…
Wow! This blog is very well written. Anyone wishing to build a straightforward login application with Spring MVC and Hibernate should check out this guide. The code examples are quite informative, and the author does an excellent job of explaining each step along the way. I'd unquestionably advise anyone trying to develop a similar application to follow this guide. I appreciate you sharing!
financial modelling course in kenya
Hema09 said…
Fantastic blog. The description of "Simple Login Application Using Spring MVC and Hibernate" is outstanding. The critical component explained is also very easy to understand. Thanks for displaying the step-by-step instructions on spring MVC. Foreseeing to learn more from your future blogs. Keep sharing more. Financial modelling course in Singapore
This blog post is incredibly informative and well-written. It provides an in-depth guide on how to create a login application with the Spring framework. Thanks for sharing!
financial modelling course in bangalore
HemaK said…
A simple login application using spring MVC is an excellent topic to describe. The author has done a fantastic job of explaining it to the readers. Top-notch explanations on crucial elements, such as "Dispatcher servlet, Controller, View Resolver, and Model." I appreciate the effort taken by the writer. Thanks for sharing it with us. Do continue to post more. Data Analytics courses in leeds
DigitalM said…
I was searching for this sort of data and delighted in perusing this one. The writer has outdone himself this time. I have bookmarked it and I am also anticipating in perusing similar new articles. If anyone is looking to learn about financial modelling course in jaipur then I would like to recommend you with this article on financial modelling course in jaipur
KHema said…
Wonderful blog. The "Simple Login Application Using Spring MVC and Hibernate" description is excellent. Moreover, the key component's exposition is quite simple to comprehend. Thank you for providing the spring MVC step-by-step guidelines. I hope to learn more from your upcoming blogs. Keep sharing it. Data Analytics courses in Glasgow
Hema said…
Brilliant article on "Simple login application using MVC and Hibernate." The steps mentioned in this blog are easy to follow and implement. Thanks for sharing the key component of the MVC Spring. I have obtained more understanding after reading this article. Keep posting the more thoughtful post in the future. Data Analytics Scope
kiran said…
This blog on usage of Spring MVC and hibernate for web application is very thoughtful. The points where you mentioned Dispatcher Servlet shows your expertise in the area. Thank you for sharing such descriptive content.
Data Analytics Jobs
Hema said…
An interesting topic for discussion is a simple spring MVC login application. The author has excellently conveyed the reader's understanding. Excellent explanations of essential concepts like "Dispatcher servlet, Controller, View Resolver, and Model." I value the time the author took to write. Glad you have shared this topic with us. Keep up the good work. Data Analyst Course Syllabus
RDataAnalyst said…
This article on simple login application using spring MVC was something a new learning for me. I appreciate the writers work done in this article. Also, if anyone is interested in learning more about Data Analyst Salary In India, then I would like to recommend you with this article to know and learn more about: Data Analyst Salary In India
karanm said…
Hello blogger, your article on Spring MVC and hibernate is very informative. Usage of spring mvc for web developing is something I came to know after reading this. The way everything is explained makes it easier for even beginners like me to comprehend. Keep posting more such blogs.
Data Analytics VS Data Science
ananya said…
I liked your article. Your way of explaining was quite interesting and good. Keep it up!
CA Coaching in Mumbai
RGSTcourse said…
I'm really impressed by the brief explanation and easy to understand article on the content Application using Spring MVC and Hibernate. Keep posting such good posts. Also, if anyone is interested in learning more about Best GST Courses in India, then I would like to recommend you with this article on the Best GST Courses in India – A Detailed Exposition With Live Training. Best GST Courses in India

Popular posts from this blog

Getting key/value pair from JSON object and getting variable name and value from JavaScript object.

 Hi, I had faced one issue like this. I have an JSON object but I don't know any key name but I need to get the all the key and corresponding value from JSON object using client side JavaScript. Suddenly I wondered whether it's possible or not, after that I had done lot of workaround and finally got this solution. See the below example.    function getKeyValueFromJSON() {     var jsonObj =  {a:10,b:20,c:30,d:50} ;     for ( var key in jsonObj) {       alert( "Key: " + key + " value: " + jsonObj[key]);     }  }  In this example I have created the one json array as string, and converted this string into JSON object using eval() function. Using for-each loop I got all the key value from jsonObj, and finally using that key I got the corresponding value.  Finally I got the alert like this,    Key: a value:10    Key: b value:20    Key...

Simple Login Application Using Spring MVC and Hibernate – Part 2

 I hope you have visited my part1 of my tutorial . Let’s see the steps for integrating hibernate framework into Spring MVC. Here I have used MySQL database. I have created one database called “ springmvc ” and created one table called “ user ” with userid, username, password fields.  I have inserted some records into table like this. Step 1: Creating User POJO class.  We need to create a User POJO class for mapping user table. Ok let’s create it. Step 2: Creating hibernate mapping xml file for user class.  In hibernate we need to create hibernate mapping xml file for all domain class for mapping into corresponding database table. Instead of creating xml file you can use annotation for mapping domain class into database table. This is my mapping xml document created for mapping our user domain class into user database table. Step 3: Creating authenticate service class.  The method “verifyUserNameAndPassword” pres...