本文实例为大家分享了java web个人通讯录系统的具体代码,供大家参考,具体内容如下
现在开始上截图:
下面粘贴代码:
首先是目录结构:
add.jspbody{
text-align: center;
font-size:38px;
}
*{
font-family:华文楷体;
font-size:18px;
color:#F0F";
}
h1{
text-align: center;
font-size:38px;
}
添加新的联系人
姓名 | |
电话 | |
地址 | |
邮编 |
body{
text-align: center;
font-size:38px;
}
*{
font-family:华文楷体;
font-size:18px;
color:#F0F";
}
h1{
text-align: center;
font-size:38px;
}
修改联系人信息
姓名 | |
电话 | |
地址 | |
邮编 |
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;
}
欢迎登录到通信录系统
姓名 | 电话 | 地址 | 邮编 | 操作1 | 操作2 |
---|---|---|---|---|---|
${c.name } | ${c.tel } | ${c.address } | ${c.zipcode } | 编辑 |
body{
text-align: center;
font-size:38px;
}
*{
font-family:华文楷体;
font-size:18px;
color:#F0F";
}
h1{
text-align: center;
font-size:38px;
}
我的通信录
用户名:
密 码:
top.jspbody{
text-align: center;
font-size:38px;
}
*{
font-family:华文楷体;
font-size:18px;
color:#F0F";
}
h1{
text-align: center;
font-size:38px;
}
我的通信录
用户名:
密 码:
index.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 = "联系人名不能为空";
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.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 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(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 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包:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。