课设为了好过,选了个简单好做的项目,十天课设,水了四天(还写了两天需求文档emm)。虚虚浮浮哈哈哈哈哈哈。
把项目界面放这儿,资源上传了还在审核,等通过了就把链接放过来。
实现的功能不多,界面也怪简洁的。尴了个尬,答辩还被老师为难。导致现在想起老师的脸就心有余悸哈哈哈哈哈哈。
有啥问题可以问我啊如果我会的话噗(大概率是不会的)
很久没登过csdn了,很多私信来不及一一回复,把下载链接放在这儿大家自行下载吧,我改成免费了,毕竟垃圾项目不好意思收费哈哈哈哈,大家随意吧
https://download.csdn.net/download/cccc___/12046762
这是项目目录
界面放上来勉强看一看吧,仙道彰未免太帅了8也
注册可实现邮箱激活验证码注册
登录由生成的随机验证码进行验证
首页显示联系人列表,可实现基础联系人增删查改功能,查询使用模糊查询
基础添加联系人界面
根据用户联系人列表 下的地址生成地域分布图
个人中心展示用户信息,可修改信息
User.java(用户类)
package mains;
public class User {
private int id;
private String name;
private String password;
private String email;
private String phone;
public int getId(){return id;}
public void setId(int id){this.id = id;}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public User(int id, String name, String password, String email, String phone) {
this.id = id;
this.name = name;
this.password = password;
this.email = email;
this.phone = phone;
}
public User() {
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", password=" + password + ", email=" + email
+ ", phone=" + phone + "]";
}
}
contacts.java(联系人类)
package mains;
public class contacts {
private int id;
private String name;
private String mobilephone;
private String telephone;
private String birthday;
private String sex;
private String address;
private String relationship;
private int userId;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMobilephone() {
return mobilephone;
}
public void setMobilephone(String mobilephone) {
this.mobilephone = mobilephone;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getRelationship() {
return relationship;
}
public void setRelationship(String relationship) {
this.relationship = relationship;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
}
regist.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%= basePath%>">
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="regist.css"/>
<title>注册</title>
<script type="text/javascript">
$(document).ready(function () {
$("[value=GETsubmit]").click(function () {
$.ajax({
type: "POST",
url: "SendMailServlet",
dataType: "text",
data: {eamil: $("#inputEmail5").val()},
success: function (data) {
},
error: function () {
}
})
});
$("[value=getsubmit]").click(function () {
$.ajax({
type: "POST",
url: "RegistServlet",
dataType: "text",
data: {
name: $("#name").val(),
password: $("#password").val(),
verifyc: $("#verifyc").val(),
eamil: $("#inputEmail5").val(),
rpsw: $("#rpsw").val()
},
success: function (data) {
},
error: function () {
}
})
});
});
</script>
<link href="./images/1.jpg" rel="icon" type="image/x-ico">
<style type="text/css">
body {
background-image: url('./images/bklogin.jpg');
background-position: center;
background-repeat: no-repeat;
}
a:hover {
color: #f34e4e;
}
.col-sm-offset-2 col-sm-10 input-login a {
color: #999;
float: right;
}
</style>
</head>
<body>
<div class="row clearfix rowall">
<h2 style="margin-right:150px;margin-left:450px;">注册</h2>
<div class="col-md-6 column row-div-all" style="width: 400px;left:250px">
<form class="form-horizontal row-form1" role="form" action="<%= basePath %>/RegistServlet" method="post"
id="Form" style="margin-top:80px">
<div class="form-group row1-div-left" style="margin-left: 0px;margin-right: 0px; margin-bottom: 20px;">
<label for="name" class="col-sm-2 control-label row1-div-left-label">用户名</label>
<div class="col-sm-10 row1-div-left-name">
<input type="text" class="form-control" id="name" name="name"/>
</div>
</div>
<div class="form-group row1-div-left" style="margin-left: 0px;margin-right: 0px; margin-bottom: 20px;">
<label for="inputEmail5" class="col-sm-2 control-label row1-div-left-label">邮箱</label>
<div class="col-sm-10 row1-div-left-name">
<input type="text" class="form-control" id="inputEmail5" name="email"/>
</div>
</div>
<div class="form-group row1-div" style="margin-left: 0px;margin-right: 0px; margin-bottom: 20px;">
<label for="verifyc" class="col-sm-2 control-label row1-div-left-label">验证码</label>
<div class="col-sm-10 row1-div-left-name input-group" style="width: 260px;">
<input type="text" class="form-control " id="verifyc" name="verifyc" style="width: 163px;">
<span class="input-group-btn row-span">
<button class="btn btn-default" type="button" style=" border-radius: 10px; left:20px;outline: none"
value="GETsubmit">获取验证码</button>
</span>
</div>
</div>
<div class="form-group row1-div-left" style="margin-left: 0px;margin-right: 0px; margin-bottom: 20px;">
<label for="password" class="col-sm-2 control-label row1-div-left-label">密码</label>
<div class="col-sm-10 row1-div-left-name">
<input type="password" class="form-control" id="password" name="password"/>
</div>
</div>
<div class="form-group row1-div-left" style="margin-left: 0px;margin-right: 0px; margin-bottom: 20px;">
<label for="rpsw" class="col-sm-2 control-label row1-div-left-label">重输密码</label>
<div class="col-sm-10 row1-div-left-name">
<input type="password" class="form-control" id="rpsw" name="rpsw"/>
</div>
</div>
<div class="form-group row-form">
<div class="col-sm-offset-2 col-sm-10 input-login">
<a href="Login.jsp" style="outline: none;background-color: transparent">账号登录</a>
<button class="btn btn-primary btn-block " type="submit" value="getsubmit"> 立即注册</button>
</div>
</div>
</form>
</div>
</div>
</div>
<font color="red" size="2"> ${msg }</font>
</body>
</html>
RegistServlet.java
package servlets;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import controller.SendMail;
import controller.UserController;
/**
* Servlet implementation class RegistServlet
*/
@WebServlet(urlPatterns = "/RegistServlet")
public class RegistServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public RegistServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("11111");
response.setContentType("text/html;charset=UTF-8");
String name = request.getParameter("name");
String password = request.getParameter("password");
String eamil = request.getParameter("eamil");
System.out.println("name = "+name);
System.out.println("pasword = "+password);
System.out.println("email = "+eamil);
String verifyc = request.getParameter("verifyc");
String rpsw = request.getParameter("rpsw"); // 获取表单全部信息
if(!verifyc.equals(SendMail.emailCode)) {
System.out.println("2222");
request.setAttribute("msg", "验证码错误");
request.getRequestDispatcher("/regist.jsp").forward(request, response);
return ;
}
if(name==null || name.trim().isEmpty()) {
System.out.println("3333");
request.setAttribute("msg", "账号不能为空");
request.getRequestDispatcher("/regist.jsp").forward(request, response);
return ;
}
else if(password == null || password.trim().isEmpty()) {
System.out.println("4444");
request.setAttribute("msg", "密码不为空");
request.getRequestDispatcher("/regist.jsp").forward(request, response);
return ;
}
else if(!password.equals(rpsw)) {
System.out.println("5555");
request.setAttribute("msg", "两次输入密码不同");
request.getRequestDispatcher("/regist.jsp").forward(request, response);
return ;
}else {
System.out.println("6666");
UserController userController = new UserController();
userController.addUser(name,password,eamil);
request.setAttribute("msg", "恭喜," + name + ",注册成功");
System.out.println(rpsw);
System.out.println("在等待跳转辣");
// response.sendRedirect("/Login.jsp");
request.getRequestDispatcher("/Login.jsp").forward(request, response);
System.out.println("跳转成功辣");
}
}
}
Login.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%= basePath%>">
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="Login.css"/>
<title>登录</title>
<link href="./images/1.jpg" rel="icon" type="image/x-ico">
<script type="text/javascript">
function change() {
var img = document.getElementById("verifyc");
img.src = "VerifycCodeServlet?t=" + new Date().getMilliseconds();
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>登录界面</title>
<style type="text/css">
body{
background-image:url('./images/bklogin.jpg');
background-position:center;
background-repeat:no-repeat;
}
</style>
</head>
<body>
<div style="text-align:center;margin-top:120px">
<h2 style="margin-right:150px;">登录</h2>
<div class="row clearfix rowall">
<div class="col-md-6 column row-div-all" style="width: 400px;left:70px;">
<form class="form-horizontal row-form1" role="form" action="<%= request.getContextPath() %>/TestConnect"
method="post">
<div class="form-group row1-div-left" style="margin-left: 0px;margin-right: 0px; margin-bottom: 20px;">
<label for="inputEmail3" class="col-sm-2 control-label row1-div-left-label">用户名</label>
<div class="col-sm-10 row1-div-left-name">
<input type="text" class="form-control" id="inputEmail3" name="name" value="${name}"/>
</div>
</div>
<div class="form-group row1-div-left" style="margin-left: 0px;margin-right: 0px; margin-bottom: 20px;">
<label for="inputPassword3" class="col-sm-2 control-label row1-div-left-label">密码</label>
<div class="col-sm-10 row1-div-left-name">
<input type="password" class="form-control" id="inputPassword3" name="password"/>
</div>
</div>
<div class="form-group row1-div" style="margin-left: 0px;margin-right: 0px; margin-bottom: 20px;">
<label for="inputEmail4" class="col-sm-2 control-label row1-div-left-label">验证码</label>
<div class="col-sm-10 row1-div-left-name" style="width: 130px;">
<input type="text" class="form-control " id="inputEmail4" name="verifyc" style="width: 120px;">
</div>
<span class="input-group-btn"><img src="VerifycCodeServlet" id="verifyc" border="0"
style="width: 90px;" onclick="javaScript:change();"></span>
</div>
<div class="form-group adout-pwd " style=" margin-left: 0px;
margin-right: 0px;">
<div class="col-sm-offset-2 col-sm-10 keep-pwd">
<div class="checkbox" style="width: 300px;">
<label><input type="checkbox"/>记住密码<a class="forget-pwd" href="#">忘记密码?</a></label>
</div>
</div>
</div>
<div class="form-group row-form">
<div class="col-sm-offset-2 col-sm-10 input-login">
<button type="submit" class="btn btn-primary btn-block ">登录</button>
</div>
</div>
</form>
</div>
<div class="col-md-6 column other-login-way">
<span class="seprate-line" style="left:70px;">或</span>
<a href="regist.jsp"> <button type="button" class="btn btn-default btn-block"style="margin-left:70px">新用户注册</button></a>
</div>
</div>
</div>
<font color="red" size="2"> ${msg }</font>
</body>
</html>
LoginServlet.java
package servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import controller.UserController;
import mains.User;
/**
* Servlet implementation class LoginServlet
*/
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("do post in");
// TODO Auto-generated method stub
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String name = request.getParameter("name");
String password = request.getParameter("password");
String verifyc = request.getParameter("verifyc");
String svc = (String) request.getSession().getAttribute("sessionverify");
System.out.println("go select");
User user = new UserController().findUserName(name);
System.out.println("out select");
System.out.println("ver = "+verifyc);
System.out.println("svc = "+svc);
System.out.println("name = "+name);
System.out.println("pws = "+user.getPassword());
HttpSession session = request.getSession(true);
if(!svc.equalsIgnoreCase(verifyc)) {
request.setAttribute("msg", "验证码错误");
System.out.println("验证码错误");
request.getRequestDispatcher("Login.jsp").forward(request, response);
return ;
}
if(user == null) {
request.setAttribute("msg" , "没有这个用户");
out.println("没有这个用户");
request.getRequestDispatcher("Login.jsp").forward(request, response);
return ;
}
if(!user.getPassword().equals(password)&&user.getPassword() != null) {
request.setAttribute("msg", "密码错误请重新输入");
out.println("密码错误请重新输入");
request.getRequestDispatcher("Login.jsp").forward(request, response);
return ;
}else {
session.setAttribute("user",user);
out.println("欢迎访问");
request.getRequestDispatcher("/ContactsServlet?id=" + user.getId()).forward(request, response);
}
}
}
index.jsp(主页界面代码)
<%@ page import="mains.contacts" %>
<%@ page import="java.util.List" %>
<%@ page import="mains.User" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<%
User user = (User) session.getAttribute("user");
System.out.print(user);
%>
<head>
<meta charset="UTF-8">
<title>通讯录主页</title>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="./images/ti.jpg" rel="icon" type="image/x-ico">
<style type="text/css">
body {
}
</style>
<script type="text/javascript">
$(document).ready(function () {
$("[value=search]").click(function () {
var msg = $("#msg").val();
console.log(msg)
$.ajax({
type: "POST",
url: "SearchServlet",
dataType: "text",
data: {
'msg': $("#msg").val(),
'userId': <%=user.getId()%>
},
success: function (data) {
window.location.href = 'searchContact.jsp';
},
error: function () {
}
})
});
});
</script>
</head>
<body>
<div class="container-fluid">
<div class="row-fluid">
<div class="span12">
<nav class="navbar navbar-default" role="navigation" style="background-color:#000000;">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">通讯录</a>
</div>
<div>
<ul class="nav navbar-nav">
<li class="active"><a href="#">联系人列表</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
统计表
<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li class="disabled"><a href="#">姓氏分布图(暂未开启)</a></li>
<li class="divider"></li>
<li><a href="/RegionListServlet?id=<%=user.getId()%>">地域分布图</a></li>
<li class="divider"></li>
<li><a href="chartforsex.jsp">性别分布图</a></li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
用户: <%=user.getName()%> ,欢迎访问
<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><a href="PersonCenter.jsp">个人中心</a></li>
<li class="divider"></li>
<li><a href="/LoginOutServlet">退出登录</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
<div class="col-md-6 column other-login-way" style="display:inline-block;">
<button type="button" class="btn btn-default btn-block"
style="margin-left:70px;width: 96px;outline: none;display:inline-block;">
<td class="td-manage" style="display:inline-block;">
<a href="addcontact.jsp" style="text-decoration: none;display:inline-block;">
添加联系人
</a>
</td>
</button>
<button type="button" class="btn btn-default btn-block"
style="margin-left: 0px;width: 106px;outline: none;display:inline-block;margin-top: 0px;">
<td class="td-manage" style="display:inline-block;">
<a href="excelOut.jsp" style="text-decoration: none;display:inline-block;">
导出为Excel
</a>
</td>
</button>
</div>
<div class="searchTempDiv">
<input type="text" placeholder="输入待查找信息" style="margin-left: 250px;" id="msg">
<input type="submit" value="search">
</div>
<div class="row-fluid">
<div class="span12">
<table class="table">
<thead>
<tr>
<th>
编号
</th>
<th>
姓名
</th>
<th>
手机号码
</th>
<th>
电话号码
</th>
<th>
生日
</th>
<th>
性别
</th>
<th>
地址
</th>
<th>
关系
</th>
<th>
操作
</th>
</tr>
</thead>
<tbody>
<%
//循环显示数据
List<contacts> list = (List) session.getAttribute("list"); // 取request里面的对象队列
int j = 1;
for (int i = 0; i < list.size(); i++) {
pageContext.setAttribute("slist", list.get(i));
//保存到页面pageContext里面方便下面进行EL表达式调用
%>
<tr>
<td>
<%=i + 1%>
</td>
<td>
${slist.name}
</td>
<td>
${slist.mobilephone}
</td>
<td>
${slist.telephone}
</td>
<td>
${slist.birthday}
</td>
<td>
${slist.sex}
</td>
<td>
${slist.address}
</td>
<td>
${slist.relationship}
</td>
<td>
<a href="/editcontact.jsp?id=${slist.id}">
<button ><img src="images/edit.jpg" style="width:17px;height:17px" title="编辑">
</button>
</a>
<a href="/DelServlet?id=${slist.id}&userid=<%=user.getId()%>">
<button ><img src="images/del.jpg" style="width:17px;height:17px" title="删除"
onclick="contain()"></button>
</a>
</td>
</tr>
<%
}
%>
</tbody>
</table>
<ul class="pager">
<li><a href="#" style="color: #1F1F1F">«</a></li>
<li><a href="#" style="color: #1F1F1F">1</a></li>
<li><a href="#" style="color: #1F1F1F">2</a></li>
<li><a href="#" style="color: #1F1F1F">3</a></li>
<li><a href="#" style="color: #1F1F1F">4</a></li>
<li><a href="#" style="color: #1F1F1F">5</a></li>
<li><a href="#" style="color: #1F1F1F">»</a></li>
</ul>
<form action="ImportExcelServlet?id=<%=user.getId()%>" method="post" enctype="multipart/form-data">
<input type="file" name="importExcel" id="importExcel" style="display:inline-block;">
<input type="submit" value="导入" id="submit" style="display:inline-block;" onclick="success()">
</form>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript" src="lib/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="lib/layer/2.4/layer.js"></script>
<script type="text/javascript" src="static/h-ui/js/H-ui.js"></script>
<script type="text/javascript" src="static/h-ui.admin/js/H-ui.admin.page.js"></script>
<!--/_footer /作为公共模版分离出去-->
<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="lib/My97DatePicker/4.8/WdatePicker.js"></script>
<script type="text/javascript" src="lib/datatables/1.10.0/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="lib/laypage/1.2/laypage.js"></script>
<script type="application/javascript">
function contain() {
alert("删除成功")
}
function success() {
alert("导入成功")
}
</script>
</body>
</html>
ContactsServlet.java(首页展示联系人列表)
package servlets;
import controller.UserController;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
@WebServlet("/ContactsServlet")
public class ContactsServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//数据库操作
UserController uc = new UserController();
//编码
response.setContentType("text/html;charset=UTF-8");
// PrintWriter out = response.getWriter();
//得到jsp传来的用户id
int id = Integer.parseInt(request.getParameter("id"));
System.out.println("id" + id);
//根据得到的id得到对应id的联系人
List list=uc.showContacts(id);
HttpSession session = request.getSession(true);
//将数据库得到的联系人列表放入session
session.setAttribute("list",list);
//跳转页面
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
}
AddContactServlet.java(添加联系人)
package servlets;
import controller.UserController;
import mains.User;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/AddContactServlet")
public class AddContactServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("AddContactServlet's servlet");
response.setContentType("text/html;charset=UTF-8");
// PrintWriter out = response.getWriter();
//得到jsp传来的联系人姓名
String name = request.getParameter("name");
//得到jsp传来的联系人电话
String mobilephone = request.getParameter("mobilephone");
//得到jsp传来的联系人电话
String telephone = request.getParameter("telephone");
//得到jsp传来的联系人生日
String birthday = request.getParameter("birthday");
//得到jsp传来的联系人性别
String sex = request.getParameter("sex");
//得到jsp传来的联系人地址
String address = request.getParameter("address");
//得到jsp传来的联系人关系
String relationship = request.getParameter("relationship");
//得到jsp传来的用户ID
int userId = Integer.parseInt(request.getParameter("userId"));
System.out.println("!!!name" + name + "mobilephone" + mobilephone + "telephone" + telephone + "birthday" + birthday + "sex" + sex + "address" + address + "relationship" + relationship + "userid" + userId);
//数据库操作
UserController userController = new UserController();
//将联系人信息添加进数据库
userController.addContacts(name, mobilephone, telephone, birthday, sex, address, relationship, userId);
System.out.println("添加成功");
// response.getWriter().write("成功");
//页面跳转
request.getRequestDispatcher("/index.jsp").forward(request, response);
System.out.println("success");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
DelServlet.java(删除联系人)
package servlets;
import controller.UserController;
import mains.contacts;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
@WebServlet("/DelServlet")
public class DelServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println(" DelServlett 's servlet");
//设置编码
response.setContentType("text/html;charset=UTF-8");
// PrintWriter out = response.getWriter();
//得到jsp传来的联系人id
int id = Integer.parseInt(request.getParameter("id"));
//得到jsp传来的用户id
int userid = Integer.parseInt(request.getParameter("userid"));
UserController userController = new UserController();
//数据库删除对应联系人
userController.delContacts(id);
List list=userController.showContacts(userid);
HttpSession session = request.getSession(true);
//将session中存储的list修改
session.setAttribute("list",list);
//跳转页面
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
EditContactServlet.java(修改联系人)
package servlets;
import controller.UserController;
import mains.contacts;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
@WebServlet("/EditContactServlet")
public class EditContactServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("EditContactServlet's servlet");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
//得到jsp传来的联系人id
int id = Integer.parseInt(request.getParameter("id"));
UserController userController = new UserController();
contacts c;
c = userController.findContact(id);
String name = request.getParameter("name");
String mobilephone = request.getParameter("mobilephone");
String telephone = request.getParameter("telephone");
String birthday = request.getParameter("birthday");
String sex = request.getParameter("sex");
String address = request.getParameter("address");
String relationship = request.getParameter("relationship");
int userId = Integer.parseInt(request.getParameter("userId"));
userController.editContact(id,name, mobilephone, telephone, birthday, sex, address, relationship, userId);
System.out.println("修改成功");
UserController uc = new UserController();
List list=uc.showContacts(id);
HttpSession session = request.getSession(true);
session.setAttribute("list",list);
response.getWriter().write("成功");
System.out.println("success");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
UserController.java(执行数据库语句,没封装了,主要还是懒)
package controller;
import mains.User;
import mains.contacts;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class UserController {
/**
* 登录
*
* @param name
* @return
*/
public User findUserName(String name) {
System.out.println("ok start select");
String psw = null;
Integer id;
Connection conn = null;
PreparedStatement stmt = null;
ResultSet st = null;
User user1 = new User();
try {
String dirver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/javaweb";
String user = "root";
String password = "123456";
Class.forName(dirver);
conn = DriverManager.getConnection(url, user, password);
String sql = "select * from user where name = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, name);
st = stmt.executeQuery();
System.out.println(sql);
System.out.println("stmt : " + stmt);
System.out.println("st : " + st);
System.out.println("one----");
if (st == null) {
return null;
}
System.out.println("two----");
if (st.next()) {
user1.setPassword(st.getString("password"));
user1.setId(st.getInt("id"));
user1.setName(st.getString("name"));
user1.setEmail(st.getString("email"));
user1.setPhone(st.getString("phone"));
System.out.println("three----");
} else {
psw = null;
System.out.println("forth----");
}
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception se) {
se.printStackTrace();
} finally {
try {
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e1) {
}
}
return user1;
}
public void addUser(String name, String psw, String email) {
System.out.println("11111name" + name + "psw" + psw + "email" + email);
Connection conn = null;
PreparedStatement stmt = null;
try {
String dirver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/javaweb";
String user = "root";
String password = "123456";
Class.forName(dirver);
conn = DriverManager.getConnection(url, user, password);
String sql = "insert into user values(?,?,?,?,?)";
stmt = conn.prepareStatement(sql);
stmt.setString(1, null);
stmt.setString(2, name);
stmt.setString(3, psw);
stmt.setString(4, email);
stmt.setString(5, null);
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e2) {
}
}
}
public List<contacts> showContacts(int userid) {
List<contacts> conlist = new ArrayList<contacts>();
System.out.println("showContacts");
Connection conn = null;
PreparedStatement stmt = null;
ResultSet st = null;
try {
String dirver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/javaweb";
String user = "root";
String password = "123456";
Class.forName(dirver);
conn = DriverManager.getConnection(url, user, password);
String sql = "select * from contacts where userId = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, userid);
st = stmt.executeQuery();
System.out.println("66" + sql);
System.out.println("66stmt : " + stmt);
System.out.println("66st : " + st);
System.out.println("66one----");
if (st == null) {
return null;
}
System.out.println("66two----");
while (st.next()) {
contacts show = new contacts();
show.setId(st.getInt("id"));
show.setName(st.getString("name"));
show.setMobilephone(st.getString("mobilephone"));
show.setTelephone(st.getString("telephone"));
show.setBirthday(st.getString("birthday"));
show.setSex(st.getString("sex"));
show.setAddress(st.getString("address"));
show.setRelationship(st.getString("relationship"));
show.setUserId(st.getInt("userId"));
conlist.add(show);
}
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception se) {
se.printStackTrace();
} finally {
try {
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e1) {
}
}
return conlist;
}
public void addContacts(String name, String mobilephone, String telephone, String birthday, String sex, String address, String relationship, int userid) {
System.out.println("???name" + name + "mobilephone" + mobilephone + "telephone" + telephone + "birthday" + birthday + "sex" + sex + "address" + address + "relationship" + relationship + "userid" + userid);
Connection conn = null;
PreparedStatement stmt = null;
try {
String dirver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/javaweb";
String user = "root";
String password = "123456";
Class.forName(dirver);
conn = DriverManager.getConnection(url, user, password);
String sql = "insert into contacts values(?,?,?,?,?,?,?,?,?)";
stmt = conn.prepareStatement(sql);
stmt.setString(1, null);
stmt.setString(2, name);
stmt.setString(3, mobilephone);
stmt.setString(4, telephone);
stmt.setString(5, birthday);
stmt.setString(6, sex);
stmt.setString(7, address);
stmt.setString(8, relationship);
stmt.setInt(9, userid);
stmt.executeUpdate();
System.out.println(sql);
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e2) {
}
}
}
public contacts findContact(int id) {
System.out.println("id" + id);
Connection conn = null;
PreparedStatement stmt = null;
ResultSet st = null;
contacts show = new contacts();
try {
String dirver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/javaweb";
String user = "root";
String password = "123456";
Class.forName(dirver);
conn = DriverManager.getConnection(url, user, password);
String sql = "select * from contacts where id = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1,id);
st = stmt.executeQuery();
while(st.next()) {
show.setId(st.getInt("id"));
show.setName(st.getString("name"));
show.setMobilephone(st.getString("mobilephone"));
show.setTelephone(st.getString("telephone"));
show.setBirthday(st.getString("birthday"));
show.setSex(st.getString("sex"));
show.setAddress(st.getString("address"));
show.setRelationship(st.getString("relationship"));
show.setUserId(st.getInt("userId"));
}
System.out.println(sql);
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e2) {
}
}
return show;
}
public void editContact(int id,String name, String mobilephone, String telephone, String birthday, String sex, String address, String relationship, int userid) {
System.out.println("???name" + name + "mobilephone" + mobilephone + "telephone" + telephone + "birthday" + birthday + "sex" + sex + "address" + address + "relationship" + relationship + "userid" + userid);
Connection conn = null;
PreparedStatement stmt = null;
try {
String dirver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/javaweb";
String user = "root";
String password = "123456";
Class.forName(dirver);
conn = DriverManager.getConnection(url, user, password);
String sql = "UPDATE contacts SET name = ? ," +
" mobilephone = ?,telephone = ?,birthday = ?," +
"sex = ?,address = ?,relationship = ?,userid = ? where id = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, name);
stmt.setString(2, mobilephone);
stmt.setString(3, telephone);
stmt.setString(4, birthday);
stmt.setString(5, sex);
stmt.setString(6, address);
stmt.setString(7, relationship);
stmt.setInt(8, userid);
stmt.setInt(9, id);
stmt.executeUpdate();
System.out.println(sql);
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e2) {
}
}
}
public void delContacts(int id) {
System.out.println("id" + id);
Connection conn = null;
PreparedStatement stmt = null;
ResultSet st = null;
contacts show = new contacts();
try {
String dirver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/javaweb";
String user = "root";
String password = "123456";
Class.forName(dirver);
conn = DriverManager.getConnection(url, user, password);
String sql = "delete from contacts where id=?;";
stmt = conn.prepareStatement(sql);
stmt.setInt(1,id);
stmt.executeUpdate();
System.out.println(sql);
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e2) {
}
}
}
public List<contacts> searchContacts(int userid,String msg){
List<contacts> conlist = new ArrayList<contacts>();
System.out.println("searchContacts");
Connection conn = null;
PreparedStatement stmt = null;
ResultSet st = null;
try {
String dirver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/javaweb";
String user = "root";
String password = "123456";
Class.forName(dirver);
conn = DriverManager.getConnection(url, user, password);
String sql = "select * from contacts where userId = ? and name like concat('%',?,'%') or mobilephone like concat('%',?,'%') or telephone like concat('%',?,'%') or address like concat('%',?,'%') or relationship like concat('%',?,'%')";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, userid);
stmt.setString(2,msg);
stmt.setString(3,msg);
stmt.setString(4,msg);
stmt.setString(5,msg);
stmt.setString(6,msg);
System.out.println("userid"+userid+"msg"+msg);
st = stmt.executeQuery();
if (st == null) {
return null;
}
System.out.println("379two----");
while (st.next()) {
contacts show = new contacts();
show.setId(st.getInt("id"));
show.setName(st.getString("name"));
show.setMobilephone(st.getString("mobilephone"));
show.setTelephone(st.getString("telephone"));
show.setBirthday(st.getString("birthday"));
show.setSex(st.getString("sex"));
show.setAddress(st.getString("address"));
show.setRelationship(st.getString("relationship"));
show.setUserId(st.getInt("userId"));
conlist.add(show);
}
for(contacts c:conlist){
System.out.println("usercontroller:"+c.getName()+" "+c.getMobilephone());
}
System.out.println("397已打印表单");
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception se) {
se.printStackTrace();
} finally {
try {
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e1) {
}
}
System.out.println("conlist.size()"+conlist.size());
return conlist;
}
public int getGirlnum (int id){
int girl = 0;
System.out.println("getGirlnum:id " + id);
Connection conn = null;
PreparedStatement stmt = null;
ResultSet st = null;
try {
String dirver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/javaweb";
String user = "root";
String password = "123456";
Class.forName(dirver);
conn = DriverManager.getConnection(url, user, password);
String sql = "select * from contacts where userId = ? and sex = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1,id);
stmt.setString(2,"女");
st = stmt.executeQuery();
while(st.next()) {
girl+=1;
}
System.out.println(sql);
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e2) {
}
}
System.out.println("girl's num:"+girl);
return girl;
}
public int getBoynum (int id){
int boy = 0;
System.out.println("getBoynum:id " + id);
Connection conn = null;
PreparedStatement stmt = null;
ResultSet st = null;
try {
String dirver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/javaweb";
String user = "root";
String password = "123456";
Class.forName(dirver);
conn = DriverManager.getConnection(url, user, password);
String sql = "select * from contacts where userId = ? and sex = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1,id);
stmt.setString(2,"男");
st = stmt.executeQuery();
while(st.next()) {
boy+=1;
}
System.out.println(sql);
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e2) {
}
}
System.out.println("boy's num:"+boy);
return boy;
}
public void editUser(int id,String name, String phone, String email) {
System.out.println("editUser:name" + name + "phone" + phone + "email" + email );
Connection conn = null;
PreparedStatement stmt = null;
try {
String dirver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/javaweb";
String user = "root";
String password = "123456";
Class.forName(dirver);
conn = DriverManager.getConnection(url, user, password);
String sql = "UPDATE user SET name = ? ," +
" phone = ?,email = ? where id = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, name);
stmt.setString(2, phone);
stmt.setString(3, email);
stmt.setInt(4, id);
stmt.executeUpdate();
System.out.println(sql);
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e2) {
}
}
}
public Map<String,Integer> getRegion(int userid){
Map<String,Integer> map = new HashMap<String,Integer>();
map.put("北京", getRegionCount("北京",userid));
if(map.get("北京")==0) {
map.put("北京", null);
System.out.println("548map.get(\"北京\")"+map.get("北京"));
}
map.put("天津", getRegionCount("天津",userid));
if(map.get("天津")==0) {
map.put("天津", null);
}
map.put("上海", getRegionCount("上海",userid));
if(map.get("上海")==0) {
map.put("上海", null);
}
map.put("重庆", getRegionCount("重庆",userid));
if(map.get("重庆")==0) {
map.put("重庆", null);
}
map.put("河北", getRegionCount("河北",userid));
if(map.get("河北")==0) {
map.put("河北", null);
}
map.put("山西", getRegionCount("山西",userid));
if(map.get("山西")==0) {
map.put("山西", null);
}
map.put("辽宁", getRegionCount("辽宁",userid));
if(map.get("辽宁")==0) {
map.put("辽宁", null);
}
map.put("吉林", getRegionCount("吉林",userid));
if(map.get("吉林")==0) {
map.put("吉林", null);
}
map.put("黑龙江", getRegionCount("黑龙江",userid));
if(map.get("黑龙江")==0) {
map.put("黑龙江", null);
}
map.put("江苏", getRegionCount("江苏",userid));
if(map.get("江苏")==0) {
map.put("江苏", null);
}
map.put("浙江", getRegionCount("浙江",userid));
if(map.get("浙江")==0) {
map.put("浙江", null);
}
map.put("安徽", getRegionCount("安徽",userid));
if(map.get("安徽")==0) {
map.put("安徽", null);
}
map.put("福建", getRegionCount("福建",userid));
if(map.get("福建")==0) {
map.put("福建", null);
}
map.put("江西", getRegionCount("江西",userid));
if(map.get("江西")==0) {
map.put("江西", null);
}
map.put("山东", getRegionCount("山东",userid));
if(map.get("山东")==0) {
map.put("山东", null);
}
map.put("河南", getRegionCount("河南",userid));
if(map.get("河南")==0) {
map.put("河南", null);
}
map.put("湖北", getRegionCount("湖北",userid));
if(map.get("湖北")==0) {
map.put("湖北", null);
}
map.put("湖南", getRegionCount("湖南",userid));
if(map.get("湖南")==0) {
map.put("湖南", null);
}
map.put("广东", getRegionCount("广东",userid));
if(map.get("广东")==0) {
map.put("广东", null);
}
map.put("海南", getRegionCount("海南",userid));
if(map.get("海南")==0) {
map.put("海南", null);
}
map.put("四川", getRegionCount("四川",userid));
if(map.get("四川")==0) {
map.put("四川", null);
}
map.put("贵州", getRegionCount("贵州",userid));
if(map.get("贵州")==0) {
map.put("贵州", null);
}
map.put("云南", getRegionCount("云南",userid));
if(map.get("云南")==0) {
map.put("云南", null);
}
map.put("陕西", getRegionCount("陕西",userid));
if(map.get("陕西")==0) {
map.put("陕西", null);
}
map.put("甘肃", getRegionCount("甘肃",userid));
if(map.get("甘肃")==0) {
map.put("甘肃", null);
}
map.put("青海", getRegionCount("青海",userid));
if(map.get("青海")==0) {
map.put("青海", null);
}
map.put("台湾", getRegionCount("台湾",userid));
if(map.get("台湾")==0) {
map.put("台湾", null);
}
map.put("内蒙古", getRegionCount("内蒙古",userid));
if(map.get("内蒙古")==0) {
map.put("内蒙古", null);
}
map.put("广西", getRegionCount("广西",userid));
if(map.get("广西")==0) {
map.put("广西", null);
}
map.put("西藏", getRegionCount("西藏",userid));
if(map.get("西藏")==0) {
map.put("西藏", null);
}
map.put("宁夏", getRegionCount("宁夏",userid));
if(map.get("宁夏")==0) {
map.put("宁夏", null);
}
map.put("新疆", getRegionCount("新疆",userid));
if(map.get("新疆")==0) {
map.put("新疆", null);
}
map.put("香港", getRegionCount("香港",userid));
if(map.get("香港")==0) {
map.put("香港", null);
}
map.put("澳门", getRegionCount("澳门",userid));
if(map.get("澳门")==0) {
map.put("澳门", null);
}
return map;
}
public static int i = 0;
public static int getRegionCount(String regionName,int userid) {
int count=0;
Connection conn = null;
String sql="SELECT count(*) FROM contacts WHERE address LIKE CONCAT('%',?,'%') AND userId=?";
try {
String dirver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/javaweb";
String user = "root";
String password = "123456";
Class.forName(dirver);
conn = DriverManager.getConnection(url, user, password);
PreparedStatement psmt=conn.prepareStatement(sql);
psmt.setString(1, regionName);
psmt.setInt(2, userid);
ResultSet rs=psmt.executeQuery();
while(rs.next()) {
count=rs.getInt(1);
System.out.println("710:"+count+i);
i++;
}
conn.close();
psmt.close();
rs.close();
}catch(Exception e) {
e.printStackTrace();
}
return count;
}
}
贴代码好累,我死了,
(朗朗头发真多表情包真好用口合口合口合口合)