一、安装struts
http://struts.apache.org/download.cgi 下载最新版本的jakarta-struts-1.2.4.zip解压
我们用到的jar包
lib目录下所有jar包 拷贝到WEB-INF/lib 文件夹下。
二、配置
webapp\WEB-INF\web.xml
webapp\WEB-INF\struts-config.xml
三、程序
webapp\WEB-INF\src\struts\ LoginForm.java
webapp\WEB-INF\src\struts\ LoginAction.java
webapp\struts\login.jsp
webapp\struts\ succeed.jsp
webapp\WEB-INF\src\struts\ LoginForm.java
***************************************************************
package struts;
import org.apache.struts.action.ActionForm;
public class LoginForm extends ActionForm {
private String username;
private String password;
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return this.username;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return this.password;
}
}
***************************************************************
webapp\WEB-INF\src\struts\ LoginAction.java
***************************************************************
package struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class LoginAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
boolean isSucceed = false;
String msg = "";
String username = ((LoginForm) form).getUsername();
String password = ((LoginForm) form).getPassword();
if (username != null) {
System.out.println("登陆处理开始.........");
username = username.trim();
password = password.trim();
if (username.equals("") && username.equals("")) {
isSucceed = false;
msg = "用户名或密码为空";
} else {
isSucceed = true;
}
if (isSucceed) {
if (username.equals("zkjbeyond")
&& password.equals("zkjbeyond")) {
isSucceed = true;
HttpSession session = request.getSession();
msg = "登陆成功";
session.setAttribute("username", username);
} else {
isSucceed = false;
msg = "用户名密码不正确";
}
}
} else {
isSucceed = false;
msg = "请输入用户名密码";
}
System.out.println(username);
System.out.println(msg + isSucceed);
System.out.println("登陆处理结束.........");
if (isSucceed) {
return mapping.findForward("succeed");
} else {
request.setAttribute("msg", msg);
return mapping.findForward("error");
}
}
}
***************************************************************
webapp\struts\login.jsp
***************************************************************
<%@ page language="java" pageEncoding="GB2312" %>
<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>Lomboz JSP</title>
</head>
<%
//request.setCharacterEncoding("GBK");
Object obj=request.getAttribute("msg");
String msg="";
if(obj!=null)
msg=obj.toString();
%>
<body bgcolor="#FFFFFF">
<form action="login.struts" method="post">
<div>
<span style="width:70;">用户名:</span>
<span><input type="text" name="username"></span>
</div>
<div>
<span style="width:70;">密 码:</span>
<span><input type="password" name="password"></span>
</div>
<div>
<span style="width:70;"></span>
<span><input type="submit"></span>
</div>
<div style="color:red;">
<%
if(!msg.equals(""))
out.println(msg);
%>
</div>
</form>
</body>
</html>
***************************************************************
webapp\struts\ succeed.jsp
***************************************************************
<%@ page language="java" pageEncoding="GB2312" %>
<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>succeed</title>
</head>
<body bgcolor="#FFFFFF">
<%
Object username=session.getAttribute("username");
if(username!=null)
out.println("用户名为:"+username);
else
response.sendRedirect("login.jsp");
%>
</body>
</html>
***************************************************************
http://localhost:8080/webapp/struts/login.jsp 访问
本文地址:http://com.8s8s.com/it/it12019.htm