(本学期软件工程项目开发经验)eclipse环境下struts2和mysql结合的登录验证

时间:2021-11-01 05:49:18

环境:

[plain] view plain copyprint?
  1. <span style="font-family:Microsoft YaHei;font-size:14px;">JDK:1.8  
  2. IDE:NetBeans 8.0.2  
  3. struts:2.3.15  
  4. MySQL:5.6.26</span>  
(本学期软件工程项目开发经验)eclipse环境下struts2和mysql结合的登录验证


1. 在MySQL中创建相应的数据库和表,提供初始的测试数据

[sql] view plain copyprint?
  1. -- 创建数据库  
  2. CREATE DATABASE `attendance`;  
  3. -- 创建表  
  4. CREATE TABLE `admin` (  
  5.   `id` varchar(10) NOT NULL,  
  6.   `pwd` varchar(20) NOT NULL,  
  7.   PRIMARY KEY (`id`)  
  8. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;  
  9. -- 插入记录,测试用户名"admin",密码"admin"  
  10. INSERT INTO attendance.admin(id,pwd) values('admin','admin');  
(本学期软件工程项目开发经验)eclipse环境下struts2和mysql结合的登录验证


2. 编写数据库连接类SQLCon

[java] view plain copyprint?
  1. package SQLHelper;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.DriverManager;  
  5. import java.sql.SQLException;  
  6.   
  7. /** 
  8.  * @author falleyes 
  9.  */  
  10. public class SQLCon {  
  11.   
  12.     // 连接实例  
  13.     private static Connection conn = null;  
  14.     //连接地址  
  15.     String url = "jdbc:mysql://localhost:3306/mysql";  
  16.     // MySQL用户名  
  17.     String user = "sa";  
  18.     // MySQL密码  
  19.     String password = "mysql";  
  20.   
  21.     public SQLCon() throws Exception {  
  22.         Class.forName("com.mysql.jdbc.Driver");  
  23.         conn=DriverManager.getConnection(url,user,password);  
  24.     }  
  25.       
  26.     //获得连接对象  
  27.     public static Connection getConnection(){  
  28.         return conn;  
  29.     }  
  30.       
  31.     //关闭连接  
  32.     public static void CloseCon() throws SQLException{  
  33.         conn.close();  
  34.     }  
  35. }  
(本学期软件工程项目开发经验)eclipse环境下struts2和mysql结合的登录验证


3. 设计网站的结构

(本学期软件工程项目开发经验)eclipse环境下struts2和mysql结合的登录验证


4. 配置web.xml并且编写index.jsp页面(该页面只是个跳转页面)

[html] view plain copyprint?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">  
  3.     <filter>  
  4.         <filter-name>struts2</filter-name>  
  5.         <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  
  6.     </filter>  
  7.     <filter-mapping>  
  8.         <filter-name>struts2</filter-name>  
  9.         <url-pattern>/*</url-pattern>  
  10.     </filter-mapping>  
  11.     <session-config>  
  12.         <session-timeout>  
  13.             30  
  14.         </session-timeout>  
  15.     </session-config>  
  16.     <welcome-file-list>  
  17.         <welcome-file>index.jsp</welcome-file>  
  18.     </welcome-file-list>  
  19. </web-app>  
(本学期软件工程项目开发经验)eclipse环境下struts2和mysql结合的登录验证
[html] view plain copyprint?
  1. <%--   
  2.     Document   : index.jsp  
  3. --%>  
  4.   
  5. <%@page contentType="text/html" pageEncoding="UTF-8"%>  
  6.   
  7. <!DOCTYPE html>  
  8. <html>  
  9.     <head>  
  10.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  11.         <title>JSP Page</title>  
  12.         </style>  
  13.     </head>  
  14.     <body>  
  15.         <jsp:forward page="Login/LoginJSP.jsp"/>  
  16.     </body>  
  17. </html>  
(本学期软件工程项目开发经验)eclipse环境下struts2和mysql结合的登录验证


5. 设计编写登录页面和相应结果的跳转页面

[html] view plain copyprint?
  1. <%--   
  2.     Document   : LoginJSP.jsp  
  3. --%>  
  4.   
  5. <%@page contentType="text/html" pageEncoding="UTF-8"%>  
  6. <%@taglib prefix="s" uri="/struts-tags" %>  
  7.   
  8. <!DOCTYPE html>  
  9. <html>  
  10.     <head>  
  11.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  12.         <title>JSP Page</title>  
  13.         <style type="text/css">  
  14.             #MainLogin {  
  15.                 position: absolute;  
  16.                 width: 500px;  
  17.                 top: 30%;  
  18.                 left: 50%;  
  19.                 font-size:24px;  
  20.                 margin-left:-250px;  
  21.             }  
  22.         </style>  
  23.     </head>  
  24.     <body>  
  25.         <div class="MainLogin" id="MainLogin">  
  26.             <s:form action="LoginAction">  
  27.                 <table>  
  28.                     <tr>  
  29.                         <td  style="height:30px; text-align:center; font-size:24px; ">  
  30.                             欢迎登录  
  31.                         </td>  
  32.                     </tr>  
  33.                     <tr>  
  34.                         <td><br/></td>  
  35.                     </tr>  
  36.                     <tr>  
  37.                         <td>  
  38.                             <input name="userid" type="text" placeholder="User ID" style="width:500px; height:30px; text-align:center; font-size:24px; "/>  
  39.                         </td>  
  40.                     </tr>  
  41.                     <tr>  
  42.                         <td>  
  43.                             <input name="userpwd" type="password" placeholder="Password" style="width:500px; height:30px; text-align:center; font-size:24px; "/>  
  44.                         </td>  
  45.                     </tr>  
  46.                     <tr>  
  47.                         <td align="center">  
  48.                             <input type="submit" value="Submit" style="width:30%; height:30px; font-size:24px; " />  
  49.                         </td>  
  50.                     </tr>  
  51.                 </table>  
  52.             </s:form>  
  53.         </div>  
  54.     </body>  
  55. </html>  
(本学期软件工程项目开发经验)eclipse环境下struts2和mysql结合的登录验证
[html] view plain copyprint?
  1. <%--   
  2.     Document   : loginsuccess.jsp  
  3. --%>  
  4.   
  5. <%@page contentType="text/html" pageEncoding="UTF-8"%>  
  6. <!DOCTYPE html>  
  7. <html>  
  8.     <head>  
  9.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  10.         <title>JSP Page</title>  
  11.     </head>  
  12.     <body>  
  13.         <h1>Success!</h1>  
  14.     </body>  
  15. </html>  
(本学期软件工程项目开发经验)eclipse环境下struts2和mysql结合的登录验证
[html] view plain copyprint?
  1. <%--   
  2.     Document   : loginfail.jsp  
  3. --%>  
  4.   
  5. <%@page contentType="text/html" pageEncoding="UTF-8"%>  
  6. <!DOCTYPE html>  
  7. <html>  
  8.     <head>  
  9.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  10.         <title>JSP Page</title>  
  11.     </head>  
  12.     <body>  
  13.         <h1>Fail!</h1>  
  14.     </body>  
  15. </html>  
(本学期软件工程项目开发经验)eclipse环境下struts2和mysql结合的登录验证

以上三个文件,LoginJSP.jsp为登录界面,loginsuccess.jsp为登录成功的页面,loginfail.jsp为登录失败的页面。


6. 编写登录Action,配置struts.xml文件。Action中的属性名称需要和jsp页面中form表单中的标签name值保持一致,这样才可以在action触发的时候获得相应的属性值,并执行execute()中的代码。注意,structs.xml文件中的路径填写是很容易出错的,要根据自己实际项目的包和文件夹路径来填写。

[java] view plain copyprint?
  1. package Login;  
  2.   
  3. import SQLHelper.SQLCon;  
  4. import com.opensymphony.xwork2.ActionSupport;  
  5. import java.sql.ResultSet;  
  6. import java.sql.SQLException;  
  7. /** 
  8.  * @author falleyes 
  9.  */  
  10. public class LoginAction extends ActionSupport {  
  11.   
  12.     private String userid;  
  13.     private String userpwd;  
  14.   
  15.     public LoginAction() {  
  16.     }  
  17.   
  18.     //执行部分  
  19.     public String execute() throws SQLException {  
  20.   
  21.         //新建连接  
  22.         try {  
  23.             new SQLCon();  
  24.         } catch (Exception e) {  
  25.             return INPUT;  
  26.         }  
  27.   
  28.         //SQL语句  
  29.         String sql = "select Count(*) as Total From attendance.admin Where id='"  
  30.                 + userid + "' and pwd='" + userpwd + "'";  
  31.         ResultSet rs = null;  
  32.   
  33.         //获得检索结果并返回结果字符串  
  34.         try {  
  35.             rs = SQLCon.getConnection().createStatement().executeQuery(sql);  
  36.             if (rs.next()) {  
  37.                 return rs.getInt("Total") >= 1 ? SUCCESS : INPUT;  
  38.             }  
  39.         } catch (SQLException ex) {  
  40.             return INPUT;  
  41.         } finally {  
  42.             SQLCon.CloseCon();  
  43.         }  
  44.         return INPUT;  
  45.     }  
  46.   
  47.     /** 
  48.      * @return the userid 
  49.      */  
  50.     public String getUserid() {  
  51.         return userid;  
  52.     }  
  53.   
  54.     /** 
  55.      * @param userid the userid to set 
  56.      */  
  57.     public void setUserid(String userid) {  
  58.         this.userid = userid;  
  59.     }  
  60.   
  61.     /** 
  62.      * @return the userpwd 
  63.      */  
  64.     public String getUserpwd() {  
  65.         return userpwd;  
  66.     }  
  67.   
  68.     /** 
  69.      * @param userpwd the userpwd to set 
  70.      */  
  71.     public void setUserpwd(String userpwd) {  
  72.         this.userpwd = userpwd;  
  73.     }  
  74. }  
(本学期软件工程项目开发经验)eclipse环境下struts2和mysql结合的登录验证
[html] view plain copyprint?
  1. <!DOCTYPE struts PUBLIC  
  2. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  3. "http://struts.apache.org/dtds/struts-2.0.dtd">  
  4.   
  5. <struts>  
  6.     <package name="base" extends="struts-default">  
  7.           
  8.         <action name="LoginAction" class="Login.LoginAction">  
  9.             <result name="success">loginsuccess.jsp</result>  
  10.             <result name="input">loginfail.jsp</result>  
  11.         </action>  
  12.           
  13.     </package>  
  14. </struts>  
(本学期软件工程项目开发经验)eclipse环境下struts2和mysql结合的登录验证

7. 测试登录

(本学期软件工程项目开发经验)eclipse环境下struts2和mysql结合的登录验证
测试输入"admin","admin",跳转到Success!的页面,输入其他,跳转到Fail!的页面。