JavaWeb+jsp+Mysql登录注册

JavaWeb+jsp+Mysql登录注册

  • List item

JavaWeb+jsp+Mysql登录注册_第1张图片

先需要导入数据库包

  • 项目
    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.getParameter("loginName");
			String myPwd = request.getParameter("loginPwd");
			//把获取到的数据传到数据库,进行判断
			LoginDaoImp loginService = new LoginDaoImp();
			boolean	isHave=loginService.searchNameAndPwd(myName, myPwd);
			//如果账号密码正确,登录成功
			if(isHave){
				request.setAttribute("myName", myName);			
				request.getRequestDispatcher("index.jsp").forward(request, response);
			}
			else{
				//如果失败,报告错误
				request.getSession().setAttribute("MSG", "账号或密码错误");		
				response.sendRedirect("login.jsp");
			}

		}
		

}

5.CheckRrgist.java(提交注册表单,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.getParameter("loginName");
			String myPwd = request.getParameter("loginPwd");
			//把获取到的数据传到数据库,进行判断
			LoginDaoImp loginService = new LoginDaoImp();
			boolean	isHave=loginService.searchNameAndPwd(myName, myPwd);
			//如果账号密码正确,登录成功
			if(isHave){
				request.setAttribute("myName", myName);			
				request.getRequestDispatcher("index.jsp").forward(request, response);
			}
			else{
				//如果失败,报告错误
				request.getSession().setAttribute("MSG", "账号或密码错误");		
				response.sendRedirect("login.jsp");
			}

		}
		

}

JavaWeb+jsp+Mysql登录注册_第2张图片

JavaWeb+jsp+Mysql登录注册_第3张图片

你可能感兴趣的:(JavaWeb)