Spring Login Example

In this section , we have shown a simple Spring loging Example. In this example, we have accepted two values Username and password. We have created Logger.java and declared two private variables username and password than we have created setter getter method. Now create UserLogin.java. This class controls login request by bean properties

Spring Login Example

Spring Login Example

In this section , we have shown a simple Spring loging Example. In this example, we have accepted two values Username and password. We have created Logger.java and declared two private variables username and password than we have created setter getter method. Now create UserLogin.java. This class controls login request by bean properties Than we create dispatcher-servlet.xml to handle all bean configuration. After this we create a web.xml file to handle all DispatcherServlet. Than we create jsp page table.jsp and have defined two text fields and one button. You  can download this example directly and run it in your console/eclipse.

Following is the Simple Spring Login Example:

  1. Logger.java
  2. UserLogin.java
  3. dispatcher-servlet.xml
  4. web.xml
  5. table.jsp
  6. TableView.jsp
  7. index.jsp

This video tutorial is made using the Spring Framework 2.5 and you will learn to make form in Spring MVC. Following is the links of the similar tutorial develop using the latest version of Spring MVC:

Above are the latest tutorials of Spring MVC Login form.

Following video instruction teaches you how to download and run the code of this example from Eclipse IDE. Eclipse IDE and Tomcat server is required to run this program.

Video Tutorial: How to create Spring MVC Login Form? 

Logger.java

package roseindia.model;

public class Logger {
	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;
	}

}

UserLogin.java

package roseindia.SpringController;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import roseindia.model.Logger;


@Controller
public class UserLogin{
	@RequestMapping("/load-form.html")
	public String loadForm(Model model, Logger field) {
		model.addAttribute("field", field);
		return "table";
	}

	@RequestMapping("/process-form.html")
	public String processForm(Model model, Logger field) {
		model.addAttribute("field", field);
		return "TableView";
	}
}

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">

<context:component-scan base-package="roseindia.SpringController" />
<bean id="viewReslover"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/view/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>

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">
<display-name>SpringWebExample</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>*.html</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

table.jsp

<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<title>My Form table</title>
</head>
<body>
<form:form commandName="field" action="process-form.html">
<table cellpadding="5" cellspacing="5">
<tr>
<td>UserName</td>
<td><form:input path="UserName" /></td>
</tr>

<tr>
<td>Password</td>
<td><form:password path="Password" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Login" /></td>
</tr>
</table>
</form:form>
</body>
</html>

TableView.jsp

<H1 align="center">Welcome Roseindia.net</H1>
UserName:- ${field.userName}<br/>
Password:- ${field.password}<br/></pre>

index.jsp

<%
response.sendRedirect("load-form.html");
%>

Output

When you will enter username and password into the respective field and then if you click on login button then the output will be as follows :

Updated Source code: Download the Source code in Eclipse Project format.

Download Source Code

Download required jar files for this example