完整的项目截图
1.UserBean
package com.qst.bean;
//数据封装
public class UserBean {
private String username,pwd,email;
private int uid;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getId() {
return uid;
}
public void setId(int id) {
this.uid = id;
}
}
2.UserDao
package com.qst.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.qst.bean.UserBean;
import com.qst.util.DBUtil;
public class UserDao {
//登录
//try catch finally 表示异常处理
//try中包含可能出现异常的语句
//catch 表示异常处理
//finally 包含必须执行的语句
public UserBean login(UserBean userbean) {
//实例化Connection对象conn,连接数据库
Connection conn = DBUtil.getConnection();
//实例化PreparedStatement对象pstmt,用于传递sql语句
PreparedStatement pstmt = null;
//实例化ResultSet对象rs,用于返回结果
ResultSet rs = null;
//实例化UserBean对象userbrean,用于返回结果
UserBean userbrean = null;
//sql语句 ?代表占位符
String sql = "select * from user where username=? and pwd=?";
try {
//运行sql语句
pstmt = conn.prepareStatement(sql);
//第一个问号的值
pstmt.setString(1, userbean.getUsername());
//第二个问号的值
pstmt.setString(2, userbean.getPwd());
//返回结果集
rs = pstmt.executeQuery();
while(rs.next())//遍历结果集
{
//将结果返回到userbrean,用于最后的return返回到login。
userbrean = new UserBean();
userbrean.setUsername(rs.getString("username"));
userbrean.setUsername(rs.getString("pwd"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//关闭
DBUtil.closeJDBC(rs, pstmt, conn);
}
return userbrean;
}
//以下就是sql语句的差别,不做解释了
//注册中查询用户名是否一样
public boolean isExitEmail(String userid) {
Connection conn = DBUtil.getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select * from user where username=?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, userid);
rs = pstmt.executeQuery();
while(rs.next())
{
//用户名重复
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DBUtil.closeJDBC(rs, pstmt, conn);
}
//用户名没重复
return false;
}
//注册
public void save(String username, String pwd, String email) {
Connection conn = DBUtil.getConnection();
PreparedStatement pstmt = null;
String sql = "insert into user (username,pwd,email)" +
"values(?,?,?)";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, username);
pstmt.setString(2, pwd);
pstmt.setString(3, email);
pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DBUtil.closeJDBC(null, pstmt, conn);
}
}
}
3.Login
package com.qst.servlet;
import java.io.IOException;
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 javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;
import com.qst.bean.UserBean;
import com.qst.dao.UserDao;
/**
* Servlet implementation class Login
*/
@WebServlet("/Login")
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Login() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=utf-8");
// 从jsp页面读取用户名密码 括号里的名字需要和jsp页面的name属性一样(jsp中:name=username)
String username = request.getParameter("username");
String pwd = request.getParameter("pwd");
//UserBean实例化一个对象,对象名(userbean)
UserBean userbean = new UserBean();
//传值给userbean对象
userbean.setUsername(username);
userbean.setPwd(pwd);
//UserDao实例化一个对象,对象名(userdao)
UserDao userdao = new UserDao();
//UserBean实例化一个对象,对象名(user)
//并将userdao对象调用的login方法的返回值赋值给user
UserBean user = userdao.login(userbean);
if (user != null) {//判断用户名密码是否存在
//成功则跳转index.jsp页面,失败返回Login.jsp
//HttpSession实例化一个对象,对象名(session)
HttpSession session = request.getSession();
//调用session的setAttribute方法,
//将username的值赋值给key,在jsp页面中可以取出。
session.setAttribute("key",username);
//重定向
response.sendRedirect("index.jsp");
}
else {
//页面弹框,提示错误,可以不用。
JOptionPane.showMessageDialog(null, "用户名或密码错误", null, JOptionPane.ERROR_MESSAGE);
//重定向
response.sendRedirect("Login.jsp");
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
4RegisterServlet
package com.qst.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;
import com.qst.dao.UserDao;
/**
* Servlet implementation class RegisterServlet
*/
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public RegisterServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// 从jsp页面读取用户名密码,email
String username = request.getParameter("username");
String pwd = request.getParameter("password");
String email = request.getParameter("email");
//UserDao实例化一个对象,对象名(dao)
UserDao dao = new UserDao();
//boolean(布尔xing,只能是true和false)实例化一个对象,对象名(flag)
//给dao的isExitEmail方法传值username,判断用户名是否重复
//将返回值给flag
boolean flag = dao.isExitEmail(username);
if(flag){
//用户名重复
//弹框提示
JOptionPane.showMessageDialog(null, "用户名已被注册,请重新注册", null, JOptionPane.ERROR_MESSAGE);
//重定向Register页面
response.sendRedirect("Register.jsp");
}
else{
//用户名不重复
//弹框提示
JOptionPane.showMessageDialog(null, "注册成功!", null, JOptionPane.ERROR_MESSAGE);
//调用dao的save方法,将数据保存到数据库
dao.save(username,pwd,email);
//重定向Login页面
response.sendRedirect("Login.jsp");
}
}
}
5.DBUtil
package com.qst.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
//数据库mysql
static String name = "root";//用户名
static String password = "123456";//密码
static String url = "jdbc:mysql://localhost:3306/zuoye";
//MySQL的JDBC URL编写方式:jdbc:mysql://主机名称:连接端口/数据库的名称(zuoye)
static {
try{
//加载驱动类
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
//进行数据库连接
public static Connection getConnection() {
Connection conn = null;
try{
conn = DriverManager.getConnection(url, name, password);
}catch(SQLException e){
e.printStackTrace();
}
return conn;
}
//关闭 rs,pstmt,conn
public static void closeJDBC(ResultSet rs, PreparedStatement pstmt, Connection conn) {
if(rs !=null){
try{
rs.close();
}catch(SQLException e){
e.printStackTrace();
}
}
if(pstmt !=null){
try{
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn != null)
{
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
6index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<%-- 提取session中内容 赋值给String类型name变量--%>
<%
String name = (String) session.getAttribute("key");
%>
<body>
<%-- 简单判断用户名是否存在 并输出 --%>
<p>
<%if(name!=null){
//从登录页面跳转,则显示
out.print(name+"用户,欢迎您登录");}
else{
//直接打开显示
out.print("您还没有登录");
}%></p>
</body>
</html>
7Login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录</title>
</head>
<body>
<h1>登录</h1>
<%-- 用form表单提交数据 action=“提交的servlet” method=“提交方式” --%>
<form action="Login" method="post">
<br><br><%--br 换行--%>
<%-- type=“类型” name=“名字”,和servlet中名字一样 placeholder=“输入框默认内容” required 输入框不能为空 --%>
<%-- type=“类型” text表示文本输入 password表示密码输入(不可见) --%>
<input type="text" name="username" placeholder="用户名" required="">
<br><br>
<input type="password" name="pwd" placeholder="密码" required="">
<br><br>
<input type="submit" value="登 录">
</div>
<%-- 超链接 跳转注册--%>
<a href="Register.jsp">注册</a>
</form>
</body>
</html>
8Register.jsp`
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>注册</title>
</head>
<body>
<h1>注册</h1>
<%-- 和login中一样不做解释 --%>
<form action="RegisterServlet" method="post">
<input type="text" Name="username" placeholder="用户名" required="">
<input type="email" Name="email" placeholder="邮箱" required="">
<input type="password" Name="password" placeholder="密码" required="">
<input type="submit" value="注册">
</form>
<%-- 超链接 跳转登录页面--%>
<a href="Login.jsp">返回登录页面 </a>
</body>
</html>
9数据库
注意:其中id要设置自动递增
10运行截图
登录失败
登录成功
注册
注册失败
注册成功
数据库(有时候需要刷新)
jar包:mysql-connector-java-5.1.39-bin 下载
链接:https://pan.baidu.com/s/1YyNl1dtaTYJdi8oFqwUQ8w
提取码:fur0
完整内容
链接:https://pan.baidu.com/s/1snS8eE8wI2KFsxdFbhv-hw
提取码:42xq