Gadgets

Friday, 4 March 2016

Spring MVC Login application

Spring MVC Login application

In this page, I have given spring MVC login application step by step.


Steps to create spring MVC web application:

1.      Load the required jar file.
2.      Create the request page to give input.
3.      Create the Controller class.
4.      Create the web.xml file.
5.      Create the spring configuration xml file.
6.      Create output page
7.      Run the project.



Load the required jar files:
Note: load the jar files in lib folder also
·        commons-logging-1.1.1
·        org.springframework.asm-3.0.1
·        org.springframework.beans-3.0.1
·        org.springframework.context-3.0.1
·        org.springframework.core-3.0.1
·        org.springframework.expression-3.0.1
·        org.springframework.web-3.0.1
·        org.springframework.web.servlet-3.0.1
Create the request page to give input: This file contains input fields.
index.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Login Page</h1>
<form action="./loginform.html" method="post">
<p>username : narendar and password : admin123</p>
Name : <input type="text" name="name"/><br>
Password : <input type="password" name="password"/><br>
<input type="submit" value="submit"/>
</form>
</body>
</html>
Create the Controller class: in this file we get the details from input page and send the result to success page.
LoginController.java
package com.javathub;
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 req,
                     HttpServletResponse res) throws Exception {           
              String name=req.getParameter("name");
              String password=req.getParameter("password");
              if(password.equals("admin123")&&name.equals("narendar"))
              {
                     String msg="Hello...welcome..."+name;                 
                     return new ModelAndView("homepage","message",msg);
              }
              else{
                     return new ModelAndView("errorpage","message","incorrect username and password, please try again");
              }            
       }
}
Create the web.xml file: in this file we are specifying the DispatcherServlet, it is act as front controller. This file forwards the input requests to DispatcherServlet.
<?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" 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>springMVC_login</display-name>
  <welcome-file-list>  
    <welcome-file>index.jsp</welcome-file>   
  </welcome-file-list>
  <servlet>
  <servlet-name>login</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet> 
  <servlet-mapping>
  <servlet-name>login</servlet-name>
  <url-pattern>*.html</url-pattern>
  </servlet-mapping>
</web-app>
Create the spring configuration xml file: this file name should be (what we specified in web.xml)servletname-servlet.xml, here we define the handler mapping class, Controller class and configure the view resolver.
login-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<!--default handler mapping -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
</bean>
<!-- define controller name -->
<bean name="/loginform.html" class="com.javathub.LoginController"></bean>
<!-- view resolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>

Output page:

homepage.jsp
${message}
errorpage.jsp

${message}
<jsp:include page="index.jsp"></jsp:include>

No comments:

Post a Comment