Here I am explaining Simple Login Application in Springs
TestController.java
package com.springs;
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestController implements Controller {
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws Exception {
String name=request.getParameter("name");
String pwd =request.getParameter("pwd");
Map map=new HashMap();
map.put("name",name);
map.put("pwd",pwd);
return new ModelAndView("success.jsp", "map", map);
}
}
Test-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="TestController" class="com.springs.TestController"/>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/success.htm">TestController</prop>
</props>
</property>
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' 'http://java.sun.com/dtd/web-app_2_3.dtd'>
<web-app>
<servlet>
<servlet-name>Test</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
</web-app>
login.jsp
<html>
<body>
<form method="post" action="success.htm">
<h3><u>Login Form</u></h3><br/>
Name::<input type="text" name="name" value=""/><br/><br/>
Password::<input type="password" name="pwd" value=""/><br/><br/>
<input type="submit" value="Submit"/><br/>
</form>
</body>
</html>
success.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<html>
<body>
<c:choose>
<c:when test="${map.name == 'akbar' && map.pwd == 'santhi'}">
<h3> <c:out value="Successfully Login"/></h3>
</c:when>
<c:otherwise>
<h3> <c:out value="Login Failure"/></h3>
</c:otherwise>
</c:choose>
</body>
</html>
No comments:
Post a Comment