实现 登陆 验证 和注册 验证在一个 LoginAction 类中:
Login.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
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>Login</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">
</head>
<body>
<s:form action="loginAction" method="post">
<s:textfield name="username" label="用户名" /> <br>
<s:password name="password" label="密码"/> <br>
<input type="submit" value="登陆"> <br>
</s:form>
</body>
</html>
Reg.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
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 'reg.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">
</head>
<body>
<s:form action="regAction" method="post">
<s:textfield name="username" label="用户名" /> <br>
<s:password name="password" label="密码"/> <br>
<input type="submit" value="注册"> <br>
</s:form>
</body>
</html>
LoginAction:
package xuyan.com.action;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import xuyan.com.model.User;
import xuyan.com.model.UserDAO;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class LoginAction extends ActionSupport implements ModelDriven<User>{
/**
*
*/
private static final long serialVersionUID = 1L;
User user=new User();
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
private Connection con=null;
private ResultSet rs=null;
private PreparedStatement psmt=null;
/**
* 用户注册
*
*/
public String Login()
{
System.out.println(user.getUsername()+"1111");
System.out.println(user.getPassword()+"1111");
UserDAO dao=new UserDAO();
con=dao.getConnection();
try {
psmt =con.prepareStatement("insert into userinfo (username,password) values (?,?) ");
psmt.setString(1, user.getUsername());
psmt.setString(2, user.getPassword());
int a=psmt.executeUpdate();
if(a>0)
{
System.out.println(user.getUsername()+"第2222次");
System.out.println(user.getPassword()+"第2222次");
return SUCCESS;
}
else
{
return ERROR;
}
} catch (SQLException e) {
e.printStackTrace();
return ERROR;
}
finally
{
if(con != null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(psmt != null){
try {
psmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
@Override
public String execute() {
System.out.println(user.getUsername()+"1111");
System.out.println(user.getPassword()+"1111");
UserDAO dao=new UserDAO();
con=dao.getConnection();
try {
psmt =con.prepareStatement("select * from userinfo where username=? and password=?");
psmt.setString(1, user.getUsername());
psmt.setString(2, user.getPassword());
rs=psmt.executeQuery();
if(rs.next())
{
System.out.println(user.getUsername()+"第2222次");
System.out.println(user.getPassword()+"第2222次");
return SUCCESS;
}
else
{
return ERROR;
}
} catch (SQLException e) {
e.printStackTrace();
return ERROR;
}
finally
{
if(con != null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(psmt != null){
try {
psmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public User getModel() {
// TODO Auto-generated method stub
return user;
}
}
Struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="loginAction" class="xuyan.com.action.LoginAction">
<result name="success">/Success.jsp</result>
<result name="error">/Login.jsp</result>
</action>
<action name="regAction" class="xuyan.com.action.LoginAction" method="Login">
<result name="success">/RegSuccess.jsp</result>
<result name="error">/reg.jsp</result>
</action>
</package>
</struts>
注意:
1、
<action name="loginAction" class="xuyan.com.action.LoginAction">
中
loginAction 与login. JSP 页面中的
<s:form action="loginAction" method="post"> 对应
2、
<action name="regAction" class="xuyan.com.action.LoginAction" method="Login">
中 regAction与 reg.JSP 页面中的 <s:form action="regAction" method="post"> 对应
3、
loginAction 类中 public String Login() 方法 必须在 Struts.xml 中声明
<action name="regAction" class="xuyan.com.action.LoginAction" method="Login">
<result name="success">/RegSuccess.jsp</result>
<result name="error">/reg.jsp</result>
</action>
======================================================================
大家可以关注下 爱编程唯一网 www.ibcve.com 里面有更多的编程信息 !