使用struts2+MySQL做简单的登录验证
来源:互联网 发布:kmp算法next例题 编辑:程序博客网 时间:2024/05/18 03:47
环境:
<span style="font-family:Microsoft YaHei;font-size:14px;">JDK:1.8IDE:NetBeans 8.0.2struts:2.3.15MySQL:5.6.26</span>
-- 创建数据库CREATE DATABASE `attendance`;-- 创建表CREATE TABLE `admin` ( `id` varchar(10) NOT NULL, `pwd` varchar(20) NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;-- 插入记录,测试用户名"admin",密码"admin"INSERT INTO attendance.admin(id,pwd) values('admin','admin');
2. 编写数据库连接类SQLCon
package SQLHelper;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;/** * @author falleyes */public class SQLCon { // 连接实例 private static Connection conn = null; //连接地址 String url = "jdbc:mysql://localhost:3306/mysql"; // MySQL用户名 String user = "sa"; // MySQL密码 String password = "mysql"; public SQLCon() throws Exception { Class.forName("com.mysql.jdbc.Driver"); conn=DriverManager.getConnection(url,user,password); } //获得连接对象 public static Connection getConnection(){ return conn; } //关闭连接 public static void CloseCon() throws SQLException{ conn.close(); }}
3. 设计网站的结构
4. 配置web.xml并且编写index.jsp页面(该页面只是个跳转页面)
<?xml version="1.0" encoding="UTF-8"?><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"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
<%-- Document : index.jsp--%><%@page contentType="text/html" pageEncoding="UTF-8"%><!DOCTYPE html><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </style> </head> <body> <jsp:forward page="Login/LoginJSP.jsp"/> </body></html>
<%-- Document : LoginJSP.jsp--%><%@page contentType="text/html" pageEncoding="UTF-8"%><%@taglib prefix="s" uri="/struts-tags" %><!DOCTYPE html><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> <style type="text/css"> #MainLogin { position: absolute; width: 500px; top: 30%; left: 50%; font-size:24px; margin-left:-250px; } </style> </head> <body> <div class="MainLogin" id="MainLogin"> <s:form action="LoginAction"> <table> <tr> <td style="height:30px; text-align:center; font-size:24px; "> 欢迎登录 </td> </tr> <tr> <td><br/></td> </tr> <tr> <td> <input name="userid" type="text" placeholder="User ID" style="width:500px; height:30px; text-align:center; font-size:24px; "/> </td> </tr> <tr> <td> <input name="userpwd" type="password" placeholder="Password" style="width:500px; height:30px; text-align:center; font-size:24px; "/> </td> </tr> <tr> <td align="center"> <input type="submit" value="Submit" style="width:30%; height:30px; font-size:24px; " /> </td> </tr> </table> </s:form> </div> </body></html>
<%-- Document : loginsuccess.jsp--%><%@page contentType="text/html" pageEncoding="UTF-8"%><!DOCTYPE html><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <h1>Success!</h1> </body></html>
<%-- Document : loginfail.jsp--%><%@page contentType="text/html" pageEncoding="UTF-8"%><!DOCTYPE html><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <h1>Fail!</h1> </body></html>以上三个文件,LoginJSP.jsp为登录界面,loginsuccess.jsp为登录成功的页面,loginfail.jsp为登录失败的页面。
6. 编写登录Action,配置struts.xml文件。Action中的属性名称需要和jsp页面中form表单中的标签name值保持一致,这样才可以在action触发的时候获得相应的属性值,并执行execute()中的代码。注意,structs.xml文件中的路径填写是很容易出错的,要根据自己实际项目的包和文件夹路径来填写。
package Login;import SQLHelper.SQLCon;import com.opensymphony.xwork2.ActionSupport;import java.sql.ResultSet;import java.sql.SQLException;/** * @author falleyes */public class LoginAction extends ActionSupport { private String userid; private String userpwd; public LoginAction() { } //执行部分 public String execute() throws SQLException { //新建连接 try { new SQLCon(); } catch (Exception e) { return INPUT; } //SQL语句 String sql = "select Count(*) as Total From attendance.admin Where id='" + userid + "' and pwd='" + userpwd + "'"; ResultSet rs = null; //获得检索结果并返回结果字符串 try { rs = SQLCon.getConnection().createStatement().executeQuery(sql); if (rs.next()) { return rs.getInt("Total") >= 1 ? SUCCESS : INPUT; } } catch (SQLException ex) { return INPUT; } finally { SQLCon.CloseCon(); } return INPUT; } /** * @return the userid */ public String getUserid() { return userid; } /** * @param userid the userid to set */ public void setUserid(String userid) { this.userid = userid; } /** * @return the userpwd */ public String getUserpwd() { return userpwd; } /** * @param userpwd the userpwd to set */ public void setUserpwd(String userpwd) { this.userpwd = userpwd; }}
<!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <package name="base" extends="struts-default"> <action name="LoginAction" class="Login.LoginAction"> <result name="success">loginsuccess.jsp</result> <result name="input">loginfail.jsp</result> </action> </package></struts>
7. 测试登录
测试输入"admin","admin",跳转到Success!的页面,输入其他,跳转到Fail!的页面。
0 0
- 使用struts2+MySQL做简单的登录验证
- Java+Eclipse+Maven+struts2从MySQL数据库中查询用户密码实现简单的登录验证
- Unity3D C# MySql 简单的登录验证
- Unity3D C# MySql 简单的登录验证
- php+mysql 最简单的登录验证
- Struts2+mysql登录验证实例
- java Struts2 框架的搭建与简单的登录验证
- Struts2和hibernate3的简单应用-登录验证
- Ext做的简单登录界面(验证码刷新)
- 使用过滤器做登录验证
- 使用Java+MySQL做的一个简单的注册登录页面
- 使用java+Mysql数据库做一个简单的登录窗口界面
- 使用java+Mysql数据库做一个简单的登录窗口界面
- 简单的登录验证
- 简单的登录验证
- 简单的验证登录
- 简单的登录验证
- struts2+Spring2.0+Hibernate3使用注解的登录验证
- 排序算法之Bucket Sort
- 二维数组查询
- samsung s3 gt-19300 开发 键盘回车事件
- 设计模式——行为型模式
- iOS中操作SQLite数据库
- 使用struts2+MySQL做简单的登录验证
- Xcode升级到7.1出现itms-90049 错误 的解决办法
- 2006年浙江大学1019简单计算器代码(栈的使用)
- Jsoup获取网页源代码不一致的问题
- 详解Android中AsyncTask的使用
- 新手程序媛要怎么做
- UITabBarController定制
- JSP/Servlet-----11、Filter
- emjoy表情插入数据库问题