1、配置struts
* 拷贝struts lib下的所有jar到WEB-INF/lib下
* 修改web.xml文件,配置ActionServlet
* 提供struts-config.xml文件
3、创建登录项目
* 创建jsp页面(login.jsp,login_success.jsp,login_error.jsp)
* 创建LoginActionForm.java
* 创建LoginAction.java
* 配置struts-config.xml文件
index.jsp
login.jsp
login_error.jsp
login_success.jsp
LoginAction.java
LoginActionForm.java
UserManager.java
PasswordErrorException.java
UserNotFoundException.java
Web.xml
struts-config.xml
* 拷贝struts lib下的所有jar到WEB-INF/lib下
* 修改web.xml文件,配置ActionServlet
* 提供struts-config.xml文件
3、创建登录项目
* 创建jsp页面(login.jsp,login_success.jsp,login_error.jsp)
* 创建LoginActionForm.java
* 创建LoginAction.java
* 配置struts-config.xml文件
index.jsp
<!<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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>My JSP 'index.jsp' starting page</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<a href="login.jsp">登录</a>
</body>
</html>
login.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>用户登录</title>
</head>
<body>
<h1>用户登录</h1>
<hr>
<form action="login.do" method="post">
用户:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
login_error.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>登录失败</title>
</head>
<body>
<%=request.getAttribute("errorinfo") %>
</body>
</html>
login_success.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ page import="com.bjsxt.struts.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>登录成功</title>
</head>
<body>
<%
LoginActionForm laf = (LoginActionForm)request.getAttribute("loginForm");
%>
<%=laf.getUsername() %>,登录成功
</body>
</html>
LoginAction.java
package com.bjsxt.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
* 用户登录的Action
* @author Administrator
*
*/
public class LoginAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
LoginActionForm laf = (LoginActionForm)form;
String username = laf.getUsername();
String password = laf.getPassword();
// if ("admin".equals(username) && "admin".equals(password)) {
// //转向到登录成功页面
// request.setAttribute("username", username);
// return mapping.findForward("success");
// }else {
// //转向到登录失败页面
// return mapping.findForward("error");
// }
String errorInfo = "";
try {
UserManager.getInstance().login(username, password);
//request.setAttribute("username", username);
return mapping.findForward("success");
}catch(UserNotFoundException unfe) {
unfe.printStackTrace();
errorInfo = "用户不能找到,用户名称=[" + username + "]";
}catch(PasswordErrorException pee) {
pee.printStackTrace();
errorInfo = "密码错误";
}
request.setAttribute("errorinfo", errorInfo);
return mapping.findForward("error");
}
}
LoginActionForm.java
package com.bjsxt.struts;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
/**
* 登录的ActionForm,ActionForam是做数据收集的,
*
* ActionForm中的属性必须和表单中输入域的名称一致
* @author Administrator
*
*/
public class LoginActionForm extends ActionForm {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public void reset(ActionMapping mapping, HttpServletRequest request) {
System.out.println("----------LoginActionForm.reset()-----------");
}
@Override
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
System.out.println("----------LoginActionForm.validate()-----------");
return null;
}
}
UserManager.java
package com.bjsxt.struts;
public class UserManager {
private static UserManager instance = new UserManager();
private UserManager() {}
public static UserManager getInstance() {
return instance;
}
public void login(String username, String password) {
if (!"admin".equals(username)) {
throw new UserNotFoundException();
}
if (!"admin".equals(password)) {
throw new PasswordErrorException();
}
}
}
PasswordErrorException.java
package com.bjsxt.struts;
public class PasswordErrorException extends RuntimeException {
}
UserNotFoundException.java
package com.bjsxt.struts;
public class UserNotFoundException extends RuntimeException {
}
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- Standard Action Servlet Configuration (with debugging) -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="loginForm" type="com.bjsxt.struts.LoginActionForm"/>
</form-beans>
<action-mappings>
<action path="/login"
type="com.bjsxt.struts.LoginAction"
name="loginForm"
scope="request"
validate="false"
>
<forward name="success" path="/login_success.jsp"/>
<forward name="error" path="/login_error.jsp"/>
</action>
</action-mappings>
</struts-config>