/*
SQLyog Ultimate v13.1.1 (64 bit)
MySQL - 5.7.29 : Database - contact_sys
*********************************************************************
*/
/*!40101 SET NAMES utf8 */;
/*!40101 SET SQL_MODE=''*/;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`contact_sys` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `contact_sys`;
/*Table structure for table `client` */
DROP TABLE IF EXISTS `client`;
CREATE TABLE `client` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(32) DEFAULT NULL,
`password` varchar(32) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
/*Data for the table `client` */
insert into `client`(`id`,`username`,`password`) values
(1,'张三','123456');
/*Table structure for table `contact` */
DROP TABLE IF EXISTS `contact`;
CREATE TABLE `contact` (
`cid` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`gender` varchar(4) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`phone` varchar(11) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`qq` varchar(255) DEFAULT NULL,
PRIMARY KEY (`cid`)
) ENGINE=InnoDB AUTO_INCREMENT=36 DEFAULT CHARSET=utf8;
/*Data for the table `contact` */
insert into `contact`(`cid`,`name`,`gender`,`age`,`phone`,`email`,`qq`) values
(28,'科比','男',34,'1231','1346888','ce88029a735b44d3b553a917911b13f30d6583f95051415da21b8fcdbcae8a8d4.png'),
(29,'户','男',34,'1516152','[email protected]','4083a5d95fdb4442ae937bc0da1904102bafe2f833de4744878e3142d2ce488e74eacec67ede492f88c9be61c2602adfea21bcc015944f3091c270736d52c3ec5.png'),
(30,'张三','男',7848,'1551674585','1346888','9e1ae3313c5e4793ae94be0f4be69d96b59d1750411a46ff88ad3b716cf632c23.png'),
(31,'打','男',24,'1551674585','sdada13@162','53b1005dbd6a4923b782a067cdf48e0150b979f34c214911b6bf6c07035c560659cd76a4281f46eda598540a91cd50272.png');
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
package com.javacoffee.pojo;
public class Client {
//创建Client类 和数据库中的client表对应好
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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;
}
}
package com.javacoffee.pojo;
public class Contact {
//创建Contact类 和数据库中的字段名对应好
private Integer cid;
private String name;
private String gender;
private Integer age;
private String phone;
private String email;
private String qq;
public Contact() {
}
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
@Override
public String toString() {
return "Contact{" +
"cid=" + cid +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
", phone='" + phone + '\'' +
", email='" + email + '\'' +
", qq='" + qq + '\'' +
'}';
}
}
package com.javacoffee.dao;
import com.javacoffee.pojo.Client;
public interface ClientDao {
Client login(String username, String password);
}
package com.javacoffee.dao;
import com.javacoffee.pojo.Contact;
import com.javacoffee.util.PageBean;
import java.util.List;
public interface ContactDao {
//面向接口编程
public void addContact(Contact contact);//添加联系人
public void updateContact(Contact contact);//修改联系人
public void deleteContact(Integer cid);//删除联系人
public List findAll(); //查询所有联系人
public Contact findById(Integer cid);//根据编号查询联系人
public PageBean findByPage(PageBean pageBean);//分页
public void deleteAll(String[] cids);//删除全选
Contact selectPhone(String phone);
}
package com.javacoffee.dao.impl;
import com.javacoffee.dao.ClientDao;
import com.javacoffee.pojo.Client;
import com.javacoffee.util.JdbcUtil;
import org.apache.commons.dbutils.handlers.BeanHandler;
import java.sql.SQLException;
public class ClientDaoImpl implements ClientDao {
@Override
public Client login(String username, String password) {
//登陆的查询语句
String sql = "select * from client where username = ? and password = ?";
Object[] objects = {username,password};
try {
return JdbcUtil.getQueryRunner().query(sql, new BeanHandler(Client.class), objects);
} catch (SQLException e) {
e.printStackTrace();
}
throw new RuntimeException("sql语句异常");
}
}
package com.javacoffee.dao.impl;
import com.javacoffee.dao.ContactDao;
import com.javacoffee.pojo.Contact;
import com.javacoffee.util.JdbcUtil;
import com.javacoffee.util.PageBean;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import java.sql.SQLException;
import java.util.List;
public class ContactDaoImpl implements ContactDao {
//添加联系人的方法
@Override
public void addContact(Contact contact) {
//sql语句 后面加上参数
String sql = "insert into contact(name,gender,age,phone,email,qq) values(?,?,?,?,?,?)";
//创建一个object数组,并把需要传的参数放入数组中
Object[] objects = {contact.getName(),contact.getGender(),contact.getAge(),contact.getPhone(),contact.getEmail(),contact.getQq()};
try {
//执行sql语句 我导入了dbUtil包,所以直接得到queryRunner对象执行sql语句
JdbcUtil.getQueryRunner().update(sql,objects);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void updateContact(Contact contact) {
//修改的sql语句
String sql = "update contact set name = ?,gender = ?,age= ?,phone = ?,email = ?,qq = ? where cid = ?";
//和增加很一样,先赋值到数组 再执行sql语句
Object[] objects = {contact.getName(),contact.getGender(),contact.getAge(),contact.getPhone(),contact.getEmail(),contact.getQq(),contact.getCid()};
try {
JdbcUtil.getQueryRunner().update(sql,objects);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void deleteContact(Integer cid) {
//删除sql语句
String sql = "delete from contact where cid = ?";
try {
//传参可以直接传入sql语句和 cid形式参数
JdbcUtil.getQueryRunner().update(sql,cid);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public List findAll() {
//返回值为List集合 sql语句如下
String sql = "select * from contact";
try {
//也是通过dbUtil工具类来执行sql语句,因为输出的为List集合,所以第一个参数为sql语句 第二个如下
return JdbcUtil.getQueryRunner().query(sql, new BeanListHandler(Contact.class));
} catch (SQLException e) {
e.printStackTrace();
}
throw new RuntimeException("列表无数据");
}
@Override
public Contact findById(Integer cid) {
//这个是通过id来进行查找
String sql = "select * from contact where cid = ?";
try {
//返回值为一个对象 所以第二个参数也变成如下了,第三个参数放入cid
return JdbcUtil.getQueryRunner().query(sql, new BeanHandler(Contact.class), cid);
} catch (SQLException e) {
e.printStackTrace();
}
throw new RuntimeException("没有这个id");
}
@Override
public PageBean findByPage(PageBean pageBean) {
//分页 这里是通过sql语句分的 比较基础
String sql = "select * from contact limit ?,?";
//传入的是PageBean对象,来获取当前页号和每页的记录数
//大家可一思考一下 select * from contact limit 0,3 是查找第一个到第三个的数据
// select * from contact limit 3,3 是查找第四个到第六个的 以此类推
Object[] objects = {(pageBean.getPageNum()-1)*pageBean.getPageSize(),pageBean.getPageSize()};
try {
//传入每页的数据
List query = JdbcUtil.getQueryRunner().query(sql, new BeanListHandler(Contact.class), objects);
pageBean.setPageData(query);
//查找数据总数
String sql1 = "select count(cid) from contact";
//强转为Long类型数据
Long query1 = (Long)JdbcUtil.getQueryRunner().query(sql1, new ScalarHandler<>());
//将查找出来的数据再转化为int类型数据存入pageBean中
pageBean.setTotalData(query1.intValue());
//可以在控制台打印一下看看是不是存入
// System.out.println(pageBean.getTotalData());
//计算总页数 用了三目运算符
//当 总数据数除以每页的数据数结果余数为0时候,说明总页数是总数据数除以每页的数据数结果取整
//反之则需要加一
Integer totalCount = pageBean.getTotalData()%pageBean.getPageSize()==0 ?
pageBean.getTotalData()/pageBean.getPageSize() :
pageBean.getTotalData()/pageBean.getPageSize() + 1;
//然后赋值
pageBean.setTotalCount(totalCount);
//返回这个对象
return pageBean;
} catch (SQLException e) {
e.printStackTrace();
}
throw new RuntimeException("无法获取对象");
}
@Override
public void deleteAll(String[] cids) {
//输入sql语句 通过cid来进行多个删除 可以看到传入的参数为cids是一个String类型的数组
String sql = "delete from contact where cid = ?";
//创建一个二维数组,这个数组的长度为cids中的数据数
Object[][] objects = new Object[cids.length][];
//通过循环来对二维数组进行赋值
for (int i = 0; i < objects.length; i++) {
objects[i] = new Object[]{cids[i]};
}
try {
//执行sql语句
JdbcUtil.getQueryRunner().batch(sql,objects);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public Contact selectPhone(String phone) {
String sql = "select * from contact where phone = ?";
try {
Contact query = JdbcUtil.getQueryRunner().query(sql, new BeanHandler(Contact.class), phone);
return query;
} catch (SQLException e) {
e.printStackTrace();
}
throw new RuntimeException("查找异常");
}
}
package com.javacoffee.exception;
public class MyException extends Exception {
public MyException(String messege){
super(messege);
}
}
package com.javacoffee.service;
import com.javacoffee.exception.MyException;
import com.javacoffee.pojo.Contact;
import com.javacoffee.util.PageBean;
import java.util.List;
public interface ContactService {
//service层我并没写什么逻辑行为,因为比较简单,就起到了个传值的作用
public void addContact(Contact contact) throws MyException;//添加联系人
public void updateContact(Contact contact);//修改联系人
public void deleteContact(Integer cid);//删除联系人
public List findAll(); //查询所有联系人
public Contact findById(Integer cid);//根据编号查询联系人
public PageBean findByPage(PageBean pageBean);//分页
public void deleteAll(String[] cids);//删除所选人
Boolean selectPhone(String phone);
}
package com.javacoffee.service;
import com.javacoffee.pojo.Client;
public interface ClientService {
//获取对象
Client login(String username, String password);
}
package com.javacoffee.service.impl;
import com.javacoffee.dao.ClientDao;
import com.javacoffee.dao.impl.ClientDaoImpl;
import com.javacoffee.pojo.Client;
import com.javacoffee.service.ClientService;
public class ClientServiceImpl implements ClientService {
@Override
public Client login(String username, String password) {
ClientDao clientDao = new ClientDaoImpl();
return clientDao.login(username,password);
}
}
package com.javacoffee.service.impl;
import com.javacoffee.dao.ContactDao;
import com.javacoffee.dao.impl.ContactDaoImpl;
import com.javacoffee.exception.MyException;
import com.javacoffee.pojo.Contact;
import com.javacoffee.service.ContactService;
import com.javacoffee.util.PageBean;
import java.util.List;
public class ContactServiceImpl implements ContactService {
//首先创建 多态父类引用指向子类对象 new出来contactDao对象
private ContactDao contactDao = new ContactDaoImpl();
//增加联系人的方法
@Override
public void addContact(Contact contact) throws MyException {
Boolean aBoolean = selectPhone(contact.getPhone());
if (aBoolean){
contactDao.addContact(contact);
}else {
throw new MyException("手机号重复,不能使用!");
}
}
//修改联系人的方法
@Override
public void updateContact(Contact contact) {
contactDao.updateContact(contact);
}
//删除联系人的方法
@Override
public void deleteContact(Integer cid) {
contactDao.deleteContact(cid);
}
//查找全部的方法
@Override
public List findAll() {
return contactDao.findAll();
}
//通过id查找的方法
@Override
public Contact findById(Integer cid) {
return contactDao.findById(cid);
}
//分页
@Override
public PageBean findByPage(PageBean pageBean) {
return contactDao.findByPage(pageBean);
}
@Override
//删除所选全部
public void deleteAll(String[] cids) {
contactDao.deleteAll(cids);
}
@Override
public Boolean selectPhone(String phone) {
Contact contact = contactDao.selectPhone(phone);
if (contact == null){
return true;
}else {
return false;
}
}
}
package com.javacoffee.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter("/*")
public class ContactFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
//字符集过滤器 过滤所有 将字符集设置成utf-8
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)resp;
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
chain.doFilter(request,response);
}
public void init(FilterConfig config) throws ServletException {
}
}
package com.javacoffee.filter;
import com.javacoffee.pojo.Client;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebFilter("/*")
public class LoginFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
//登陆的过滤器 因为要确保先登录 所以将网址不是登录的重定向到登录界面
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)resp;
//获取后缀
String requestURI = request.getRequestURI();
requestURI = requestURI.substring(requestURI.lastIndexOf("/"));
//判断如果输入的网址后缀是这两个 则放行
if ("/loginServlet".equals(requestURI)|| "/login.jsp".equals(requestURI)){
chain.doFilter(request,response);
}else {
//如果不是的话 因为我们在登录的时候创建了session 如果session为空
//那么一定是没有登录的 所以不放行重定向到登陆界面
HttpSession session = request.getSession(false);
if (session == null){
response.sendRedirect(request.getContextPath()+"/loginServlet");
}else {
//如果存在session 对象 不过有可能session对象里是在别的地方创建好的
//所以我们要判断session里是否存储了我们登陆时存进去的client对象
//如果没有的话那还是没登陆 就重定向到登陆界面
Client client = (Client)session.getAttribute("client");
if (client == null){
response.sendRedirect(request.getContextPath()+"/loginServlet");
}else {
//反之 就是登录过的 就可以放行
chain.doFilter(request,response);
}
}
}
}
public void init(FilterConfig config) throws ServletException {
}
}
package com.javacoffee.util;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.apache.commons.dbutils.QueryRunner;
import javax.sql.DataSource;
import java.io.InputStream;
import java.util.Properties;
//使用DBUtils的工具类
public class JdbcUtil {
//声明DataSource对象
private static DataSource dataSource;
//静态代码块
static {
try {
//读取db.properties文件
InputStream resourceAsStream = JdbcUtil.class.getResourceAsStream("/druid.properties");
//创建Properties对象
Properties properties = new Properties();
//加载流对象
properties.load(resourceAsStream);
//创建数据源对象
dataSource = DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
}
}
//获取DButils中核心类对象QueryRunner对象
public static QueryRunner getQueryRunner(){
return new QueryRunner(dataSource);//此时已经连接上数据库了
}
}
package com.javacoffee.util;
public class PageBean {
//这是分页的工具类,一共需要获得五个属性
private Integer pageNum;//当前页码号 通过前端获取
private Integer pageSize;//一页的记录数 自己设置的
private Object pageData;//一页的数据 可以看到返回值是对象
private Integer totalCount;//总页数 需要二次计算
private Integer totalData;//数据的总记录数 用来计算总页数用的
public Integer getPageNum() {
return pageNum;
}
public void setPageNum(Integer pageNum) {
this.pageNum = pageNum;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Object getPageData() {
return pageData;
}
public void setPageData(Object pageData) {
this.pageData = pageData;
}
public Integer getTotalCount() {
return totalCount;
}
public void setTotalCount(Integer totalCount) {
this.totalCount = totalCount;
}
public Integer getTotalData() {
return totalData;
}
public void setTotalData(Integer totalData) {
this.totalData = totalData;
}
@Override
public String toString() {
return "PageBean{" +
"pageNum=" + pageNum +
", pageSize=" + pageSize +
", pageData=" + pageData +
", totalCount=" + totalCount +
", totalData=" + totalData +
'}';
}
}
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/contact_sys?useSSL=false
#我是使用的mysql 所以端口号为3306 数据库账户密码一定要改成自己的哈
username=root
password=1234
initialSize=5
maxActive=20
maxWait=2000
package com.javacoffee.web;
import com.javacoffee.exception.MyException;
import com.javacoffee.pojo.Contact;
import com.javacoffee.service.ContactService;
import com.javacoffee.service.impl.ContactServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
//注意这两个注解千万不能省略
@WebServlet("/addContactServlet")//这个是servlet的注解,可以访问的到
@MultipartConfig//这个是代表上传文件的注解
public class AddContactServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//这是增加联系人的servlet 首先设置字符集
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//新建一个contact对象 用来存储数据库中获取的数据
Contact contact = new Contact();
//通过前端所对应的键名来获取相应的值
String name = request.getParameter("name");
String gender = request.getParameter("gender");
String age = request.getParameter("age");
String phone = request.getParameter("phone");
String email = request.getParameter("email");
//这里是上传文件 获取上传的对象
Part qq = request.getPart("qq");
//获取所上传文件的名字
String qqName = qq.getSubmittedFileName();
//为了避免上传的文件后,文件重复 每次上传文件都会通过UUID 工具类获取一个随机的字符串作为前缀名
String replace = UUID.randomUUID().toString().replace("-", "");
//我是把文件上传到另一个tomcat服务器里 所以这里把他的路径给出来了
String uploadPath = "D:\\apache-tomcat-8.5.31\\webapps\\imgs";
//通过路径创建一个新的文件对象
File file = new File(uploadPath);
//如果没有这个文件的话,就创建
if (!file.exists()){
file.mkdirs();
}
//最后组成文件的名字
String uploadFileName = replace+qqName;
//然后给他上传一下,参数为(路径+文件名) 注意这两个之间要加斜杠
qq.write(uploadPath+"/"+uploadFileName);
//接着赋值给对象
contact.setName(name);
contact.setAge(Integer.parseInt(age));
contact.setEmail(email);
contact.setGender(gender);
contact.setPhone(phone);
contact.setQq(uploadFileName);
//接着调用addContact方法
ContactService contactService = new ContactServiceImpl();
try {
contactService.addContact(contact);
} catch (MyException e) {
request.setAttribute("msg",e.getMessage());
request.getRequestDispatcher("addContact.jsp").forward(request,response);
}
//因为不需要传值 重定向到首页面
response.sendRedirect(request.getContextPath()+"/pageServlet");
}
}
package com.javacoffee.web;
import com.javacoffee.service.ContactService;
import com.javacoffee.service.impl.ContactServiceImpl;
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 java.io.IOException;
import java.util.Arrays;
@WebServlet("/contactDeleteAllServlet")
public class ContactDeleteAllServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String[] cids = request.getParameterValues("cids");
if (cids != null) {
ContactService contactService = new ContactServiceImpl();
contactService.deleteAll(cids);
response.sendRedirect(request.getContextPath()+"/pageServlet");
}else {
response.sendRedirect(request.getContextPath() + "/pageServlet");
}
}
}
package com.javacoffee.web;
import com.javacoffee.service.ContactService;
import com.javacoffee.service.impl.ContactServiceImpl;
import com.javacoffee.util.PageBean;
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 java.io.IOException;
@WebServlet("/contactDeleteServlet")
public class ContactDeleteServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
ContactService contactService = new ContactServiceImpl();
String cid = request.getParameter("cid");
contactService.deleteContact(Integer.parseInt(cid));
response.sendRedirect(request.getContextPath()+"/pageServlet");
}
}
package com.javacoffee.web;
import com.javacoffee.pojo.Contact;
import com.javacoffee.service.ContactService;
import com.javacoffee.service.impl.ContactServiceImpl;
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 java.io.IOException;
import java.util.List;
@WebServlet("/contactFindAllServlet")
public class ContactFindAllServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
ContactService contactService = new ContactServiceImpl();
List contactList = contactService.findAll();
request.setAttribute("contactList",contactList);
request.getRequestDispatcher("findAll.jsp").forward(request,response);
}
}
package com.javacoffee.web;
import com.javacoffee.pojo.Contact;
import com.javacoffee.service.ContactService;
import com.javacoffee.service.impl.ContactServiceImpl;
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 java.io.IOException;
@WebServlet("/findByIdServlet")
public class FindByIdServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
ContactService contactService = new ContactServiceImpl();
String cid = request.getParameter("cid");
Contact contact = contactService.findById(Integer.parseInt(cid));
request.setAttribute("contact",contact);
request.getRequestDispatcher("update.jsp").forward(request,response);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
package com.javacoffee.web;
import com.javacoffee.pojo.Client;
import com.javacoffee.pojo.Contact;
import com.javacoffee.service.ClientService;
import com.javacoffee.service.ContactService;
import com.javacoffee.service.impl.ClientServiceImpl;
import com.javacoffee.service.impl.ContactServiceImpl;
import javax.servlet.RequestDispatcher;
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 java.io.IOException;
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ClientService clientService = new ClientServiceImpl();
//获取前端输入框的数据
String username = request.getParameter("username");
String password = request.getParameter("password");
//调用login方法 并返回client对象
Client client = clientService.login(username,password);
//如果client不为空的话也就是有这个账号密码 就 将client存入session 转发到主界面 也就是登录成功跳转
if (client != null){
HttpSession session = request.getSession();
session.setAttribute("client",client);
request.getRequestDispatcher("pageServlet").forward(request,response);
}else {
//如果为空就代表没有这个代码 就重定向到登陆界面
response.sendRedirect(request.getContextPath()+"/login.jsp");
}
}
}
package com.javacoffee.web;
import com.javacoffee.pojo.Client;
import com.javacoffee.service.ContactService;
import com.javacoffee.service.impl.ContactServiceImpl;
import com.javacoffee.util.PageBean;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
@WebServlet("/pageServlet")
public class PageServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String pageNum = request.getParameter("pageNum");
if (pageNum==null){
pageNum="1";
}
Integer pageSize = 4;
PageBean pageBean1 = new PageBean();
pageBean1.setPageNum(Integer.parseInt(pageNum));
pageBean1.setPageSize(pageSize);
ContactService contactService = new ContactServiceImpl();
PageBean pageBean = contactService.findByPage(pageBean1);
request.setAttribute("pageBean",pageBean);
HttpSession session = request.getSession(false);
Client client = (Client)session.getAttribute("client");
request.setAttribute("client",client);
request.getRequestDispatcher("findAll.jsp").forward(request,response);
}
}
package com.javacoffee.web;
import com.javacoffee.service.ContactService;
import com.javacoffee.service.impl.ContactServiceImpl;
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 java.io.IOException;
@WebServlet("/selectPhoneServlet")
public class SelectPhoneServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String phone = request.getParameter("phone");
ContactService contactService = new ContactServiceImpl();
Boolean statement = contactService.selectPhone(phone);
if (statement){
response.getWriter().write("noExist");
}else {
response.getWriter().write("exist");
}
}
}
package com.javacoffee.web;
import com.javacoffee.pojo.Contact;
import com.javacoffee.service.ContactService;
import com.javacoffee.service.impl.ContactServiceImpl;
import com.javacoffee.util.PageBean;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
@WebServlet("/updateServlet")
@MultipartConfig
public class UpdateServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String cid = request.getParameter("cid");
String name = request.getParameter("name");
String gender = request.getParameter("gender");
String age = request.getParameter("age");
String phone = request.getParameter("phone");
String email = request.getParameter("email");
Part qq = request.getPart("qq");
String updateFilePath = "D:\\apache-tomcat-8.5.31\\webapps\\imgs";
File file = new File(updateFilePath);
if (file.exists()) {
file.mkdirs();
}
String submittedFileName = qq.getSubmittedFileName();
String updateFileName = UUID.randomUUID().toString().replace("-", "") + submittedFileName;
qq.write(updateFilePath + "\\" + updateFileName);
ContactService contactService = new ContactServiceImpl();
Contact contact = new Contact();
contact.setCid(Integer.parseInt(cid));
contact.setName(name);
contact.setAge(Integer.parseInt(age));
contact.setEmail(email);
contact.setGender(gender);
contact.setPhone(phone);
contact.setQq(updateFileName);
contactService.updateContact(contact);
response.sendRedirect(request.getContextPath() + "/pageServlet");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
<%--
Created by IntelliJ IDEA.
User: 86156
Date: 2021/9/30
Time: 15:10
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
登录界面
用户登陆页面
<%--
Created by IntelliJ IDEA.
User: 86156
Date: 2021/9/28
Time: 11:14
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Title
欢迎:${client.username} 登录!
<%--
Created by IntelliJ IDEA.
User: 86156
Date: 2021/9/28
Time: 15:08
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
添加联系人
添加联系人
<%--
Created by IntelliJ IDEA.
User: 86156
Date: 2021/9/28
Time: 19:12
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
修改联系人
修改联系人