In this post we will see how to create a login page using spring 3. Below two screen shots
are the directory structure and the jar files that are necessary for to run the login
application.
File: WebContent/index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Spring Login Page Example</title> </head> <body> <a href="forms/loginform.html">Click here for Login Page</a> </body> </html>
File: com.ram.form.LoginForm
package com.ram.form; public class LoginForm { private String userName; private String password; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
File: WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Spring Login Page Example</display-name> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/forms/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
File: com.ram.controllers.LoginForm.java
package com.ram.controllers; import java.util.Map; import javax.validation.Valid; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.ram.form.LoginForm; @Controller @RequestMapping("loginform.html") public class LoginController { @RequestMapping(method = RequestMethod.GET) public String showForm(Map model) { LoginForm loginForm = new LoginForm(); model.put("loginForm", loginForm); return "loginform"; } @RequestMapping(method = RequestMethod.POST) public String processForm(@Valid LoginForm loginForm, BindingResult result, Map model) { String userName = "Admin"; String password = "root"; if (result.hasErrors()) { return "loginform"; } loginForm = (LoginForm) model.get("loginForm"); if (!loginForm.getUserName().equals(userName) || !loginForm.getPassword().equals(password)) { return "loginerror"; } model.put("loginForm", loginForm); return "loginsuccess"; } }
File: WEB-INF/dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- Enable annotation driven controllers, validation etc... --> <mvc:annotation-driven /> <context:component-scan base-package="com.ram.controllers" /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basename" value="/WEB-INF/messages" /> </bean> </beans>
File: WEB-INF/views/loginform.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Spring Login Page</title> </head> <body> <center> <h3>Login Page</h3> <form:form action="loginform.html" commandName="loginForm"> <table> <tr> <td>User Name: <FONT color="red"><form:errors path="userName" /></FONT></td> <td><form:input path="userName" /></td> </tr> <tr> <td>Password: <FONT color="red"><form:errors path="password" /></FONT></td> <td><form:password path="password" /></td> </tr> </table> <p><input type="submit" value="Submit" /> </p> </form:form> </center> </body> </html>
File: WEB-INF/views/loginsuccess.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="core" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Spring Success Page</title> </head> <body> <center> <h4> Welcome to the <core:out value="${loginForm.userName}" /> page. </h4> <p> <a href="loginform.html">Back</a> </p> </center> </body> </html>
File: WEB-INF/views/loginerror.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Spring Error Page</title> </head> <body> <center> <h4 style="color: red">Error page, please go back to login page and enter correct credentials</h4> <p> <a href="loginform.html">Retry</a> </p> </center> </body> </html>
Right click. on the "SpringLoginPageExample directory structure and click on "Run As -> Run on Server".
Once the login page appeared, enter the credentials "Admin" and "root" to successfully login. If incorrect
credentials are given, then error page will be thrown.
same code but not running
ReplyDeleteCheck if you have all the required jars. It would be helpful if you provide me with the stack trace. Give me your mail id and I will send you the code.
Delete