Spring MVC Login Example:-
The following example show how to write a simple web based Login
application using Spring
MVC framework. To start with it,
let us have working Eclipse
IDE in place and follow the
following steps to developed a Dynamic
Web Application using Spring Web
Framework:
1 Create a Dynamic
Web Project with a name SpringMVClogin and
create a package r4r.in under
the srcfolder
in the created project.
2 AddSpring and other libraries into the folder WebContent/WEB-INF/lib.
3 Create a Java class HelloWorldController under the r4r.in package.
4 Create Spring configuration files Web.xml and spring-servlet.xml under theWebContent/WEB-INF folder.
5 Create a sub-folder with a name jsp under the WebContent/WEB-INF folder. Create a view file index.jsp, hello.jsp and error.jsp under this sub-folder.
6 The final step is to create the content of all the source and configuration files and export the application as explained below.
Following are the list of JAR files required for this application.
1 commons-logging-1.0.4.jar
2 jstl-1.2.jar
3 org.springframework.asm-3.0.1.RELEASE-A.jar
4 org.springframework.beans-3.0.1.RELEASE-A.jar
5 org.springframework.context-3.0.1.RELEASE-A.jar
6 org.springframework.core-3.0.1.RELEASE-A.jar
7 org.springframework.expression-3.0.1.RELEASE-A.jar
8 org.springframework.web.servlet-3.0.1.RELEASE-A.jar
9 org.springframework.web-3.0.1.RELEASE-A.jar
10 com.springsource.org.apache.commons.fileupload-1.2.0.jar
11 com.springsource.org.apache.commons.httpclient-3.1.0.jar
12 com.springsource.org.apache.commons.logging-1.1.1.jar
13 com.springsource.org.apache.log4j-1.2.15.jar
14 com.springsource.org.codehaus.jackson.mapper-1.0.0.jar
15 jmxtools-1.2.1.jar
16 org.springframework.oxm-3.0.1.RELEASE-A.jar
17 org.springframework.web.portlet-3.0.1.RELEASE-A.jar
18 servlet-api-3.0.jar
1 Now Here is the content of HelloWorldController.java file:
package r4r.in;
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 HelloWorldController {
@RequestMapping("/hello")
public ModelAndView helloWorld(HttpServletRequest request,HttpServletResponse res) {
String name=request.getParameter("name");
String password=request.getParameter("password");
if(password.equals("admin")){
String message = "Hello Spring MVC LogIn "+name;
return new ModelAndView("hellopage", "message", message);
}
else{
return new ModelAndView("errorpage", "message","Sorry, username or password Incorrect");
} } }
2 Following is the content of Spring Web configuration file web.xml.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
3 Following is the content of another Spring Web configuration file spring-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"
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="r4r.in" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
4 Following is the content of Spring view file index.jsp.
<html>
<head>
<title>Spring MVC Series: Index- r4r.in</title>
</head>
<body>
<form action="hello.html" method="post">
Name:<input type="text" name="name"/><br/>
Password:<input type="password" name="password"/><br/>
<input type="submit" value="login"/>
</form>
</body>
</html>
5 Following is the content of Spring view file hello.jsp.
<html>
<head>
<title>Spring MVC Series: LogIn - r4r.in</title>
</head>
<body>
Message is: ${message}
</body>
</html>
6 Following is the content of Spring view file error.jsp.
<html>
<head>
<title>Spring MVC Series: Error - r4r.in</title>
</head>
<body>
${message}
<jsp:include page="index.jsp"></jsp:include>
</body>
</html>
6 Output: Message is: Hello Spring MVC LogIn abc