java web个人通讯录系统设计

现在开始上截图:

下面粘贴代码:

首先是目录结构:

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

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>add.jsp</title>

<script type="text/javascript" src="<c:url value='/jquery/contactor.js'/>"></script>

<style type="text/css">

body{

text-align: center;

font-size:38px;

}

*{

font-family:华文楷体;

font-size:18px;

color:#F0F";

}

h1{

text-align: center;

font-size:38px;

}

</style>

</head>

<body >

<h1 style="font-size:38px;">添加新的联系人</h1>

<form action="<c:url value='/ContactorServlet?userId=${userId } '/>" method="post" onsubmit="return checkForm()">

<input type="hidden" name="method" value="4">

<table border="1" width="50%" align="center">

<tr>

<td>姓名</td>

<td>

<input type="text" name="name" id="name" onfocus="nameFocus()" onblur="nameBlur()"/>

<div id="nameId"></div>

</td>

</tr>

<tr>

<td>电话</td>

<td>

<input type="text" name="tel" id="tel" onfocus="telFocus()" onblur="telBlur()"/>

<div id="telId"></div>

</td>

</tr>

<tr>

<td>地址</td>

<td>

<input type="text" name="address" id="address" />

</td>

</tr>

<tr>

<td>邮编</td>

<td>

<input type="text" name="zipcode" />

</td>

</tr>

</table>

<input style="margin-right:200px;" type="submit" value="添加"/>

</form>

</body>

</html>

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

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>edit.jsp</title>

<script type="text/javascript" src="<c:url value='/jquery/contactor.js'/>"></script>

<style type="text/css">

body{

text-align: center;

font-size:38px;

}

*{

font-family:华文楷体;

font-size:18px;

color:#F0F";

}

h1{

text-align: center;

font-size:38px;

}

</style>

</head>

<body >

<h1 style="font-size:38px;">修改联系人信息</h1>

<form action="<c:url value='/ContactorServlet'/>" method="post" onsubmit="return checkForm()">

<input type="hidden" name="method" value="6">

<input type="hidden" name="id" value="${contactor.id }">

<table border="1" width="50%" align="center">

<tr>

<td>姓名</td>

<td>

<input type="text" name="name" value="${contactor.name }" id="name" onfocus="nameFocus()" onblur="nameBlur()"/>

<div id="nameId"></div>

</td>

</tr>

<tr>

<td>电话</td>

<td>

<input type="text" name="tel" value="${contactor.tel }" id="tel" onfocus="telFocus()" onblur="telBlur()" />

<div id="telId"></div>

</td>

</tr>

<tr>

<td>地址</td>

<td>

<input type="text" id="address" name="address" value="${contactor.address }" />

</td>

</tr>

<tr>

<td>邮编</td>

<td>

<input type="text" name="zipcode" value="${contactor.zipcode }" />

</td>

</tr>

</table>

<input style="margin-right:200px;" type="submit" value="更改"/>

</form>

</body>

</html>

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

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>home.jsp</title>

</head>

<frameset rows="100%,*">

<frame name="body" src="<c:url value='/jsp/top.jsp'/>" />

</frameset>

</html>

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

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>list.jsp</title>

<style type="text/css">

body{

text-align: center;

font-size:38px;

}

*{

font-family:华文楷体;

font-size:18px;

color:#F0F";

}

h1{

text-align: center;

font-size:38px;

}

a:link{

color: #999;

text-decoration: none;

}

a:VISITED {

text-decoration: none;

color: #c60;

}

a:HOVER {

text-decoration: none;

color: #00F;

}

</style>

</head>

<body >

<h1 style="font-size:38px;">欢迎登录到通信录系统</h1>

<a style="font-size:26px;" href="<c:url value='/ContactorServlet?method=3&userId=${userId }'/>">查看联系人</a>

<a style="font-size:26px;" href="<c:url value='/jsp/add.jsp'/>">添加联系人</a>

<a style="font-size:26px;" href="<c:url value='/jsp/top.jsp'/>">退出系统</a>

