package com.wgh.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
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 com.wgh.model.LinkmanForm;
import com.wgh.tools.ConnDB;
public class LinkmanServlet extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
if (session.getAttribute("user") == null
|| session.getAttribute("id") == null) {
PrintWriter out = response.getWriter();
out
.println("<script>alert('您的帐号已经过期,请重新登录!');window.location.href='login.jsp'");
} else {
String action = request.getParameter("action"); // 获取action参数的值
if ("query".equals(action)) {
this.query(request, response); // 查询联系人数量
} else if ("queryLinkman".equals(action)) {
this.queryLinkman(request, response); // 查询联系人列表
} else if ("add".equals(action)) {
this.add(request, response); // 保存联系人信息
} else if ("del".equals(action)) {
this.del(request, response);
} else if ("modify".equals(action)) { //查询要修改的联系人信息
this.modify(request, response);
} else if ("saveModify".equals(action)) { //保存修改后的联系人信息
this.saveModify(request, response);
}
}
}
/**
* 统计联系人个数
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void query(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ConnDB conn = new ConnDB(); //创建数据库连接对象
int id = Integer.parseInt(request.getSession().getAttribute("id")
.toString()); // 获取用户ID
String sql = "SELECT COUNT(*) FROM tb_linkman WHERE byUser=" + id + "";
ResultSet rs = conn.executeQuery(sql); // 统计当前用户的联系人个数
int count = 0;
try {
if (rs.next()) {
count = rs.getInt(1); //获取联系人个数
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
conn.close(); //关闭数据库连接
}
request.setAttribute("linkmanCount", count); // 保存联系人个数
request.getRequestDispatcher("main.jsp").forward(request, response); //重定向页面
}
/**
* 查询联系人信息
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void queryLinkman(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
ConnDB conn = new ConnDB(); //创建数据库连接对象
int id = Integer.parseInt(request.getParameter("id")); // 获取通讯组ID
String sql_g = "SELECT * FROM tb_group WHERE id=" + id + "";
ResultSet rs_g = conn.executeQuery(sql_g);
try {
if (rs_g.next()) {
request.setAttribute("groupName", rs_g.getString("name")); // 保存通讯组名称
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
int uid = Integer.parseInt(request.getSession().getAttribute("id")
.toString()); // 获取用户ID
String sql = "SELECT * FROM tb_linkman WHERE byUser=" + uid
+ " AND byGroup=" + id + "";
ResultSet rs = conn.executeQuery(sql); // 查询联系人列表
List list = null;
try {
if (rs.next()) {
list = new ArrayList();
do {
LinkmanForm f = new LinkmanForm();
f.setId(rs.getInt(1));
f.setName(rs.getString(2));
f.setTel(rs.getString(3));
f.setMobileTel(rs.getString(4));
f.setEmail(rs.getString(5));
f.setAddress(rs.getString(6));
f.setPostcode(rs.getString(7));
list.add(f); // 将联系人信息添加到List集合中
} while (rs.next());
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
conn.close();
}
request.setAttribute("linkmanList", list); // 保存联系人列表
request.getRequestDispatcher("linkmanList.jsp").forward(request,
response);
}
/**
* 保存添加的联系人信息
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void add(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name"); // 获取姓名
String tel = request.getParameter("tel"); // 获取电话号码
String mobileTel = request.getParameter("mobileTel"); // 获取手机号码
String email = request.getParameter("email"); // 获取E-mail地址
String address = request.getParameter("address"); // 获取地址
String postcode = request.getParameter("postcode"); // 获取邮政编码
String byUser = request.getSession().getAttribute("id").toString(); // 获取所属用户
String byGroup = request.getParameter("byGroup"); // 获取通讯组
String message = ""; // 保存提示信息的变量
ConnDB conn = new ConnDB(); //创建数据库连接对象
String sql_ins = "INSERT INTO tb_linkman (name,tel,mobileTel,email,address,postcode,byUser,byGroup) VALUES('"
+ name
+ "','"
+ tel
+ "','"
+ mobileTel
+ "','"
+ email
+ "','"
+ address
+ "','"
+ postcode
+ "','"
+ byUser
+ "','"
+ byGroup + "')";
int rtn = conn.executeUpdate(sql_ins); // 将联系人信息保存到数据表中
System.out.println(sql_ins);
if (rtn > 0) {
message = "联系人添加成功!";
} else {
message = "联系人添加失败";
}
conn.close();
request.setAttribute("message", message); // 将提示信息保存到HttpServletRequest中
request.getRequestDispatcher("deal.jsp").forward(request, response);
}
/**
* 删除联系人
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void del(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String linkID = request.getParameter("linkID"); // 获取联系人ID
String message = ""; // 保存提示信息的变量
ConnDB conn = new ConnDB(); //创建数据库连接对象
String sql = "DELETE FROM tb_linkman WHERE id=" + linkID + "";
int rtn = conn.executeUpdate(sql); // 删除指定的联系人信息
if (rtn > 0) {
message = "联系人删除成功!";
} else {
message = "联系人删除失败";
}
conn.close();
request.setAttribute("message", message); // 将提示信息保存到HttpServletRequest中
request.getRequestDispatcher("deal.jsp").forward(request, response);
}
/**
* 查询要修改的联系人信息
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void modify(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ConnDB conn = new ConnDB(); //创建数据库连接对象
String linkID = request.getParameter("linkID"); // 获取联系人ID
String sql = "SELECT * FROM tb_linkman WHERE id=" + linkID + "";
ResultSe
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
addresslist.rar (59个子文件)
addresslist
.project 1KB
.settings
.jsdtscope 406B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.name 6B
.mymetadata 306B
WebRoot
login.jsp 2KB
linkmanAdd.jsp 5KB
WEB-INF
web.xml 2KB
lib
jstl-1.2.jar 405KB
mysql-connector-java-3.0.16-ga-bin.jar 231KB
jsf-api.jar 314KB
jsf-impl.jar 1.15MB
classes
com
wgh
servlet
UserServlet.class 4KB
GroupServlet.class 3KB
LinkmanServlet.class 8KB
model
GroupForm.class 752B
LinkmanForm.class 2KB
UserForm.class 1KB
filter
CharacterEncodingFilter.class 2KB
tools
ConnDB.class 3KB
connDB.properties 125B
index.jsp 489B
META-INF
MANIFEST.MF 39B
CSS
style.css 2KB
linkmanModify.jsp 5KB
register.jsp 4KB
safe.jsp 291B
left.jsp 1KB
main.jsp 2KB
deal.jsp 1KB
images
copyright.jpg 45KB
main_bg.jpg 37KB
新建文件夹
ico.jpg 13KB
left_line.jpg 14KB
复件 login.jpg 154KB
button.jpg 10KB
通迅录登录.jpg 159KB
联系人列表.jpg 248KB
right_bg.jpg 13KB
提示页.jpg 39KB
title.JPG 18KB
button_1.jpg 14KB
login.jpg 154KB
left_bg.jpg 13KB
主界面1.jpg 273KB
message_bg.jpg 42KB
top.jpg 154KB
register_ok.jsp 893B
linkmanList.jsp 4KB
.myeclipse
src
com
wgh
servlet
GroupServlet.java 3KB
LinkmanServlet.java 11KB
UserServlet.java 5KB
model
GroupForm.java 324B
LinkmanForm.java 2KB
UserForm.java 780B
filter
CharacterEncodingFilter.java 1KB
tools
connDB.properties 125B
ConnDB.java 4KB
.classpath 671B
共 59 条
- 1
SDSW1990
- 粉丝: 4
- 资源: 19
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页