JavaWeb+jsp+Mysql登入註冊
阿新 • • 發佈:2018-12-02
JavaWeb+jsp+Mysql登入註冊
- List item
先需要匯入資料庫包
- 專案
1.login.jsp(登入介面)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort ()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content= "no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" >
function doSubmit(){
var myLoginName = document.getElementById("loginName").value;
if(myLoginName==""){
alert("使用者名稱不能為空");
document.getElementById("loginName").focus();
return false;
}
else{
return true;
}
}
</script>
<style type="text/css">
#main {
position: fixed;
width: 400px;
height: 300px;
top: 70%;
left: 60%;
margin-top: -300px;
margin-left: -200px;
}
td {
padding: 5px;
}
input {
width: 100%;
height: 30px;
}
</style>
</head>
<body
style="background: url(pic/login10.png);background-size:cover;font-family: 微軟雅黑;">
<!-- 頁面的Form表單 用來和後端建立互動的關鍵點 -->
<div id="main">
<form name="f1" id="f1" action="toCheckLogin" method="post"
onsubmit="return doSubmit();">
<table border="0">
<tr>
<td colspan="1"><center>
<h3>使用者登入</h3>
</center>
</td>
</tr>
<tr>
<td><input type="text" name="loginName" id="loginName" placeholder="請輸入您的使用者名稱">
</td>
<td colspan="1"><center>
<td><font color="red" size="2"> ${MSG}</font></td>
</center>
</td>
</tr>
<tr>
<td><input type="password" name="loginPwd" id="password" placeholder="請輸入您的密碼">
</td>
</tr>
<tr>
<td colspan="1" align="center"><input type="submit" value="登陸">
</td>
</tr>
</table>
</form>
<a href="regist.jsp" style="margin-left: 40px;"><font size="2"><i>沒有帳號?點選註冊</i></font></a>
<br>
</div>
</body>
</html>
2.regist.jsp(註冊介面)
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'regist.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script src="https://code.jquery.com/jquery-3.3.1.min.js"
type="text/javascript"></script>
<style type="text/css">
#main {
position: fixed;
width: 400px;
height: 300px;
top: 70%;
left: 60%;
margin-top: -300px;
margin-left: -200px;
}
td {
padding: 5px;
}
input {
width: 100%;
height: 30px;
}
</style>
<script type="text/javascript">
function doSubmit1(){
var registerName = document.getElementById("registerName").value;
var psw = document.getElementById("psw").value;
if(registerName==""){
alert("使用者名稱不能為空");
document.getElementById("registerName").focus();
return false;
}
if(registerName!=""&&psw==""){
alert("密碼名不能為空");
document.getElementById("psw").focus();
return false;
}
else{
return true;
}
}
</script>
</head>
<body
style="background: url(pic/login10.png);background-size:cover;font-family: 微軟雅黑;">
<div id="main">
<form action="RegistServlet" method="post" onsubmit="return doSubmit1()">
<table >
<tr>
<td><center>
<h3>註冊</h3>
</center></td>
</tr>
<tr>
<td><input type="text" name="registerName" id="registerName"
placeholder="設定您的使用者名稱"> <span id="tishi1"></span></td>
</tr>
<tr>
<td><input type="password" name="psw" id="psw" placeholder="設定您的密碼"></td>
</tr>
<tr>
<td><input type="password" name="rpsw" placeholder="請確認您的密碼"></td>
<td><font color="red" size="2"> ${MSG1}</font></td>
</tr>
<tr>
<td><input type="text" name="email" id="email" placeholder="請確認您的郵箱"></td>
</tr>
<tr>
<td><input type="submit" value="註冊"></td>
<td><font color="red" size="2"> ${MSG2}</font></td>
</tr>
</table>
</form>
<a href="login.jsp" style="margin-left: 70px;"><font size="2"><i>返回登入</i>
</font> </a>
</div>
</body>
</html>
Java程式碼
1.Login.java(com.aiit.model)
package com.aiit.model;
public class Login {
private String LoginName;
private String LoginPwd;
private String email;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getLoginName() {
return LoginName;
}
public void setLoginName(String loginName) {
LoginName = loginName;
}
public String getLoginPwd() {
return LoginPwd;
}
public void setLoginPwd(String loginPwd) {
LoginPwd = loginPwd;
}
public Login(String loginName, String loginPwd) {
super();
LoginName = loginName;
LoginPwd = loginPwd;
}
public Login(String loginName) {
super();
LoginName = loginName;
}
public Login() {
super();
}
public Login(String loginName, String loginPwd, String email) {
super();
LoginName = loginName;
LoginPwd = loginPwd;
this.email = email;
}
}
2.JDBCUtils.java(把資料庫封裝成一個類)
package com.aiit.common;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
*
* @author 123
*
*/
public class JDBCUtils {
public static Connection getConnection()
{
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/java01?useUnicode=true&characterEncoding=UTF-8", "root", "");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static void closeAll(Connection conn,PreparedStatement pre,ResultSet rs)
{
try {
if(rs!=null)
{
rs.close();
}
if(pre!=null)
{
pre.close();
}
if(conn!=null)
{
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void closeBoth(Connection conn,PreparedStatement pre)
{
try {
if(pre!=null)
{
pre.close();
}
if(conn!=null)
{
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3.LoginDaoImp.java(資料庫操作)
package com.aiit.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.aiit.common.JDBCUtils;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
public class LoginDaoImp {
//登入時,進行資料庫判斷,賬號和密碼是否正確
public boolean searchNameAndPwd(String loginName, String loginPwd) {
//連線資料庫
Connection conn = (Connection) JDBCUtils.getConnection();
String sql="SELECT loginName,loginPwd FROM tbl_login WHERE loginName=? AND loginPwd=?";
try {
PreparedStatement pre=(PreparedStatement) conn.prepareStatement(sql);
pre.setString(1, loginName);
pre.setString(2, loginPwd);
ResultSet rs=pre.executeQuery();
while(rs.next()){
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
//註冊,在資料庫中插入賬號和密碼、郵箱
public void RegistNameAndPwd(String loginName, String loginPwd,String email) {
// TODO Auto-generated method stub
Connection conn = (Connection) JDBCUtils.getConnection();
String sql="insert into tbl_login(loginName,loginPwd,loginEmail) values(?,?,?)";
try {
PreparedStatement pre=(PreparedStatement) conn.prepareStatement(sql);
pre.setString(1, loginName);
pre.setString(2, loginPwd);
pre.setString(3, email);
pre.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
4.toCheckLogin(登入介面提交表單,service進行接收處理資料)
package com.aiit.service;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.aiit.dao.LoginDaoImp;
import com.aiit.model.Login;
@WebServlet("/toCheckLogin")
public class CheckLoginController extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
//獲取表單傳過來的資料
String myName = request