java web个人通讯录系统设计

网友投稿 295 2023-06-17


java web个人通讯录系统设计

现在开始上截图:

下面粘贴代码:

首先是目录结构:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

查看联系人

添加联系人

退出系统

删除

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

用户名:

密   码:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

用户名:

密   码:

注册

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

contactor.js

//当鼠标放在客户名文本框时,给出一个提示文本

function nameFocus() {

var nameId = document.getElementById("nameId");

nameId.innerHTML = "请输入客户真实姓名";

}

//当鼠标离开客户名文本框时,给出一个文本提示

function nameBlur() {

var name = document.getElementById("name");

var nameId = document.getElementById("nameId");

var reg= /^[\u4e00-\u9fa5]+$/;

if(name.value == ""){

nameId.innerHTML = "联系人名不能为空";

return false;

}

if(reg.test(name.value) == false){

nameId.innerHTML = "联系人名只能输入中文简体汉字";

return false;

}

nameId.innerHTML = "联系人名输入正确";

return true;

}

//当鼠标放在客户名文本框时,给出一个提示文本

function telFocus() {

var telId = document.getElementById("telId");

telId.innerHTML = "请输入手机号码";

}

//当鼠标离开客户名文本框时,给出一个文本提示

function telBlur() {

var tel = document.getElementById("tel");

var telId = document.getElementById("telId");

var reg= /^(13|15|17|18)\d{9}$/;

if(tel.value == ""){

telId.innerHTML = "手机号码不能为空";

return false;

}

if(reg.test(tel.value) == false){

telId.innerHTML = "手机号码输入不正确";

return false;

}

telId.innerHTML = "手机号码输入正确";

return true;

}

//表单提交时验证输入内容的有效性

function checkForm() {

var flagname = nameBlur();

var flagtel = telBlur();

if(flagname == true && flagtel == true ){

return true;

}else{

alert("失败,请按提示输入信息 !")

return false;

}

}

package cn.edu.aynu.rjxhttp://y.bean;

/**

* 联系人表属性

* @author Administrator

*

*/

public class Contactor {

private int id;

private int userId;

private String name;

private String tel;

private String address;

private String zipcode;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public int getUserId() {

return userId;

}

public void setUserId(int userId) {

this.userId = userId;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getTel() {

return tel;

}

public void setTel(String tel) {

this.tel = tel;

}

public String getAddress() {

return address;

}

public void setAddress(String address) {

this.address = address;

}

public String getZipcode() {

return zipcode;

}

public void setZipcode(String zipcode) {

this.zipcode = zipcode;

}

}

package cn.edu.aynu.rjxy.bean;

/**

* 用户表属性

* @author Administrator

*

*/

public class User {

private int id;

private String username;

private String password;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.usernhttp://ame = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

}

package cn.edu.aynu.rjxy.dao;

import java.sql.SQLException;

import java.util.List;

import org.apache.commons.dbutils.QueryRunner;

import org.apache.commons.dbutils.handlers.BeanHandler;

import org.apache.commons.dbutils.handlers.BeanListHandler;

import cn.edu.aynu.rjxy.bean.Contactor;

import cn.edu.aynu.rjxy.utils.JDBCUtils;

/**

* 对linkman表的增删改查

* @author Administrator

*

*/

public class ContactorDao {

private QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());

/**

* 添加联系人

* @param c

* @throws SQLException

*/

public void insert(Contactor c,int userId) throws SQLException{

String sql = "insert into linkman(name,tel,address,zipcode,userId) values(?,?,?,?,?)";

qr.update(sql, c.getName(),c.getTel(),c.getAddress(),c.getZipcode(),userId);

}

/**

* 删除联系人

* @param cid

* @throws SQLException

*/

public void delete(int id) throws SQLException{

String sql = "delete from linkman where id=?";

qr.update(sql, id);

}

/**

* 修改联系人

* @param c

* @throws SQLException

*/

public void update(Contactor c,int userId) throws SQLException{

String sql = "update linkman set name=?,tel=?,address=?,zipcode=?,userId=? where id=?";

qr.update(sql, c.getName(),c.getTel(),c.getAddress(),c.getZipcode(),userId,c.getId());

}

/**

* 根据userId查询联系人

* @throws SQLException

*

*/

public List findAll(int userId) throws SQLException{

String sql = "select * from linkman where userId = ?";

List list = qr.query(sql, new BeanListHandler(Contactor.class),userId);

return list;

}

/**

* 根据id查询某个联系人

* @throws SQLException

*

*/

public Contactor findByCid(int id) throws SQLException{

String sql = "select * from linkman where id=?";

Contactor c = qr.query(sql, new BeanHandler(Contactor.class), id);

return c;

}

}

package cn.edu.aynu.rjxy.dao;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;

import org.apache.commons.dbutils.handlers.BeanHandler;

import cn.edu.aynu.rjxy.bean.User;

import cn.edu.aynu.rjxy.utils.JDBCUtils;

