java web个人通讯录系统设计

时间:2022-12-01 22:16:01

现在开始上截图:

java web个人通讯录系统设计java web个人通讯录系统设计java web个人通讯录系统设计java web个人通讯录系统设计java web个人通讯录系统设计java web个人通讯录系统设计

下面粘贴代码:

首先是目录结构:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<%@ 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>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<%@ 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>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%@ 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>

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<%@ 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>

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<%@ 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>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<%@ 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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//当鼠标放在客户名文本框时,给出一个提示文本
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;
  }
  
  
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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;
  }
  
  
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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;
  }
  
  
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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;
  }
  
}

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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());
  }
}

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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);
    }
  }
  
}

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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);
    }
  }
}

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
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 7this.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");
 
 
}
 
}

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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);
    }
  }
  
  
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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();
  
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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个人通讯录系统设计

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