web.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
登錄頁面代碼
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%
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>
<script type="text/javascript">
function check(){
var username=document.getElementById("userName").value;
var password=document.getElementById("passWord").value;
if(username.length<=0){
alert("用戶名不能爲空");
return false;
}
if(password.length<=0){
alert("密碼不能爲空");
return false;
}
if(username.valueOf()==" "){
alert("用戶名不能爲空格")
return false;
}
if(password.valueOf()==" "){
alert("密碼不能爲空格")
return false;
}
from1.submit();
}
function checks(){
var username=document.getElementById("userName").value;
var password=document.getElementById("passWord").value;
if(username.length<=0){
alert("用戶名不能爲空");
return false;
}
if(password.length<=0){
alert("密碼不能爲空");
return false;
}
if(username.valueOf()==" "){
alert("用戶名不能爲空格")
return false;
}
if(password.valueOf()==" "){
alert("密碼不能爲空格")
return false;
}
alert("驗證通過")
document.loginForm.submit();
}
</script>
<body>
<form name="from1" action="${pageContext.request.contextPath}/login.do?operate=login" method="post" >
username:<input type="text" name="userName"/><br>
password:<input type="text" name="passWord"><br>
<input type="button" name="button" value="tijiao" οnclick="check()">
</form>
<html:form action="/login.do?operate=login" method="post">
username:<html:text property="userName" ></html:text>
password:<html:password property="passWord"></html:password>
<input type="button" name="button" value="提交" οnclick="checks()">
</html:form>
</body>
</html>
action 代碼
public ActionForward login(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LoginForm userForm = (LoginForm) form;// TODO Auto-generated method stub
request.setAttribute("login", userForm);
System.out.println(userForm.getPassWord());
HttpSession session=request.getSession();
request.setAttribute("list", listadd());
session.setAttribute("users", userForm);
if(userForm.getUserName().equals("quwei")){
return mapping.findForward("ok");
}else{
return mapping.findForward("cuowu");
}
}
struts-config.xml
<action-mappings >
<action
attribute="loginForm"
name="loginForm"
parameter="operate"
path="/login"
scope="request"
type="com.cardvalue.view.action.LoginAction">
<set-property property="cancellable" value="true" />
<forward name="cuowu" path="/cuowu.jsp" />
<forward name="ok" path="/ok.jsp" />
</action>