1、工程目录:

2、工程用到的jar包有:

3、数据库用户表tb_user:

4、web.xml的配置:<?xml version="1.0" encoding="UTF-8" standalone="yes" ?><web-app 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/web-app_2_4.xsd"version="2.4"><filter> <filter-name>struts2</filter-name> <!-注意新版本的struts要按下面这样来配置--> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list></web-app>
5、struts.xml的配置:<?xml version="1.0" encoding="炽扃仄呦UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <package name="com" extends="struts-default"> <action name="login" class="com.UserAction" method="login"> <result name="loginout">/error.jsp</result> <result name="loginin">/welcome.jsp</result> </action> <action name="regist" class="com.UserAction" method="regist"> <result name="error">/error.jsp</result> <result name="success">/welcome.jsp</result> </action> </package></struts>
6、数据库Dao.javapackage dao;import java.sql.*;public class Dao { Connection con = null; Statement stat = null; ResultSet rs = null; public Dao() { try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_vote","root","root"); stat = con.createStatement(); } catch (Exception e) { // TODO: handle exception con = null; } } public ResultSet executeQuery(String sql) { try { rs = stat.executeQuery(sql); } catch (Exception e) { // TODO: handle exception rs = null; } return rs; } public int executeUpdate(String sql) { try { stat.executeUpdate(sql); return 0; } catch (Exception e) { // TODO: handle exception } return -1; }}
7、UserAction.javapackage com;import java.sql.*;import com.opensymphony.xwork2.ActionSupport;import dao.Dao;@SuppressWarnings("serial")public class UserAction extends ActionSupport{ private Dao dao = new Dao(); private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String login() { String sql = "select * from tb_user where username='" + getUsername()+"' and password ='"+getPassword()+"'"; ResultSet rS = dao.executeQuery(sql); try { if (rS.next()) { return "loginin"; } return "loginout"; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return "loginout"; } } public String regist() { String sql = "insert into tb_user(username,password,isCheck) values('"+getUsername()+"','"+getPassword()+"','0')"; int i = dao.executeUpdate(sql); if (i > -1) { return "success"; } return "error"; } //通用的执行方法// public String execute() throws Exception {// if (getUsername().equals("scott") && getPassword().equals("tiger")) {// System.out.println("我是success");// return "success";// }// // else {// System.out.println("我是error");// return "error";// }// }}
8、用户注册界面regist.jsp<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!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=UTF-8"><title>Insert title here</title></head><body><center><form action="regist.action" method="post"> <table> <caption><h2>用户登录</h2></caption> <tr> <td>username: </td> <td><input type="text" name="username" /></td> </tr> <tr> <td>password: </td> <td><input type="password" name="password" /></td> </tr> <tr> <td><input type="submit" name="submit" /></td> </tr> </table> </form></center></body></html>
9、登录界面login.jsp<%@ page language="java争犸禀淫" contentType="text/html; charset租涫疼迟=UTF-8" pageEncoding="UTF-8"%><!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=UTF-8"><title>Insert title here</title></head><body><form action="login.action" method="post"> <table align="center"> <caption><h2>用户登录</h2></caption> <tr> <td>用户名:<input type="text" name="username" /></td> </tr> <tr> <td>密码: <input type="text" name="password"/></td> </tr> <tr align="center"> <td colspan="2"><input type="submit"/></td> </tr> </table></form></body></html>
10、操作错误页面error.jsp<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags" %><!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=UTF-8"><title>Insert title here</title></head><body><center> 执行失败!!</center></body></html>
11、操作成功界面welcome.jsp<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!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=UTF-8"><title>Insert title here</title></head><body>您已经执行成功!</body></html>