JavaWeb个人通讯录实现(JSP+JDBC)

JavaWeb实现通讯录
开发工具:idea2020.1 + navicat premium 15
开发技术:servlet + jsp + jdbc
源码地址:通信录源码:https://github.com/mogumogui/JAVAWEB-CONTACTLIST
CSDN下载无需积分:https://download.csdn.net/download/weixin_43305485/13738378
通讯录功能演示:

  1. 首页(利用flexibile布局)

JavaWeb个人通讯录实现(JSP+JDBC)_第1张图片

  1. 注册页面(利用js进行表单验证以及通过servlet对用户名进行是否存在校验)
    JavaWeb个人通讯录实现(JSP+JDBC)_第2张图片
    若是表单信息不满足要求则表单提交失败
    JavaWeb个人通讯录实现(JSP+JDBC)_第3张图片
    若注册用户名已存在则提示已存在

  2. 登陆页面(利用servlet进行用户名及密码验证)
    JavaWeb个人通讯录实现(JSP+JDBC)_第4张图片
    JavaWeb个人通讯录实现(JSP+JDBC)_第5张图片
    若是账号不存在或者密码错误都将登陆失败并提示信息!

  3. 修改功能(通过servlet与jdbc对联系人数据库进行修改操作)JavaWeb个人通讯录实现(JSP+JDBC)_第6张图片
    JavaWeb个人通讯录实现(JSP+JDBC)_第7张图片

  4. 删除功能JavaWeb个人通讯录实现(JSP+JDBC)_第8张图片

  5. 查询功能(通讯servlet对通讯录进行条件查询)
    JavaWeb个人通讯录实现(JSP+JDBC)_第9张图片
    可以通过姓名、电话号码、qq、电子邮箱进行查询!

  6. 管理员功能(可以修改删除用户信息)JavaWeb个人通讯录实现(JSP+JDBC)_第10张图片
    JavaWeb个人通讯录实现(JSP+JDBC)_第11张图片

各部分功能代码演示:

  1. main.jsp (js,css文件在上面给的github中)
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>


  
    
    
    

    
    

    
    
    
    
    
    个人通信录
  
  
	<%@ include file="header.jsp" %>
    

注册功能

通讯录用户注册

可以通过手机、邮箱、等方式注册。

修改功能

修改通讯录成员、修改用户

可以用户指定信息、并实时返回数据供使用者查看。

增加功能

增加通讯录成员

可以给登陆的用户为自己通讯录新增成员、并填写对应信息。

查询功能

查询通讯录成员

可以根据条件查询并获得通讯录成员信息。

管理员功能

管理用户

可以修改与删除用户的基本信息。

  1. 注册功能(register.jsp / RegisterServlet.java)
    register.jsp
<%@ page contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>


    
    注册页面
    
    


<%@ include file="header.jsp" %>
    
通讯录用户注册
${requestScope.nameRegisterError}
点击 “注册” 按钮,即表示您同意并愿意遵守用户协议

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 {
    }
}

  1. 删除功能(DeleteFriendServlet.java)
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
	}

}

  1. 修改功能(editfriends.jsp / EditFriendServlet.java)
    editfriends.jsp
<%@ 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);
	}
}

  1. 查询功能(index.jsp / IndexServlet.java)
    index.jsp
<%@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" %>

    
    
通讯录查询
<% @SuppressWarnings("unchecked") ArrayList friendlist = (ArrayList) request.getSession().getAttribute("friendlist"); if(friendlist != null){ for (int i=0; i <% } } %>
朋友ID 朋友姓名 电话号码 qq 电子邮箱 创建时间 操作
<%=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();
		}

	}
}

  1. 管理员功能
    admin_login.jsp
<%@ 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" %>


用户ID 用户姓名 用户密码 电话号码 电子邮箱 操作
${ul.userID} ${ul.userName} ${ul.password} ${ul.phoneID} ${ul.email} 修改 删除
<%@ include file="footer.jsp" %>

你可能感兴趣的:(JavaWeb学习,javaweb)