1、在Tomcat中建立一个空站点
2、将必须的包放入WEB-INF/lib下,有
jstl.jar
standard.jar
commons-beanutils-1.8.0.jar
commons-chain-1.2.jar
commons-digester-1.8.jar
struts-core-1.3.10.jar
3、修改web.xml文件,如:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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">
<display-name>webapp</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<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>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
4、新建登录网页index.jsp,内容为:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!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>
<form action="login.do" method="POST">
<input type="text" name="loginName">
<input type="password" name="loginPass">
<input type="submit" value="submit" >
</body>
</html>
5、创建LoginForm.java,内容为:
package app;
import org.apache.struts.action.ActionForm;
public class LoginForm extends ActionForm {
String loginName;
String loginPass;
public LoginForm() {
}
public LoginForm(String user, String pass) {
setLoginName(user);
setLoginPass(pass);
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getLoginPass() {
return loginPass;
}
public void setLoginPass(String loginPass) {
this.loginPass = loginPass;
}
}
编译方法为(将struts-core-1.3.10.jar文件放入当前目录):
javac -d . -classpath .;struts-core-1.3.10.jar LoginForm.java
将编译后的包类放入WEB-INF/classes
6、创建LoginAction.java,内容为:
package app;
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;
public class LoginAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
LoginForm lf = (LoginForm) form;
if (lf.getLoginName().trim().equals(lf.getLoginPass().trim()))
return mapping.findForward("success");
else
return mapping.findForward("failure");
}
}
编译方法为(将struts-core-1.3.10.jar和servlet-api.jar文件放入当前目录):
javac -d . -classpath .;struts-core-1.3.10.jar;servlet-api.jar LoginAction.java
将编译后的包类放入WEB-INF/classes
7、新建struts-config.xml,放入WEB-INF目录下,内容为:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config SYSTEM "http://struts.apache.org/dtds/struts-config_1_3.dtd" >
<struts-config>
<form-beans>
<form-bean name="loginForm" type="app.LoginForm" />
</form-beans>
<action-mappings>
<action path="/login" type="app.LoginAction"
name="loginForm" validate="false" scope="request">
<forward name="success" path="/success.html" />
<forward name="failure" path="/failure.html" />
</action>
</action-mappings>
</struts-config>
8、创建success.html文件,内容为:
<HTML>
<HEAD>
<TITLE>SUCCESS</TITLE>
</HEAD>
<BODY>
Registration succeeded!
<P><A href="index.jsp">try another?</A></P>
</BODY>
</HTML>
9、创建failure.html文件,内容为:
<HTML>
<HEAD>
<TITLE>FAILURE</TITLE>
</HEAD>
<BODY>
Registration failed!
<P><A href="index.jsp">try again?</A></P>
</BODY>
</HTML>
10、即可运行,因为Tomcat自动缓存,所以每次修改后要及时将<Tomcat>/work目录下的内容清空