目录
1、在MySQL中新建一个servletdatabase数据库,创建表admin
2、在web中创建登录页面login.html
3、在web中创建CSS文件夹,在CSS文件夹中创建login.css
4、在web下新建注册页面register.html
5、在CSS文件夹中新建register.css
6、在CSS文件夹下新建table.css
7、在src下添加db.properties配置文件:数据库连接及连接池配置文件
8、创建www.admin.utils包:创建工具类DBUtils
9、创建www.admin.entity包:实体类Admin
10、创建com.admin.dao包:数据访问层接口AdminDao
11、创建com.admin.dao.impl包:数据访问层接口实现类AdminDaoImpl
12、创建com.admin.service包:业务逻辑层接口AdminService
13、创建com.admin.service.impl包:业务逻辑层接口实现类AdminServiceImpl
14、创建com.admin.servlet包:Servlet类
(1)LoginServlet类:登录
(2)AdminShowAllServlet类:显示所有用户
(3)RegisterServlet类:注册新用户
(4)DeleteServlet类:获取要删除用户的信息
(5)DeleteControllerServlet类:进行删除操作
(6)UpdateShowServlet类:显示要修改的个人信息
(7)UpdateServlet类:进行修改操作
实现登录功能,登录成功后显示所有管理员信息,登录失败给出“账号或密码错误,无法登录”提示信息 。
1、在MySQL中新建一个servletdatabase数据库,创建表admin
CREATE DATABASE servletdatabase;
USE servletdatabase;
CREATE TABLE IF NOT EXISTS `admin`(
`username` VARCHAR(20) PRIMARY KEY,
`password` VARCHAR(20) NOT NULL,
`phone` VARCHAR(11) UNIQUE NOT NULL,
`address` VARCHAR(10) NOT NULL
);
INSERT INTO `admin` VALUES
('张三','123456','18145772645','安徽合肥包河区'),
('李四','456789','15695688968','安徽合肥高新区');
2、在web中创建登录页面login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录页面</title>
<link rel="stylesheet" type="text/css" href="CSS/login.css">
</head>
<body>
<div>
<form action="LoginServlet" method="post">
<p><label>姓名:</label><input type="text" name="username"></p>
<p><label>密码:</label><input type="password" name="password"></p>
<p><input type="submit" value="登录"></p>
</form>
</div>
</body>
</html>
3、在web中创建CSS文件夹,在CSS文件夹中创建login.css
*{
margin: 0;
padding: 0;
}
div{
width: 400px;
height: 100px;
background-color: #ccc;
margin: 30px auto;
padding-top: 30px;
text-align: center;
}
p{
margin-top: 10px;
}
input{
outline: none;
}
4、在web下新建注册页面register.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册</title>
<link rel="stylesheet" type="text/css" href="CSS/register.css">
</head>
<body>
<div>
<form action="RegisterServlet" method="post">
<p><label>姓名:</label><input type="text" name="username"></p>
<p><label>密码:</label><input type="password" name="password"></p>
<p><label>手机:</label><input type="text" name="phone"></p>
<p><label>住址:</label><input type="text" name="address"></p>
<p><input type="submit" value="注册"></p>
</form>
</div>
</body>
</html>
5、在CSS文件夹中新建register.css
*{
margin: 0;
padding: 0;
}
div{
width: 400px;
height: 200px;
background-color: #ccc;
margin: 30px auto;
padding-top: 30px;
text-align: center;
}
p{
margin-top: 10px;
}
input{
outline: none;
}
6、在CSS文件夹下新建table.css
*{
margin: 0;
padding: 0;
}
table{
margin: 20px auto;
width: 500px;
height: 100px;
text-align: center;
}
7、在src下添加db.properties配置文件:数据库连接及连接池配置文件
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/servletdatabase
username=root
password=123456
initialSize=10
maxActive=30
maxIdle=5
maxWait=3000
8、创建www.admin.utils包:创建工具类DBUtils
package com.admin.utils;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class DBUtils {
// 声明一个连接池对象
private static DruidDataSource druidDataSource;
static {
// 实例化配置文件对象
Properties properties = new Properties();
try {
// 加载配置文件内容
InputStream is = DBUtils.class
.getResourceAsStream("/db.properties");
properties.load(is);
// 创建连接池
druidDataSource = (DruidDataSource) DruidDataSourceFactory
.createDataSource(properties);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
//返回一个数据源
public static DataSource getDataSource(){
return druidDataSource;
}
}
9、创建www.admin.entity包:实体类Admin
package com.admin.entity;
public class Admin {
private String username;
private String password;
private String phone;
private String address;
public Admin() {
}
public Admin(String username, String password, String phone, String address) {
this.username = username;
this.password = password;
this.phone = phone;
this.address = address;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Admin{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", phone='" + phone + '\'' +
", address='" + address + '\'' +
'}';
}
}
10、创建com.admin.dao包:数据访问层接口AdminDao
package com.admin.dao;
import com.admin.entity.Admin;
import java.util.List;
public interface AdminDao {
//登录查询姓名和密码
public Admin selectLogin(String username,String password);
//查询所有
public List<Admin> selectAll();
//注册用户
public int insert(Admin admin);
//以手机号码为条件删除用户
public int delete(String phone);
//修改用户信息
public int update(Admin admin);
//以手机号码查询用户
public Admin selectPhone(String phone);
}
11、创建com.admin.dao.impl包:数据访问层接口实现类AdminDaoImpl
package com.admin.dao.impl;
import com.admin.dao.AdminDao;
import com.admin.entity.Admin;
import com.admin.utils.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;
public class AdminDaoImpl implements AdminDao {
//创建QueryRunner对象,并传递一个数据源
QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());
@Override
public Admin selectLogin(String username, String password) {
String sql = "SELECT * FROM `admin` WHERE username=? AND `password`=?;";
Object[] args = {username,password};
try {
return queryRunner.query(sql,new BeanHandler<Admin>(Admin.class),args);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
@Override
public List<Admin> selectAll() {
String sql = "select * from admin;";
try {
return queryRunner.query(sql,new BeanListHandler<Admin>(Admin.class));
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
@Override
public int insert(Admin admin) {
String sql = "insert into admin values(?,?,?,?)";
Object[] args = {admin.getUsername(),admin.getPassword(),admin.getPhone(),admin.getAddress()};
try {
return queryRunner.update(sql,args);
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
@Override
public int delete(String phone) {
String sql = "delete from admin where phone=?;";
try {
return queryRunner.update(sql,phone);
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
@Override
public int update(Admin admin) {
String sql = "update admin set `username`=?,`password`=?,`address`=? where phone=?;";
Object[] args = {admin.getUsername(),admin.getPassword(),admin.getAddress(),admin.getPhone()};
try {
return queryRunner.update(sql,args);
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
@Override
public Admin selectPhone(String phone) {
String sql = "select * from admin where phone=?";
try {
return queryRunner.query(sql,new BeanHandler<Admin>(Admin.class),phone);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
12、创建com.admin.service包:业务逻辑层接口AdminService
package com.admin.service;
import com.admin.entity.Admin;
import java.util.List;
public interface AdminService {
public Admin login(String username,String password);
public List<Admin> selectAdmin();
public int addAdmin(Admin admin);
public int deleteAdmin(String phone);
public int updateAdmin(Admin admin);
public Admin selectOne(String phone);
}
13、创建com.admin.service.impl包:业务逻辑层接口实现类AdminServiceImpl
package com.admin.service.impl;
import com.admin.dao.AdminDao;
import com.admin.dao.impl.AdminDaoImpl;
import com.admin.entity.Admin;
import com.admin.service.AdminService;
import java.util.List;
public class AdminServiceImpl implements AdminService {
AdminDao adminDao = new AdminDaoImpl();
@Override
public Admin login(String username, String password) {
Admin admin = adminDao.selectLogin(username, password);
if (admin != null){
System.out.println("登陆成功");
return adminDao.selectLogin(username, password);
}else {
System.out.println("登陆失败");
}
return null;
}
@Override
public List<Admin> selectAdmin() {
return adminDao.selectAll();
}
@Override
public int addAdmin(Admin admin) {
Admin admin1 = adminDao.selectPhone(admin.getPhone());
if (admin1 != null){
System.out.println("添加失败");
}else {
System.out.println("添加成功");
return adminDao.insert(admin);
}
return 0;
}
@Override
public int deleteAdmin(String phone) {
Admin admin = adminDao.selectPhone(phone);
if (admin != null){
System.out.println("删除成功");
return adminDao.delete(phone);
}else {
System.out.println("删除失败");
}
return 0;
}
@Override
public int updateAdmin(Admin admin) {
Admin admin1 = adminDao.selectPhone(admin.getPhone());
if (admin1 != null){
System.out.println("修改成功");
return adminDao.update(admin);
}else {
System.out.println("修改失败");
}
return 0;
}
@Override
public Admin selectOne(String phone) {
Admin admin = adminDao.selectPhone(phone);
if (admin != null){
System.out.println("用户存在");
return adminDao.selectPhone(phone);
}else {
System.out.println("无记录");
}
return null;
}
}
14、创建com.admin.servlet包:Servlet类
(1)LoginServlet类:登录
package com.admin.servlet;
import com.admin.entity.Admin;
import com.admin.service.AdminService;
import com.admin.service.impl.AdminServiceImpl;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet {
//创建业务逻辑层AdminService对象
AdminService adminService = new AdminServiceImpl();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编码格式
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//获取输出流
PrintWriter printWriter = response.getWriter();
//获取浏览器数据
String username = request.getParameter("username");
String password = request.getParameter("password");
//输出获取到的数据
System.out.println("username:"+username+"\npassword:"+password);
//调用业务逻辑层login方法,传参username,password进行查询数据库
Admin admin = adminService.login(username,password);
//判断在数据库中是否查询到记录
if (admin != null){
//查询到记录在页面显示登录成功
printWriter.println("<h2>登录成功</h2>");
}else {
//未查询到记录在页面显示登陆失败
printWriter.println("<h2>登陆失败</h2>");
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
(2)AdminShowAllServlet类:显示所有用户
package com.admin.servlet;
import com.admin.entity.Admin;
import com.admin.service.AdminService;
import com.admin.service.impl.AdminServiceImpl;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
//查询所有用户
@WebServlet(name = "AdminShowAllServlet", value = "/AdminShowAllServlet")
public class AdminShowAllServlet extends HttpServlet {
//创建业务逻辑层AdminService对象
AdminService adminService = new AdminServiceImpl();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编码格式
response.setContentType("text/html;charset=utf-8");
//获取输出流
PrintWriter printWriter = response.getWriter();
//获取数据库中的全部记录
List<Admin> adminList = adminService.selectAdmin();
//处理获取到的集合
if (adminList.size() != 0){
printWriter.println("<html>");
printWriter.println("<head>");
printWriter.println("<title>所有admin</title>");
printWriter.println("<link rel='stylesheet' type='text/css' href='CSS/table.css' />");
printWriter.println("</head>");
printWriter.println("<a href='/adminproject_war_exploded/register.html'>注册</a>");
printWriter.println("<table border='1px' cellspacing='0'>");
printWriter.println("<tr>");
printWriter.println("<th>账号</th>");
printWriter.println("<th>密码</th>");
printWriter.println("<th>手机号码</th>");
printWriter.println("<th>住址</th>");
printWriter.println("<th>操作</th>");
printWriter.println("</tr>");
for (Admin admin:adminList){
printWriter.println("<tr>");
printWriter.println("<td>"+admin.getUsername()+"</td>");
printWriter.println("<td>" + admin.getPassword() + "</td>");
printWriter.println("<td>" + admin.getPhone() + "</td>");
printWriter.println("<td>" + admin.getAddress() + "</td>");
printWriter.println("<td><a href='/adminproject_war_exploded/UpdateShowServlet?phone="+admin.getPhone()+"'>修改</a> <a href='/adminproject_war_exploded/DeleteServlet?phone="+admin.getPhone()+"'>删除</a></td>");
printWriter.println("</tr>");
}
printWriter.println("</table>");
printWriter.println("</html>");
}else {
printWriter.println("<html>");
printWriter.println("<head>");
printWriter.println("<title>所有admin</title>");
printWriter.println("</head>");
printWriter.println("<body>");
printWriter.println("<h2>当前没有用户</h2>");
printWriter.println("</body>");
printWriter.println("</html>");
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
(3)RegisterServlet类:注册新用户
package com.admin.servlet;
import com.admin.entity.Admin;
import com.admin.service.AdminService;
import com.admin.service.impl.AdminServiceImpl;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "RegisterServlet", value = "/RegisterServlet")
public class RegisterServlet extends HttpServlet {
//创建业务逻辑层AdminService对象
AdminService adminService = new AdminServiceImpl();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编码格式
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//收参
String username = request.getParameter("username");
String password = request.getParameter("password");
String phone = request.getParameter("phone");
String address = request.getParameter("address");
//在控制台输出接收的参数
System.out.println("username:"+username+"\npassword:"+password+"\nphone:"+phone+"\naddress"+address);
//创建Admin对象
Admin admin = new Admin(username,password,phone,address);
//调用业务逻辑层的addAdmin方法
int num = adminService.addAdmin(admin);
//声明输出流
PrintWriter printWriter = response.getWriter();
//处理返回的数据
if (num == 1){
printWriter.println("<html>");
printWriter.println("<head>");
printWriter.println("<title>注册成功</title>");
printWriter.println("</head>");
printWriter.println("<body>");
printWriter.println("<h2>注册成功</h2>");
printWriter.println("<a href='/adminproject_war_exploded/AdminShowAllServlet'>返回首页</a>");
printWriter.println("</body>");
printWriter.println("</html>");
}else {
printWriter.println("<html>");
printWriter.println("<head>");
printWriter.println("<title>注册失败</title>");
printWriter.println("</head>");
printWriter.println("<body>");
printWriter.println("<h2>注册失败</h2>");
printWriter.println("<a href='/adminproject_war_exploded/AdminShowAllServlet'>返回首页</a>");
printWriter.println("</body>");
printWriter.println("</html>");
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
(4)DeleteServlet类:获取要删除用户的信息
package com.admin.servlet;
import com.admin.service.AdminService;
import com.admin.service.impl.AdminServiceImpl;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "DeleteServlet", value = "/DeleteServlet")
public class DeleteServlet extends HttpServlet {
//创建业务逻辑层AdminService对象
AdminService adminService = new AdminServiceImpl();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编码格式
response.setContentType("text/html;charset=utf-8");
//收参
String phone = request.getParameter("phone");
//声明输出流
PrintWriter printWriter = response.getWriter();
printWriter.println("<html>");
printWriter.println("<head>");
printWriter.println("<title>删除admin</title>");
printWriter.println("</head>");
printWriter.println("<body>");
printWriter.println("<h2>确定要删除用户:"+phone+"吗?</h2>");
printWriter.println("<a href='/adminproject_war_exploded/AdminShowAllServlet'>取消</a>");
printWriter.println("<a href='/adminproject_war_exploded/DeleterControllerServlet?phone="+phone+"'>确定</a>");
printWriter.println("</body>");
printWriter.println("</html>");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
(5)DeleteControllerServlet类:进行删除操作
package com.admin.servlet;
import com.admin.entity.Admin;
import com.admin.service.AdminService;
import com.admin.service.impl.AdminServiceImpl;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "DeleterControllerServlet", value = "/DeleterControllerServlet")
public class DeleterControllerServlet extends HttpServlet {
//创建业务逻辑层AdminService对象
AdminService adminService = new AdminServiceImpl();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编码格式
response.setContentType("text/html;charset=utf-8");
//收参
String phone = request.getParameter("phone");
//声明输出流
PrintWriter printWriter = response.getWriter();
//删除数据库中的记录
int num = adminService.deleteAdmin(phone);
//处理返回的数据
if (num == 1){
printWriter.println("<html>");
printWriter.println("<head>");
printWriter.println("<title>删除成功</title>");
printWriter.println("</head>");
printWriter.println("<body>");
printWriter.println("<h2>删除成功</h2>");
printWriter.println("<a href='/adminproject_war_exploded/AdminShowAllServlet'>返回首页</a>");
printWriter.println("</body>");
printWriter.println("</html>");
}else {
printWriter.println("<html>");
printWriter.println("<head>");
printWriter.println("<title>删除失败</title>");
printWriter.println("</head>");
printWriter.println("<body>");
printWriter.println("<h2>删除失败</h2>");
printWriter.println("<a href='/adminproject_war_exploded/AdminShowAllServlet'>返回首页</a>");
printWriter.println("</body>");
printWriter.println("</html>");
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
(6)UpdateShowServlet类:显示要修改的个人信息
package com.admin.servlet;
import com.admin.entity.Admin;
import com.admin.service.AdminService;
import com.admin.service.impl.AdminServiceImpl;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "UpdateShowServlet", value = "/UpdateShowServlet")
public class UpdateShowServlet extends HttpServlet {
//创建业务逻辑层AdminService对象
AdminService adminService = new AdminServiceImpl();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编码格式
response.setContentType("text/html;charset=utf-8");
//声明输出流
PrintWriter printWriter = response.getWriter();
//收参
String phone = request.getParameter("phone");
//查询phone相关的记录
Admin admin = adminService.selectOne(phone);
//在页面中显示修改信息
printWriter.println("<html>");
printWriter.println("<head>");
printWriter.println("<title>修改admin</title>");
printWriter.println("</head>");
printWriter.println("<body>");
printWriter.println("<div><form action='UpdateServlet' method='post'><h2>信息修改</h2>");
printWriter.println("<p><label>姓名:</label><input type='text' name='username' value='"+admin.getUsername()+"'></p>");
printWriter.println("<p><label>密码:</label><input type='password' name='password' value='"+admin.getPassword()+"'></p>");
//给phone设置只读属性,用户不能修改
printWriter.println("<p><label>手机:</label><input type='text' name='phone' readonly='readonly' value='"+admin.getPhone()+"'></p>");
printWriter.println("<p><label>住址:</label><input type='text' name='address' value='"+admin.getAddress()+"'></p>");
printWriter.println("<p><input type='submit' value='修改'></p>");
printWriter.println("</div></body>");
printWriter.println("</html>");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
(7)UpdateServlet类:进行修改操作
package com.admin.servlet;
import com.admin.entity.Admin;
import com.admin.service.AdminService;
import com.admin.service.impl.AdminServiceImpl;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "UpdateServlet", value = "/UpdateServlet")
public class UpdateServlet extends HttpServlet {
//创建业务逻辑层AdminService对象
AdminService adminService = new AdminServiceImpl();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编码格式
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//声明输出流
PrintWriter printWriter = response.getWriter();
//收参
String username = request.getParameter("username");
String password = request.getParameter("password");
String phone = request.getParameter("phone");
String address = request.getParameter("address");
//创建Admin对象
Admin admin = new Admin(username,password,phone,address);
//调用业务逻辑层的updateAdmin方法
int num = adminService.updateAdmin(admin);
//处理返回结果
if (num == 1){
printWriter.println("<html>");
printWriter.println("<head>");
printWriter.println("<title>修改成功</title>");
printWriter.println("</head>");
printWriter.println("<body>");
printWriter.println("<h2>修改成功</h2>");
printWriter.println("<a href='/adminproject_war_exploded/AdminShowAllServlet'>返回首页</a>");
printWriter.println("</body>");
printWriter.println("</html>");
}else {
printWriter.println("<html>");
printWriter.println("<head>");
printWriter.println("<title>修改失败</title>");
printWriter.println("</head>");
printWriter.println("<body>");
printWriter.println("<h2>修改失败</h2>");
printWriter.println("<a href='/adminproject_war_exploded/AdminShowAllServlet'>返回首页</a>");
printWriter.println("</body>");
printWriter.println("</html>");
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}