JavaWeb实现通讯录
开发工具:idea2020.1 + navicat premium 15
开发技术:servlet + jsp + jdbc
源码地址:通信录源码:https://github.com/mogumogui/JAVAWEB-CONTACTLIST
CSDN下载无需积分:https://download.csdn.net/download/weixin_43305485/13738378
通讯录功能演示:
各部分功能代码演示:
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
个人通信录
<%@ include file="header.jsp" %>
个人通讯录
注册功能
通讯录用户注册
可以通过手机、邮箱、等方式注册。
修改功能
修改通讯录成员、修改用户
可以用户指定信息、并实时返回数据供使用者查看。
增加功能
增加通讯录成员
可以给登陆的用户为自己通讯录新增成员、并填写对应信息。
查询功能
查询通讯录成员
可以根据条件查询并获得通讯录成员信息。
管理员功能
管理用户
可以修改与删除用户的基本信息。
<%@ page contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
注册页面
<%@ include file="header.jsp" %>
RegisterServlet.java
package User.Servlet;
import User.Dao.UserDao;
import User.Domain.User;
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 java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.ResultSet;
import java.sql.SQLException;
@WebServlet(name = "RegisterServlet")
public class RegisterServlet extends HttpServlet {
public RegisterServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setCharacterEncoding("utf-8"); // 设置编码格式
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
String phoneid = request.getParameter("phoneid");
String email = request.getParameter("email");
UserDao userDao = new UserDao();
ResultSet rs = userDao.selectUser(username);
try {
if(rs.next()) {
if (username.equals(rs.getString("username"))) {
request.setAttribute("nameRegisterError", "用户名已经存在!");
request.getRequestDispatcher("register.jsp").forward(request, response);
}
} else {
request.setAttribute("username", username);
User user = new User();
user.setUserName(username);
user.setPassword(password);
user.setPhoneID(phoneid);
user.setEmail(email);
userDao.add(user);
request.getRequestDispatcher("login.jsp").forward(request, response);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
package User.Servlet;
import User.Dao.FriendDao;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class DeletFriendServlet
*/
public class DeleteFriendServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public DeleteFriendServlet() {
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.setCharacterEncoding("utf-8"); // 设置编码格式
request.setCharacterEncoding("utf-8");
// 获取用户传过来的 friendid
int id = Integer.parseInt(request.getParameter("friendid"));
FriendDao friendDao = new FriendDao();
friendDao.detele(id);
response.getWriter().print("删除成功!");
request.getRequestDispatcher("index.jsp").forward(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException {
// TODO Auto-generated method stub
}
}
<%@ page import="User.Domain.Friend" %>
<%@ page import="User.Dao.FriendDao" %>
<%@ page import="java.sql.SQLException" %>
<%@ page contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
修改通讯录用户
<%@ include file="header.jsp" %>
<%
FriendDao friendDao = new FriendDao();
Friend friend = null;
try {
friend = friendDao.getFriendById(request.getParameter("friend"));
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
assert friend != null;
%>
<%@ include file="footer.jsp" %>
EditFriendServlet.java
package User.Servlet;
import User.Dao.FriendDao;
import User.Domain.Friend;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class EditFriendServlet
*/
public class EditFriendServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public EditFriendServlet() {
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
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setCharacterEncoding("utf-8"); // 设置编码格式
request.setCharacterEncoding("utf-8");
// 获取用户传过来的 friend
FriendDao friendDao = new FriendDao();
try {
Friend friend = friendDao.getFriendById(request.getParameter("friendid"));
String friendname = request.getParameter("friendname");
String phoneid = request.getParameter("phoneid");
String qq = request.getParameter("qq");
String email = request.getParameter("email");
friend.setFriendName(friendname);
friend.setPhoneID(phoneid);
friend.setQQ(qq);
friend.setEmail(email);
friendDao.modify(friend);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
request.getRequestDispatcher("index.jsp").forward(request,response);
}
}
<%@page import="User.Domain.Friend"%>
<%@page import="java.util.ArrayList"%>
<%@ page import="User.Domain.Friend" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
个人通信录
<%@ include file="header.jsp" %>
通讯录查询
朋友ID
朋友姓名
电话号码
qq
电子邮箱
创建时间
操作
<%
@SuppressWarnings("unchecked")
ArrayList friendlist = (ArrayList) request.getSession().getAttribute("friendlist");
if(friendlist != null){
for (int i=0; i
<%=friendlist.get(i).getFriendID() %>
<%=friendlist.get(i).getFriendName() %>
<%=friendlist.get(i).getPhoneID() %>
<%=friendlist.get(i).getQQ() %>
<%=friendlist.get(i).getEmail() %>
<%=friendlist.get(i).getCreateDate() %>
修改
删除
<%
}
}
%>
<%@ include file="footer.jsp" %>
IndexServlet.java
package User.Servlet;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import User.Dao.FriendDao;
import User.Dao.UserDao;
import User.Domain.Friend;
import User.Domain.User;
/**
* Servlet implementation class IndexServlet
*/
public class IndexServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public IndexServlet() {
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
response.setCharacterEncoding("utf-8"); // 设置编码格式
request.setCharacterEncoding("utf-8");
Friend friend = new Friend();
FriendDao friendDao = new FriendDao();
UserDao userDao = new UserDao();
ArrayList friendlist;
User user = (User) request.getSession().getAttribute("user");
try {
int userid = userDao.getUserIdByUserName(user);
String friendname = request.getParameter("friendname");
String phoneid = request.getParameter("phoneid");
String qq = request.getParameter("qq");
String email = request.getParameter("email");
friend.setUserID(userid);
friend.setFriendName(friendname);
friend.setPhoneID(phoneid);
friend.setQQ(qq);
friend.setEmail(email);
friendlist = friendDao.serchFriendByCondition(friend);
request.getSession().setAttribute("friendlist",friendlist);
request.getRequestDispatcher("index.jsp").forward(request,response);
} catch (SQLException | ClassNotFoundException throwables) {
throwables.printStackTrace();
}
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
管理员页面
<%@ include file="header.jsp" %>
admin_index.jsp
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
个人通信录
<%@ include file="header.jsp" %>
<%@ include file="footer.jsp" %>