1、index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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=utf-8">
<title>Insert title here</title>
</head>
<body>
<h3>登录页面</h3>
<form action="myLogin.action" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="uname" /></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="upwd" /></td>
</tr>
<tr>
<td><input type="submit" value="提交" /></td>
<td><input type="reset" value="重置" /></td>
</tr>
</table>
</form>
</body>
</html>
2、struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.devMode" value="true"/>
<package name="default" namespace="/" extends="struts-default">
<action name="myLogin" class="action.MyLoginAction">
<result>
/myLoginSuccess.jsp
</result>
<result name="fail">/myLoginFail.jsp</result>
</action>
</package>
</struts>
3、model包User.java
package model;
public class User {
public String uname;
public String Upwd;
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpwd() {
return Upwd;
}
public void setUpwd(String upwd) {
Upwd = upwd;
}
}
4、action包MyLoginAction.java
package action;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import model.User;
public class MyLoginAction extends ActionSupport implements ModelDriven<User>{
private User u = new User();
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
public MyLoginAction() {
request = ServletActionContext.getRequest();
session = request.getSession();
application = ServletActionContext.getServletContext();
}
public String execute() {
if("yjw".equals(u.getUname())
&&"123".equals(u.getUpwd())) {
session.setAttribute("usersession", u);
application.setAttribute("userapplication", u);
return SUCCESS;
}else{
request.setAttribute("userrequest", u);
return "fail";
}
}
@Override
public User getModel() {
return u;
}
}
5、myLoginSuccess.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
从session中取值<br>
用户名:${sessionScope.usersession.uname}<br>
密码:${sessionScope.usersession.upwd}<br>
从application中取值<br>
用户名:${applicationScope.userapplication.uname}<br>
密码:${applicationScope.userapplication.upwd}
</body>
</html>
6、myLoginFail.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
从request中取值<br>
用户名:${requestScope.userrequest.uname}<br>
密码:${requestScope.userrequest.upwd}
</body>
</html>