实现 登陆 验证 和注册 验证在一个 LoginAction 类中:
Login.jsp:
[html] view plaincopy
- <%@ 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>
- <basehrefbasehref="<%=basePath%>">
- <title>Login</title>
- <metahttp-equivmetahttp-equiv="pragma"content="no-cache">
- <metahttp-equivmetahttp-equiv="cache-control"content="no-cache">
- <metahttp-equivmetahttp-equiv="expires"content="0">
- <metahttp-equivmetahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
- <metahttp-equivmetahttp-equiv="description"content="This is my page">
- </head>
- <body>
- <s:formactions:formaction="loginAction"method="post">
- <s:textfieldnames:textfieldname="username"label="用户名"/><br>
- <s:passwordnames:passwordname="password"label="密码"/><br>
- <inputtypeinputtype="submit"value="登陆"><br>
- </s:form>
- </body>
- </html>
LoginAction
[java] view plaincopy
- 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;
- publicclass LoginAction extends ActionSupport implements ModelDriven<User>{
- /**
- *
- */
- privatestaticfinallong serialVersionUID = 1L;
- User user=new User();
- public User getUser() {
- return user;
- }
- publicvoid 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;
- }
- }
#p#
Struts.xml
[html] view plaincopy
- <?xmlversionxmlversion="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>
- <packagenamepackagename="default"extends="struts-default">
- <actionnameactionname="loginAction"class="xuyan.com.action.LoginAction">
- <resultnameresultname="success">/Success.jsp</result>
- <resultnameresultname="error">/Login.jsp</result>
- </action>
- <actionnameactionname="regAction"class="xuyan.com.action.LoginAction"method="Login">
- <resultnameresultname="success">/RegSuccess.jsp</result>
- <resultnameresultname="error">/reg.jsp</result>
- </action>
- </package>
- </struts>
Reg.jsp:
[html] view plaincopy
- <%@ 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>
注意:
[html] view plaincopy
- <actionnameactionname="loginAction"class="xuyan.com.action.LoginAction">
- loginAction与login. JSP页面中的<s:form action="loginAction" method="post">对应
- <action name="regAction" class="xuyan.com.action.LoginAction" method="Login">
- regAction与reg.JSP 页面中的 <s:form action="regAction" method="post">对应
- 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>