/**

* 对user表的查询

* @author Administrator

*

*/

public class UserDao {

private QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());

/**

* 根据用户名和用户密码查询

* @param cid

* @return

* @throws SQLException

*/

public User findByCid(String username,String password) throws SQLException{

String sql = "select * from user where username=?&&password=?";

User u = qr.query(sql, new BeanHandler(User.class), username,password);

return u;

}

/**

* 注册

* @param c

* @throws SQLException

*/

public void register(User u) throws SQLException{

String sql = "insert into user(username,password) values(?,?)";

qr.update(sql, u.getUsername(),u.getPassword());

}

}

package cn.edu.aynu.rjxy.service;

import java.sql.SQLException;

import java.util.List;

import cn.edu.aynu.rjxy.bean.Contactor;

import cn.edu.aynu.rjxy.dao.ContactorDao;

/**

* 联系人功能处理

* @author Administrator

*

*/

public class ContactorService {

//创建ContactorDao对象

ContactorDao dao = new ContactorDao();

/**

* 添加联系人

* @param c

*/

public void add(Contactor c,int userId){

try {

dao.insert(c,userId);

} catch (SQLException e) {

throw new RuntimeException(e);

}

}

/**

* 删除联系人

* @param cid

*/

public void delete(int id){

try {

dao.delete(id);

} catch (SQLException e) {

throw new RuntimeException(e);

}

}

/**

* 加载(查询)联系人

* @param cid

*/

public List load(int userId){

try {

return dao.findAll(userId);

} catch (SQLException e) {

throw new RuntimeException(e);

}

}

/**

* 加载(查询)联系人(为修改做准备)

* @param cid

*/

public Contactor loadById(int id){

try {

return dao.findByCid(id);

} catch (SQLException e) {

throw new RuntimeException(e);

}

}

/**

* 编辑(修改)联系人信息

* @param c

*/

public void edit(Contactor c,int userId){

try {

dao.update(c,userId);

} catch (SQLException e) {

throw new RuntimeException(e);

}

}

}

package cn.edu.aynu.rjxy.service;

import java.sql.SQLException;

import cn.edu.aynu.rjxy.bean.User;

import cn.edu.aynu.rjxy.dao.UserDao;

/**

*

* @author Administrator

*

*/

public class UserService {

//创建ContactorDao对象

UserDao dao = new UserDao();

/**

* 根据用户名和密码查询用户

* @param cid

*/

public User login(String username,String password){

try {

return dao.findByCid(usernahttp://me,password);

} catch (SQLException e) {

throw new RuntimeException(e);

}

}

/**

* 注册

* @param c

*/

public void register(User u){

try {

dao.register(u);

} catch (SQLException e) {

throw new RuntimeException(e);

}

}

}

package cn.edu.aynu.rjxy.servlet;

import java.io.IOException;

import java.util.List;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import cn.edu.aynu.rjxy.bean.Contactor;

import cn.edu.aynu.rjxy.bean.User;

import cn.edu.aynu.rjxy.service.ContactorService;

import cn.edu.aynu.rjxy.service.UserService;

import cn.edu.aynu.rjxy.utils.CommonsUtils;

/**

* 处理联系人请求

* @author Administrator

*

*/

public class ContactorServlet extends HttpServlet {

private ContactorService cs = new ContactorService();

private UserService us = new UserService();

private int userId;

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

this.doPost(request, response);

}

/**

* 通过获取表单中的隐藏字段method或者超链接中的method参数的值来区分调用执行那个方法来处理这个请求

*/

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

//处理中文乱码

request.setCharacterEncoding("UTF-8");

response.setContentType("text/html;charset=UTF-8");

//获取隐藏字段method的值,并把它转换为int型

int method = Integer.parseInt(request.getParameter("method"));

switch(method){

case 1: this.register(request, response);

break;

case 2: this.login(request, response);

break;

case 3: this.query(request, response);

break;

case 4: this.add(request, response);

break;

case 5: this.loadForUpdate(request, response);

break;

case 6: this.edit(request, response);

break;

case 7: this.delete(request, response);

break;

}

}

/**

* 处理注册的请求

* @param request

* @param response

* @throws ServletException

* @throws IOException

*/

public void register(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

//使用CommonUtils工具类将请求数据封装到bean中

User u = CommonsUtils.toBean(request.getParameterMap(), User.class);

//执行添加客户业务

us.register(u);

//输出添加成功提示

response.getWriter().print("注册成功,三秒钟自动跳转到登录界面");

response.setHeader("Refresh","3;url=http://127.0.0.1:8099/addresslist/jsp/top.jsp");

}

/**

* 处理登录的请求 method=2

* @param request

* @param response

* @throws ServletException

* @throws IOException

*/

