error.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>标题</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
十分对不起,登录失败,由于:<%=request.getAttribute("msg") %>
</body>
</html>
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>标题</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<form action="login.do" method="post">
用户名:<input type="text" name="username"/><br>
密码:<input type="text" name="password"/><br>
<input type=submit value="登录"/>
</form>
</body>
</html>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>标题</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
恭喜:<%=request.getAttribute("username") %>,登录成功
</body>
</html>
web.xml
<?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"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<!-- 这里配置spring 的后台servlet-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<!-- 指定spring配置文件的路径-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<display-name>
</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
applicationContext.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">
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="login.do">login</prop>
</props>
</property>
</bean>
<bean id="login" class="com.spring.controller.LoginController">
<property name="errorPage">
<value>error.jsp</value>
</property>
<property name="successPage">
<value>success.jsp</value>
</property>
</bean>
</beans>
LoginController
package com.spring.controller;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import com.spring.model.UserInfoBean;
public class LoginController implements Controller
{
private String successPage;
private String errorPage;
public String getSuccessPage()
{
return successPage;
}
public void setSuccessPage(String successPage)
{
this.successPage = successPage;
}
public String getErrorPage()
{
return errorPage;
}
public void setErrorPage(String errorPage)
{
this.errorPage = errorPage;
}
@Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception
{
// TODO Auto-generated method stub
String username = request.getParameter("username");
String password = request.getParameter("password");
String message = null;
if(username == null || password == null ||
username.trim().equals("") || password.trim().equals(""))
{
message = "用户名或密码为空";
Map<String, String> model = new HashMap<String,String>();
model.put("msg", message);
return new ModelAndView(getErrorPage(),model);
}
if(!UserInfoBean.exisitUser(username))
{
message = username + "不存在";
Map<String, String>model = new HashMap<String, String>();
model.put("msg", message);
return new ModelAndView(getErrorPage(),model);
}
if(!UserInfoBean.confirmPassword(username, password))
{
message = username + "密码不正确";
Map<String, String> model = new HashMap<String, String>();
model.put("msg", message);
return new ModelAndView(getErrorPage(),model);
}
else
{
Map<String, String>model = new HashMap<String, String>();
model.put("username", username);
return new ModelAndView(getSuccessPage(),model);
}
}
}
UserInfoBean
package com.spring.model;
import java.util.HashMap;
import java.util.Map;
public class UserInfoBean
{
private static Map<String,String>userinfo = new HashMap<String, String>();
static
{
String numberOneUser = "zhangsan";
String numberOnePassword = "123";
String numberTwoUser = "lisi";
String numberTwoPassword = "456";
userinfo.put(numberTwoUser, numberTwoPassword);
userinfo.put(numberOneUser, numberOnePassword);
}
public static boolean exisitUser(String username)
{
return userinfo.containsKey(username);
}
public static boolean confirmPassword(String username,String password)
{
return userinfo.get(username).equals(password);
}
}