Spring mvc login tutorial

Let us discuss spring mvc login example in eclipse.

Example Explanation:

Use http://localhost:8080/SpringMVCExample4/ url to start the application. Enter username “jai” and password “123”. Click on login button. Request will be handled by DispatcherServlet. It delegates the request to the LoginController controller. The LoginController controller resolve the request with help of RequestMapping annotation, executes the specific functionality and returns the ModelAndView object to the DispatcherServlet. The DispatcherServlet then take the help of InternalResourceViewResolver to get the actual view name. In our example it will return the “/WEB-INF/jsp/welcome.jsp” if username and password are correct otherwise returns “/WEB-INF/jsp/errorPage.jsp”. The DispatcherServlet then insert the model data into view and render response.

Note: We are using HttpServletRequest and HttpServletResponse in request processing method to get the form parameters.

Example:

index.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Spring MVC login example.</title>
  </head>
 
  <body>
    <form action="login.html" method="post">  
    UserName:<input type="text" name="userName"/>
    <br/><br/>  
    Password:<input type="password" name="password"/>
    <br/><br/>  
    <input type="submit" value="login"/>  
    </form>  
  </body>
</html>

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>
      <servlet-name>Login</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>
 
   <servlet-mapping>
      <servlet-name>Login</servlet-name>
      <url-pattern>*.html</url-pattern>
   </servlet-mapping>
 
</web-app>

Login-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   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-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
   <context:component-scan base-package="com.w3schools.business" />
 
   <bean class=
      "org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
   </bean>
 
</beans>

LoginController.java

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
 
@Controller
public class LoginController {
   @RequestMapping("/login")  
   public ModelAndView login(HttpServletRequest request,
		   HttpServletResponse response) {
	  String userName=request.getParameter("userName");  
      String password=request.getParameter("password");
      String message;
      if(userName != null && 
    		  !userName.equals("") 
    		  && userName.equals("jai") && 
    		  password != null && 
    		  !password.equals("") && 
    		  password.equals("123")){
    	  message = "Welcome " +userName + ".";
	      return new ModelAndView("welcome", 
	    		  "message", message);  
 
      }else{
    	  message = "Wrong username or password.";
    	  return new ModelAndView("errorPage", 
    			  "message", message);
      }
   }
}

welcome.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Spring MVC login example.</title>
  </head>
  <body>
    <h2>${message}</h2>
  </body>
</html>

errorPage.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Spring MVC login example.</title>
  </head>
  <body>
    <h2>${message}</h2><br/>
    <jsp:include page="/index.jsp"></jsp:include>  
  </body>
</html>

Output:

Spring example3-1
Enter Username = Jai and Password = 123
Spring example3-2
Click on login button.
Spring example3-3
 
Download this example.