public void login(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

//从请求中解析用户名和密码

String username = request.getParameter("username");

String password = request.getParameter("password");

//执行加载业务

User u = us.login(username, password);

if (u != null) {

HttpSession session = request.getSession();

session.setAttribute("userId", u.getId());

response.getWriter().print("登录成功"+u.getId());

//通过请求转发将pb带到list.jsp上去

request.getRequestDispatcher("/jsp/list.jsp").forward(request, response);

}else{

response.getWriter().print("登录失败,即将跳转到登录页面,请重新登录......");

response.setHeader("Refresh","3;url=http://127.0.0.1:8099/addresslist/jsp/top.jsp");

}

}

/**

* 处理查看联系人的请求 method=3

* @param request

* @param response

* @throws ServletException

* @throws IOException

*/

public void query(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

HttpSession session = request.getSession();

userId = (Integer) session.getAttribute("userId");

//执行查询业务

List list = cs.load(userId);

//将list存放到request域中

request.setAttribute("contactorList", list);

System.out.println(list.size());

//通过请求转发将查询结果带到list.jsp页面上显示

request.getRequestDispatcher("/jsp/list.jsp").forward(request, response);

}

/**

* 处理添加客户的请求

* @param request

* @param response

* @throws ServletException

* @throws IOException

*/

public void add(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

HttpSession session = request.getSession();

userId = (Integer) session.getAttribute("userId");

//使用CommonUtils工具类将请求数据封装到bean中

Contactor c = CommonsUtils.toBean(request.getParameterMap(), Contactor.class);

//执行添加客户业务

cs.add(c,userId);

//输出添加成功提示

response.getWriter().print("添加成功,三秒后跳转到功能界面......");

response.setHeader("Refresh","3;url=http://127.0.0.1:8099/addresslist/jsp/list.jsp");

}

/**

* 处理加载某一个客户的请求

* @param request

* @param response

* @throws ServletException

* @throws IOException

*/

public void loadForUpdate(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

//从请求中获取客户的cid

String ids = request.getParameter("id");

int id = Integer.valueOf(ids);

//执行加载业务

Contactor c = cs.loadById(id);

//将Customer对象c保存在request域中

request.setAttribute("contactor", c);

//通过请求转发将customer对象显示在list.jsp页面上

request.getRequestDispatcher("/jsp/edit.jsp").forward(request, response);

}

/**

* 处理更改某一联系人的请求

* @param request

* @param response

* @throws ServletException

* @throws IOException

*/

public void edit(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

HttpSession session = request.getSession();

userId = (Integer) session.getAttribute("userId");

//使用CommonUtils工具类将请求数据封装到bean中

Contactor c = CommonsUtils.toBean(request.getParameterMap(), Contactor.class);

//执行更改业务

cs.edit(c,userId);

//给客户端发送更改成功提示

response.getWriter().print("更改成功,三秒后跳转到功能界面......");

response.setHeader("Refresh","3;url=http://127.0.0.1:8099/addresslist/jsp/list.jsp");

}

/**

* 处理删除某一联系人的请求

* @param request

* @param response

* @throws ServletException

* @throws IOException

*/

public void delete(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

//从请求中获取cid

String cid = request.getParameter("id");

int id = Integer.valueOf(cid);

//执行删除客户业务

cs.delete(id);

//给客户端发送删除成功提示

response.getWriter().print("删除成功,三秒后跳转到功能界面......");

response.setHeader("Refresh","3;url=http://127.0.0.1:8099/addresslist/jsp/list.jsp");

}

}

package cn.edu.aynu.rjxy.utils;

import java.util.Map;

import java.util.UUID;

import org.apache.commons.beanutils.BeanUtils;

import org.apache.commons.beanutils.ConvertUtils;

/**

* 提供UUID,实现表单数据封装到bean中

* @author Administrator

*

*/

public class CommonsUtils {

/**

* 返回一个UUID

* @return

*/

public static String uuid(){

return UUID.randomUUID().toString().replace("-", "").toUpperCase();

}

/**

* 把表单数据封装到bean中

*/

public static T toBean(Map data, Class clazz){

try{

T bean = clazz.newInstance();

BeanUtils.populate(bean, data);

return bean;

}catch (Exception e) {

throw new RuntimeException(e);

}

}

}

package cn.edu.aynu.rjxy.utils;

import java.sql.Connection;

import java.sql.SQLException;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**

* 创建数据库连接池

* @author Administrator

*

*/

public class JDBCUtils {

//读取的是C3P0-config默认配置创建数据库连接池对象

private static DataSource ds = new ComboPooledDataSource();

//获取数据库连接池对象

public static DataSource getDataSource(){

return ds;

}

//从池中获取连接

public static Connection getConnection() throws SQLException{

return ds.getConnection();

}

}

jdbc:mysql://localhost:3306/linkman?characterEncoding=UTF-8

com.mysql.jdbc.Driver

root

123456

3

10

2

10

该工程需要的jar包:


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Vue实例简单方法介绍
下一篇:利用transition实现文字上下抖动的效果
相关文章

 发表评论

暂时没有评论,来抢沙发吧~