<table border="1" align="center" width="60%">

<tr>

<th>姓名</th>

<th>电话</th>

<th>地址</th>

<th>邮编</th>

<th>操作1</th>

<th>操作2</th>

</tr>

<c:forEach items="${contactorList }" var="c">

<tr>

<td>${c.name }</td>

<td>${c.tel }</td>

<td>${c.address }</td>

<td>${c.zipcode }</td>

<td><a href="<c:url value='/ContactorServlet?method=5&id=${c.id }'/>">编辑</a>

</td>

<td>

<a href="<c:url value='/ContactorServlet?method=7&id=${c.id }'/>" >删除</a>

</td>

</tr>

</c:forEach>

</table>

</body>

</html>

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

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>top.jsp</title>

<style type="text/css">

body{

text-align: center;

font-size:38px;

}

*{

font-family:华文楷体;

font-size:18px;

color:#F0F";

}

h1{

text-align: center;

font-size:38px;

}

</style>

</head>

<body >

<h1 style="font-size:38px;">我的通信录</h1>

<form action="<c:url value='/ContactorServlet'/>" method="post">

<input type="hidden" name="method" value="1">

用户名:<input type="text" name="username" /><br/>

密   码:<input type="password" name="password" /><br/>

<input type="submit" value="注册">

</form>

</body>

</html>

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

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>top.jsp</title>

<style type="text/css">

body{

text-align: center;

font-size:38px;

}

*{

font-family:华文楷体;

font-size:18px;

color:#F0F";

}

h1{

text-align: center;

font-size:38px;

}

</style>

</head>

<body >

<h1 style="font-size:38px;">我的通信录</h1>

<form action="<c:url value='/ContactorServlet'/>" method="post">

<input type="hidden" name="method" value="2">

用户名:<input type="text" name="username"/><br/>

密   码:<input type="password" name="password"/><br/>

<input type="submit" value="登录">

<a href="<c:url value='/jsp/register.jsp'/>" target="body" >注册</a>

</form>

</body>

</html>

index.jsp

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

<jsp:forward page="/jsp/home.jsp" />

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 = "<font color='red'>联系人名不能为空</font>";

return false;

}

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

nameId.innerHTML = "<font color='red'>联系人名只能输入中文简体汉字</font>";

return false;

}

nameId.innerHTML = "<font color='green'>联系人名输入正确</font>";

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 = "<font color='red'>手机号码不能为空</font>";

return false;

}

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

telId.innerHTML = "<font color='red'>手机号码输入不正确</font>";

return false;

}

telId.innerHTML = "<font color='green'>手机号码输入正确</font>";

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.rjxy.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.username = 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<Contactor> findAll(int userId) throws SQLException{

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

List<Contactor> list = qr.query(sql, new BeanListHandler<Contactor>(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>(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>(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<Contactor> 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(username,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<Contactor> 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> T toBean(Map data, Class<T> 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();

}

}

<?xml version="1.0" encoding="UTF-8"?>

<c3p0-config>

<!-- 默认配置,当使用ComboPooledDataSource无参构造器时,使用的就是这个配置 -->

<default-config>

<!-- 基本配置 -->

<property name="jdbcUrl">jdbc:mysql://localhost:3306/linkman?characterEncoding=UTF-8</property>

<property name="driverClass">com.mysql.jdbc.Driver</property>

<property name="user">root</property>

<property name="password">123456</property>

<!-- 每次增量,当需要创建Connection对象时,一次创建几个 -->

<property name="acquireIncrement">3</property>

<!-- 当创建池对象后,池中应该有几个Connection对象 -->

<property name="initialPoolSize">10</property>

<!-- 池中最少Connection个数,如果少于这个值,就会创建Connection -->

<property name="minPoolSize">2</property>

<!-- 池中最大连接个数 -->

<property name="maxPoolSize">10</property>

</default-config>

</c3p0-config>

该工程需要的jar包:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

以上是 java web个人通讯录系统设计 的全部内容, 来源链接: utcz.com/p/211021.html

回到顶部