1. 程式人生 > >struts2連結資料庫的簡單登陸驗證

struts2連結資料庫的簡單登陸驗證

1、安裝Mysql,建立資料庫struts2,這裡建立三個欄位username,userpassword,userrealname


2、建立資料變數EndUser.java

package com.model.user;
public class EndUser {
private String userName;
private String userPassword;
private String userRealName;
public EndUser(){}
public String getUserName() {
	return userName;
}

public void setUserName(String userName) {
	this.userName = userName;
}

public String getUserPassword() {
	return userPassword;
}

public void setUserPassword(String userPassword) {
	this.userPassword = userPassword;
}
public String getUserRealName() {
	return userRealName;
}

public void setUserRealName(String userRealName) {
	this.userRealName = userRealName;
}
}
建立資料庫連結類DBConection.java
package com.db_util;
import java.sql.*;

public class DBConnection {
	private static String driverName="com.mysql.jdbc.Driver";
	private static String userName="root";
	private static String userPwd="root";
	private static String dbName="struts2";
	public static Connection getDBConnection(){
		String url1="jdbc:mysql://localhost:3306/"+dbName;
		String url2="?user="+userName+"&password="+userPwd;
		String url3="&useUnicode=true&characterEncoding=utf-8";
		String url=url1+url2+url3;
		Connection con=null;
		try{
			Class.forName(driverName);//載入驅動
			con=DriverManager.getConnection(url);//連結資料庫
		}catch(Exception e){e.printStackTrace();}
		return con;
	}
	//關閉資料庫
	public static void closeDB(Connection con,PreparedStatement pstm,ResultSet rs){
		if(rs!=null)try{rs.close();}catch(SQLException e){e.printStackTrace();}
		if(pstm!=null)try{pstm.close();}catch(SQLException e){e.printStackTrace();}
		if(con!=null)try{con.close();}catch(SQLException e){e.printStackTrace();}
	}
}
建立資料庫查詢和新增類UserDao.java
package com.db_util;
import java.sql.*;

public class DBConnection {
	private static String driverName="com.mysql.jdbc.Driver";
	private static String userName="root";
	private static String userPwd="root";
	private static String dbName="struts2";
	public static Connection getDBConnection(){
		String url1="jdbc:mysql://localhost:3306/"+dbName;
		String url2="?user="+userName+"&password="+userPwd;
		String url3="&useUnicode=true&characterEncoding=utf-8";
		String url=url1+url2+url3;
		Connection con=null;
		try{
			Class.forName(driverName);//載入驅動
			con=DriverManager.getConnection(url);//連結資料庫
		}catch(Exception e){e.printStackTrace();}
		return con;
	}
	//關閉資料庫
	public static void closeDB(Connection con,PreparedStatement pstm,ResultSet rs){
		if(rs!=null)try{rs.close();}catch(SQLException e){e.printStackTrace();}
		if(pstm!=null)try{pstm.close();}catch(SQLException e){e.printStackTrace();}
		if(con!=null)try{con.close();}catch(SQLException e){e.printStackTrace();}
	}
}
建立Action類UserAction.java
package com.action.user;
import com.model.user.*;
import com.dao.user.*;
public class UserAction {
	private EndUser user;
	private UserDao userDao=new UserDao();
	public EndUser getUser(){
		return user;
	}
	public void setUser(EndUser user){
		this.user=user;
	}
	//使用者登入Action的方法
	public String userLogin() throws Exception{
		String forward=null;
		EndUser user2=userDao.find(user);
		if(user2!=null){forward="success";}else{forward="failure";}
		return forward;
	}
	//使用者註冊Action的方法
	public String userRegister() throws Exception{
		String forward="error";
		int flag=0;
		EndUser user2=(userDao.find(user));
		if(user2!=null){forward="error_user";}
		else{
			flag=userDao.save(user);
			if(flag==1){forward="success";}
		}
		return forward;
	}
}
3、建立jsp頁面

建立login.jsp頁面

<form action="/struts2/user/logincheck" method="post">
<table>
<tr><th colspan="2">使用者登入</th></tr>
<tr><th align="right">使用者名稱:</th><td><input name="user.userName"/></td></tr>
<tr><td align="right">密碼:</td>
<td><input type="password" name="user.userPassword"/></td></tr>
<tr><td align="left"><input type="submit" value="登入"/></td>
<td>未註冊者,請先註冊,單擊<a href="/struts2/user/register.jsp">註冊</a></td>
</tr>
</table>
</form>
建立register.jsp
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>註冊頁面</title>
<script type="text/javascript">
function isValidate(form){
	var username=document.getElementById("username").value;
	var userpass=document.getElementById("userpassword").value;
	var userpass1=document.getElementById("userpass1").value;
	if(userpass!=userpass1){
		alert("兩次密碼輸入不一致,請重新輸入!");
		return false;
	}else if(username.length<=0||userpassword.length<=0)
	{
		alert("使用者名稱及其密碼不能為空,請重新輸入!");
		returnfalse;}
	else {return true;}
}
</script>
</head>
<body>
<h3 align="left">歡迎註冊,請填資訊!</h3>
<form name="register" action="/struts2/user/register" method="post" onsubmit="return isValidate()">
<table>
<tr><td align="right">賬戶名:</td>
<td><input name="user.userName" id="username"></td></tr>
<tr><td align="right">賬戶設定密碼:</td>
<td><input type="password" name="user.userPassword" id="userpassword"></td></tr>
<tr><td align="right">再次確認你的密碼:</td>
<td><input type="password" name="userpass1" id="userpass1"></td></tr>
<tr><td align="right">真實姓名:</td>
<td><input name="user.userRealName" id="userrealname"></td></tr>
<tr><td align="right"><input type="submit" value="提交"></td>
<td colspan="2"><input type="reset" value="重新填寫"></td></tr>
</table>
</form>
</body>
簡單建立login_failure.jsp login_success.jsp register_success.jsp register_failure.jsp register_failure_user.jsp index.jsp頁面

login_success.jsp

<body>
歡迎你,${user.userRealName} ,您已經成功登入!<br>
進入主頁面,請點選<a href="/struts2/index.jsp">主頁面</a>
</body>
login_failure.jsp
<body>
<h2 align="center">
<font color="red">對不起,您填寫的帳號和密碼不正確!請</font>
<a href="/struts2/user/login.jsp">重新登入</a>
</h2>
</body>
配置struct.xml
<struts>
<package name="struts2" namespace="/" extends="struts-default">
<action name="register" class="com.action.user.UserAction" method="userRegister">
<result name="success">/user/register_success.jsp</result>
<result name="error">/user/struts2/register_failure.jsp</result>
<result name="error_user">/user/struts2/register_failure_user.jsp</result>
</action>
<action name="logincheck" class="com.action.user.UserAction" method="userLogin">
<result name="success">/user/login_success.jsp</result>
<result name="error">/user/login_failure.jsp</result>
</action>
</package>
</struts>
配置web.xml
<filter>
        <filter-name>struts2PrepareFilter</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter</filter-class>
    </filter>
    <filter>
        <filter-name>struts2</filter-name>
        <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> 

註冊頁面圖