Spring Programing laungage

Spring Projects

Spring Project 1

adplus-dvertising
Login Example in Spring MVC
Previous Home Next

Directory Structure of Spring MVC application is given below:

step 1:

Create index.jsp file

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Login Example in Spring MVC</title>
  </head>
  
  <body>
    <form action="login.bean">
    Name:-<input type="text" name="name"/><br/>
    Password:-<input type="password" name="password"/><br/>
    <input type="Submit" value="Login"/>
    </form>
  </body>
</html>

step 2:

Create web.xml file

<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>Spring MVC Application</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.bean</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>	
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

step 3:

Create dispatcher-servlet.xml file

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<bean id="viewResolver"
class
="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean> 
<bean id="loginController" class="com.r4r.LoginController" />
<bean class
="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/login.bean">loginController</prop>
</props>
</property>
</bean>
</beans>

step 4:

Create LoginController.java file

package com.r4r;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class LoginController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
String name=request.getParameter("name");
String password=request.getParameter("password");
if(name.equals("Mukund")&& password.equals("Singh")){
ModelAndView modelSuccess=new ModelAndView("loginPage");
modelSuccess.addObject("msgSuccess", name);
return modelSuccess;
}else{
ModelAndView modelError=new ModelAndView("errorPage");
modelError.addObject("msgError", "Invalid UserName and Password");
return modelError;
}
}
}

step 5:

Create loginPage.jsp file

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<h4>You are successfully Login</h4>
<h5>Welcome, <c:out value="${msgSuccess}"/></h5>

step 6:

Create errorPage.jsp file

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<h4><c:out value="${msgError}"/></h4>
<h4>Please try again</h4>

Output:

Previous Home Next