利用struts2框架连接数据库实现用户注册与登录
工具/原料
eclipse、struts-2.3.16.3
Mysql
方法/步骤
1、工程目录:
2、工程用到的jar包有:
3、数据库用户表tb_user:
4、web.xml的配置:<?xmlversion="1.0"encoding="UTF-8"standalone="yes"?><web-appxmlns="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的配置:<?x葡矩酉缸mlversion="1.0"encoding="炽扃仄呦UTF-8"?><!DOCTYPEstrutsPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <packagename="com"extends="struts-default"> <actionname="login"class="com.UserAction"method="login"> <resultname="loginout">/error.jsp</result> <resultname="loginin">/welcome.jsp</result> </action> <actionname="regist"class="com.UserAction"method="regist"> <resultname="error">/error.jsp</result> <resultname="success">/welcome.jsp</result> </action> </package></struts>
6、数据库Dao.javapackagedao;importjava.sql.*;publicclassDao{ Connectioncon=null; Statementstat=null; ResultSetrs=null; publicDao(){ try{ Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection("jdbc:mysql://localhost:3306/db_vote","root","root"); stat=con.createStatement(); }catch(Exceptione){ //TODO:handleexception con=null; } } publicResultSetexecuteQuery(Stringsql){ try{ rs=stat.executeQuery(sql); }catch(Exceptione){ //TODO:handleexception rs=null; } returnrs; } publicintexecuteUpdate(Stringsql){ try{ stat.executeUpdate(sql); return0; }catch(Exceptione){ //TODO:handleexception } return-1; }}
7、UserAction.javapackagecom;importjava饱终柯肢.sql.*;importcom.opensymphony.xwork2.ActionSupport;impor墉掠载牿tdao.Dao;@SuppressWarnings("serial")publicclassUserActionextendsActionSupport{ privateDaodao=newDao(); privateStringusername; privateStringpassword; publicStringgetUsername(){ returnusername; } publicvoidsetUsername(Stringusername){ this.username=username; } publicStringgetPassword(){ returnpassword; } publicvoidsetPassword(Stringpassword){ this.password=password; } publicStringlogin(){ Stringsql="select*fromtb_userwhereusername='"+getUsername()+"'andpassword='"+getPassword()+"'"; ResultSetrS=dao.executeQuery(sql); try{ if(rS.next()){ return"loginin"; } return"loginout"; }catch(SQLExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); return"loginout"; } } publicStringregist(){ Stringsql="insertintotb_user(username,password,isCheck)values('"+getUsername()+"','"+getPassword()+"','0')"; inti=dao.executeUpdate(sql); if(i>-1){ return"success"; } return"error"; } //通用的执行方法// publicStringexecute()throwsException{// if(getUsername().equals("scott")&&getPassword().equals("tiger")){// System.out.println("我是success");// return"success";// }// // else{// System.out.println("我是error");// return"error";// }// }}
8、用户注册界面regist.jsp<%@pagelanguage=&quo隋茚粟胫t;java"contentType=&qu泠贾高框ot;text/html;charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><metahttp-equiv="Content-Type"content="text/html;charset=UTF-8"><title>Inserttitlehere</title></head><body><center><formaction="regist.action"method="post"> <table> <caption><h2>用户登录</h2></caption> <tr> <td>username:</td> <td><inputtype="text"name="username"/></td> </tr> <tr> <td>password:</td> <td><inputtype="password"name="password"/></td> </tr> <tr> <td><inputtype="submit"name="submit"/></td> </tr> </table> </form></center></body></html>
9、登录界面login.jsp<%@pagelanguage="java争犸禀淫"contentType="text/html;charset租涫疼迟=UTF-8"pageEncoding="UTF-8"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><metahttp-equiv="Content-Type"content="text/html;charset=UTF-8"><title>Inserttitlehere</title></head><body><formaction="login.action"method="post"> <tablealign="center"> <caption><h2>用户登录</h2></caption> <tr> <td>用户名:<inputtype="text"name="username"/></td> </tr> <tr> <td>密码:<inputtype="text"name="password"/></td> </tr> <tralign="center"> <tdcolspan="2"><inputtype="submit"/></td> </tr> </table></form></body></html>
10、操作错误页面error.jsp<%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><%@taglibprefix="s"uri="/struts-tags"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><metahttp-equiv="Content-Type"content="text/html;charset=UTF-8"><title>Inserttitlehere</title></head><body><center> 执行失败!!</center></body></html>
11、操作成功界面welcome.jsp<%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><metahttp-equiv="Content-Type"content="text/html;charset=UTF-8"><title>Inserttitlehere</title></head><body>您已经执行成功!</body></html>