使用技术:
Oracle 数据库
前端后台: Servlet + jsp + JDBC + html + css + js
前端界面自定, 但一定实现需要的功能
实现功能:
用户可以登录
登录之后可以看到自己的存下来的联系人信息, 联系人字段自定(至少包含字段: 联系人姓名, 联系人电话, 存入的时间)
如果直接访问电话本页面需要提醒登录
添加联系人
记录一条数据到数据库,
删除联系人
修改联系人信息
查询联系人
按照名称模糊查询
按照电话号码模糊查询
增删改查的操作不管成功与失败都要有提示消息
第一版本:布置了项目之后自己写的,很多地方因为经验不足,知识储备不足,实现的方法不合适,样式也没有设置
第二版本:上课讲解,实现的方法更加规范
修改功能跳转到其他页面实现
com.util 工具包 包含 通讯录类,用户类,数据库JDBC设置,数据库操作方法类
1 //JDBC设置
2 package com.util;
3
4 import java.sql.Connection;
5 import java.sql.DriverManager;
6 import java.sql.SQLException;
7
8 import com.util.JdbcConnectionUtil;
9
10 public class JdbcConnectionUtil {
11 private static final String USERNAME = "test";
12 private static final String PASSWORD = "test";
13 private static final String URL = "jdbc:oracle:thin:@localhost:1521:xe";
14 private static final String DRIVERCLASSNAME = "oracle.jdbc.OracleDriver";
15
16 public static Connection getConnection(){
17 Connection conn=null;
18 try {
19 Class.forName(DRIVERCLASSNAME);
20 conn=DriverManager.getConnection(URL,USERNAME,PASSWORD);
21 } catch (ClassNotFoundException e) {
22 e.printStackTrace();
23 } catch (SQLException e) {
24 e.printStackTrace();
25 }
26 return conn;
27 }
28 public static void destroy(Connection conn){
29 if(conn!=null){
30 try {
31 conn.close();
32 conn=null;
33 } catch (SQLException e) {
34 e.printStackTrace();
35 }
36
37 }
38 }
39 public static void main(String[] args) {
40 System.out.println(JdbcConnectionUtil.getConnection());
41 }
42 }
1 //数据库操作方法
2 package com.util;
3
4 import java.sql.Connection;
5 import java.sql.PreparedStatement;
6 import java.sql.ResultSet;
7 import java.sql.SQLException;
8 import java.util.ArrayList;
9 import java.util.Date;
10 import java.util.List;
11
12 import com.util.JdbcConnectionUtil;
13 import com.util.User;
14
15 public class MethodDal {
16 private Connection con;
17 private PreparedStatement pste;
18 private ResultSet rs;
19
20 // 向用户表表中添加用户
21 public int insertData(User user) {
22 init();
23 int i = -1;
24 String sql = "insert into puser values(?,?)";
25 try {
26 pste = con.prepareStatement(sql);
27 pste.setString(1, user.getUsername());
28 pste.setString(2, user.getPassword());
29 i = pste.executeUpdate();
30 } catch (SQLException e) {
31 e.printStackTrace();
32 }
33 close();
34 return i;
35 }
36
37
38 // 查现有用户名
39 public String selectName(String name) {
40 init();
41 String sql = "select * from puser p where p.pname=?";
42 try {
43 pste = con.prepareStatement(sql);
44 pste.setString(1, name);
45 rs = pste.executeQuery();
46 while (rs.next()) {
47 String pname = rs.getString("pname");
48 if (pname != null) {
49 return pname;
50 }
51 }
52 } catch (SQLException e) {
53 e.printStackTrace();
54 }
55 close();
56 return "no";
57 }
58
59 // 根据姓名查密码
60 public String selectPwd(String name) {
61 init();
62 String sql = "select * from puser p where p.pname=?";
63 try {
64 pste = con.prepareStatement(sql);
65 pste.setString(1, name);
66 rs = pste.executeQuery();
67 while (rs.next()) {
68 String pwd = rs.getString("ppassword");
69 return pwd;
70 }
71 } catch (SQLException e) {
72 e.printStackTrace();
73 }
74 close();
75 return "wu";
76 }
77
78 // 添加电话本信息
79 public int insertPhone(PhoneText p) {
80 init();
81 int i = -1;
82 String sql = "insert into phonetext values(tablexulie.nextval,?,?,?,?)";
83 try {
84 long l = new Date().getTime();
85 pste = con.prepareStatement(sql);
86 pste.setString(1, p.getPuname().trim());
87 pste.setString(2, p.getPhname().trim());
88 pste.setString(3, p.getPhone().trim());
89 pste.setDate(4, new java.sql.Date(l));
90
91 i = pste.executeUpdate();
92 } catch (SQLException e) {
93 e.printStackTrace();
94 }
95 close();
96 return i;
97 }
98
99 // 查询电话本信息
100 public List<PhoneText> selectPhone(String text,String uname) {
101 init();
102 String sql = "select p.pid,p.phname,p.phone,p.ptime from phonetext p where p.phname like '%" + text
103 + "%' or p.phone like '%" + text + "%' and p.puname=? order by p.ptime desc";// '
104 init();
105 List<PhoneText> list = new ArrayList<PhoneText>();
106 try {
107 pste = con.prepareStatement(sql);
108 pste.setString(1,uname);
109
110 // pste.setString(1,text);
111
112 rs = pste.executeQuery();
113 while (rs.next()) {
114
115 PhoneText ph = new PhoneText();
116 ph.setPid(rs.getInt(1));
117 ph.setPhname(rs.getString(2));
118 ph.setPhone(rs.getString(3));
119 ph.setPtime(rs.getDate(4));
120 list.add(ph);
121 }
122
123 } catch (SQLException e) {
124 e.printStackTrace();
125 }
126 close();
127 return list;
128 }
129
130 // 查询所所属用户名的通讯录
131 public List<PhoneText> selectAllphone(String username) {
132 String sql = "select pid,phname,phone,ptime from phonetext where puname='"+username+"' order by ptime desc ";
133 init();
134 List<PhoneText> list = new ArrayList<PhoneText>();
135 try {
136 pste = con.prepareStatement(sql);
137 rs = pste.executeQuery();
138 while (rs.next()) {
139
140 PhoneText ph = new PhoneText();
141 ph.setPid(rs.getInt(1));
142 ph.setPhname(rs.getString(2));
143 ph.setPhone(rs.getString(3));
144 ph.setPtime(rs.getDate(4));
145 list.add(ph);
146 }
147
148 } catch (SQLException e) {
149 e.printStackTrace();
150 }
151 close();
152 return list;
153 }
154
155 // 更新表中的数据
156 public int updateData(int id, String phname,String phone) {
157 init();
158 int i = -1;
159 String sql = "update phonetext p set p.phname=?,p.phone=? where p.pid=? ";
160 try {
161 pste = con.prepareStatement(sql);
162 pste.setString(1, phname);
163 pste.setString(2, phone);
164 pste.setInt(3, id);
165 i = pste.executeUpdate();
166 } catch (SQLException e) {
167 e.printStackTrace();
168 }
169 close();
170 return i;
171 }
172 // 删除记录
173 public int deletePhone(int id) {
174 init();
175 int i = -1;
176 String sql = "delete phonetext where pid=?";
177 try {
178 pste = con.prepareStatement(sql);
179 pste.setInt(1, id);
180 i = pste.executeUpdate();
181 } catch (SQLException e) {
182 e.printStackTrace();
183 }
184 close();
185 return i;
186 }
187 // 查id信息
188 public List<PhoneText> selectId(int id) {
189 init();
190 String sql = "select p.pid,p.phname,p.phone,p.ptime from phonetext p where p.pid=?";
191 PhoneText ph = new PhoneText();
192 List<PhoneText> list = new ArrayList<PhoneText>();
193 try {
194 pste = con.prepareStatement(sql);
195 pste.setInt(1, id);
196 rs = pste.executeQuery();
197
198 while (rs.next()) {
199
200
201 ph.setPid(rs.getInt(1));
202 ph.setPhname(rs.getString(2));
203 ph.setPhone(rs.getString(3));
204 ph.setPtime(rs.getDate(4));
205 list.add(ph);
206 }
207
208 } catch (SQLException e) {
209 e.printStackTrace();
210 }
211 close();
212 return list;
213 }
214
215 // 初始化链接
216 public void init() {
217 con = JdbcConnectionUtil.getConnection();
218 }
219
220 public void close() {
221 JdbcConnectionUtil.destroy(con);
222 }
223 }
1 //通讯录
2 package com.util;
3
4 import java.sql.Date;
5
6 public class PhoneText {
7
8 private String puname;
9 private String phname;
10 private String phone;
11 private Date ptime;
12
13 public PhoneText(){}
14
15 public PhoneText(int pid,String puname,String phname,String phone,Date ptime){
16 super();
17 this.pid=pid;
18 this.puname=puname;
19 this.phname=phname;
20 this.phone=phone;
21 this.ptime=ptime;
22
23 }
24 public int getPid() {
25 return pid;
26 }
27
28 public void setPid(int pid) {
29 this.pid = pid;
30 }
31 private int pid;
32
33 public String getPuname() {
34 return puname;
35 }
36 public void setPuname(String puname) {
37 this.puname = puname;
38 }
39 public String getPhname() {
40 return phname;
41 }
42 public void setPhname(String phname) {
43 this.phname = phname;
44 }
45 public String getPhone() {
46 return phone;
47 }
48 public void setPhone(String phone) {
49 this.phone = phone;
50 }
51 public Date getPtime() {
52 return ptime;
53 }
54 public void setPtime(Date ptime) {
55 this.ptime = ptime;
56 }
57 }
1 //用户类
2 package com.util;
3
4 public class User {
5 private String username;
6 private String password;
7
8 public String getUsername() {
9 return username;
10 }
11
12 public void setUsername(String username) {
13 this.username = username;
14 }
15
16 public String getPassword() {
17 return password;
18 }
19
20 public void setPassword(String password) {
21 this.password = password;
22 }
23 }
com.servlet 各种servlet 处理 注册,登录,添加,删除,修改 的逻辑
1 //注册逻辑
2 package com.servlet;
3
4 import java.io.IOException;
5 import javax.servlet.ServletException;
6 import javax.servlet.annotation.WebServlet;
7 import javax.servlet.http.HttpServlet;
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
10 import com.util.MethodDal;
11 import com.util.User;
12
13 /**
14 * Servlet implementation class ZhueServlet
15 */
16 @WebServlet("/ZhueServlet")
17 public class ZhueServlet extends HttpServlet {
18
19 private static final long serialVersionUID = 1L;
20
21 /**
22 * @see HttpServlet#HttpServlet()
23 */
24 public ZhueServlet() {
25 super();
26 // TODO Auto-generated constructor stub
27 }
28
29 /**
30 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
31 */
32 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
33 //首先设置可以处理中文
34 request.setCharacterEncoding("utf-8");
35 response.setCharacterEncoding("utf-8");
36 response.setContentType("text/html; charset=utf-8");
37 //获取传入的数据
38 String username=request.getParameter("username");
39 String password=request.getParameter("password");
40 String password1=request.getParameter("password1");
41 String realname=request.getParameter("realname");
42 //调用方法判断传入的参数有没有空,都不为空才可以执行下去
43 if(checkParam(username,password,password1)){
44 if(password.equals(password1)){
45 MethodDal m=new MethodDal();
46 if(m.selectName(username).equals("no")){//调用方法根据用户名查询,如果返回no说明数据库没有此用户名,可以注册
47 User user=new User();//实例化用户类并添加信息
48 user.setUsername(username);
49 user.setPassword(password);
50 m.insertData(user);//将实例化的用户传到添加用户的方法
51 response.sendRedirect("message.jsp?code=1");
52 }else{
53 response.sendRedirect("message.jsp?code=4");
54 }
55 }else{
56 response.sendRedirect("message.jsp?code=2");
57 }
58 }else{
59 response.sendRedirect("message.jsp?code=3");
60 }
61 }
62
63 /**
64 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
65 */
66 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
67 // TODO Auto-generated method stub
68 doGet(request, response);
69 }
70 //判断传入的参数有没有空的方法,只要有空的就返回false
71 public boolean checkParam(String... args){//这样传参数代表参数个数不确定,传几个都可以
72 for(String s : args){
73 if("".equals(s)||s==null){
74 return false;
75 }
76 }
77 return true;
78 }
79 }
1 //登录逻辑
2 package com.servlet;
3
4 import java.io.IOException;
5 import javax.servlet.ServletException;
6 import javax.servlet.annotation.WebServlet;
7 import javax.servlet.http.HttpServlet;
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
10 import javax.servlet.http.HttpSession;
11
12 import com.util.MethodDal;
13 import com.util.User;
14
15 /**
16 * Servlet implementation class DengluServlet
17 */
18 @WebServlet("/DengluServlet")
19 public class DengluServlet extends HttpServlet {
20 private static final long serialVersionUID = 1L;
21
22 /**
23 * @see HttpServlet#HttpServlet()
24 */
25 public DengluServlet() {
26 super();
27 // TODO Auto-generated constructor stub
28 }
29
30 /**
31 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
32 */
33 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
34 //首先设置可以处理中文
35 request.setCharacterEncoding("utf-8");
36 response.setCharacterEncoding("utf-8");
37 response.setContentType("text/html; charset=utf-8");
38 //获取传入的数据
39 String username=request.getParameter("username");
40 String password=request.getParameter("password");
41
42 MethodDal m=new MethodDal();
43 //调用方法判断传入的参数有没有空,都不为空才可以执行下去
44 if(checkParam(username,password)){
45 if(!(m.selectName(username).equals("no"))){//调用方法根据用户名查询,如果返回不为no说明数据库有此用户
46 if((m.selectPwd(username)).equals(password)){//根据用户名查询密码,并对比输入的密码和数据库的密码
47 //用request.getSession()创建出客户端的session对象并存内容,用作主页的验证
48 HttpSession session=request.getSession();
49 session.setAttribute("name", username);
50
51 response.sendRedirect("indextest1.jsp");
52 }else{
53 response.sendRedirect("message.jsp?code=4");
54 }
55 }else{
56 response.sendRedirect("message.jsp?code=5");
57 }
58 }else{
59 response.sendRedirect("message.jsp?code=3");
60 }
61 }
62
63
64 /**
65 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
66 */
67 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
68 // TODO Auto-generated method stub
69 doGet(request, response);
70 }
71 //判断传入的参数有没有空的方法,只要有空的就返回false
72 public boolean checkParam(String... args){//这样传参数代表参数个数不确定,传几个都可以
73 for(String s : args){
74 if("".equals(s)||s==null){
75 return false;
76 }
77 }
78 return true;
79 }
80 }
1 //添加联系人逻辑
2 package com.servlet;
3
4 import java.io.IOException;
5 import java.util.Date;
6
7 import javax.servlet.ServletException;
8 import javax.servlet.annotation.WebServlet;
9 import javax.servlet.http.HttpServlet;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12
13 import com.util.MethodDal;
14 import com.util.PhoneText;
15
16 /**
17 * Servlet implementation class TianjiaServlet
18 */
19 @WebServlet("/TianjiaServlet")
20 public class TianjiaServlet extends HttpServlet {
21 private static final long serialVersionUID = 1L;
22
23 /**
24 * @see HttpServlet#HttpServlet()
25 */
26 public TianjiaServlet() {
27 super();
28 // TODO Auto-generated constructor stub
29 }
30
31 /**
32 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
33 */
34 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
35 request.setCharacterEncoding("utf-8");
36 response.setCharacterEncoding("utf-8");
37 response.setContentType("text/html; charset=utf-8");
38
39 String puname=request.getParameter("puname");
40 String phname=request.getParameter("phname");
41 String phone=request.getParameter("phone");
42
43
44 MethodDal m=new MethodDal();
45 //调用方法判断传入的参数有没有空,都不为空才可以执行下去
46 if(checkParam(puname,phname,phone)){
47 MethodDal me=new MethodDal();
48 PhoneText ph=new PhoneText();
49 ph.setPhname(phname);
50 ph.setPhone(phone);
51 ph.setPuname(puname);
52
53 me.insertPhone(ph);
54 response.sendRedirect("message.jsp?code=7");
55 }else{
56 response.sendRedirect("message.jsp?code=3");
57 }
58 }
59
60 /**
61 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
62 */
63 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
64 // TODO Auto-generated method stub
65 doGet(request, response);
66 }
67 //判断传入的参数有没有空的方法,只要有空的就返回false
68 public boolean checkParam(String... args){//这样传参数代表参数个数不确定,传几个都可以
69 for(String s : args){
70 if("".equals(s)||s==null){
71 return false;
72 }
73 }
74 return true;
75 }
76 }
1 //修改联系人数据逻辑
2 package com.servlet;
3
4 import java.io.IOException;
5 import javax.servlet.ServletException;
6 import javax.servlet.annotation.WebServlet;
7 import javax.servlet.http.HttpServlet;
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
10
11 import com.util.MethodDal;
12
13 /**
14 * Servlet implementation class UpdateServlet
15 */
16 @WebServlet("/UpdateServlet")
17 public class UpdateServlet extends HttpServlet {
18 private static final long serialVersionUID = 1L;
19
20 /**
21 * @see HttpServlet#HttpServlet()
22 */
23 public UpdateServlet() {
24 super();
25 // TODO Auto-generated constructor stub
26 }
27
28 /**
29 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
30 */
31 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
32 //设置可以处理中文
33 request.setCharacterEncoding("utf-8");
34 response.setCharacterEncoding("utf-8");
35 response.setContentType("text/html; charset=utf-8");
36 //获取传入的参数
37 String phname=request.getParameter("phname");
38 String phone=request.getParameter("phone");
39 String pid=request.getParameter("pid");
40 //将获取的ID转换为int类型
41 int intpid=Integer.parseInt(pid);
42
43 MethodDal m=new MethodDal();
44 //调用修改的方法
45 int i=m.updateData(intpid, phname, phone);
46 if(i==-1){
47 response.sendRedirect("message.jsp?code=10");
48 }else{
49 response.sendRedirect("message.jsp?code=9");
50 }
51 }
52
53 /**
54 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
55 */
56 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
57 // TODO Auto-generated method stub
58 doGet(request, response);
59 }
60 }
1 //删除联系人的逻辑
2 package com.servlet;
3
4 import java.io.IOException;
5 import javax.servlet.ServletException;
6 import javax.servlet.annotation.WebServlet;
7 import javax.servlet.http.HttpServlet;
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
10
11 import com.util.MethodDal;
12 import com.util.User;
13
14 /**
15 * Servlet implementation class DeleteServlet
16 */
17 @WebServlet("/DeleteServlet")
18 public class DeleteServlet extends HttpServlet {
19 private static final long serialVersionUID = 1L;
20
21 /**
22 * @see HttpServlet#HttpServlet()
23 */
24 public DeleteServlet() {
25 super();
26 // TODO Auto-generated constructor stub
27 }
28
29 /**
30 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
31 */
32 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
33 request.setCharacterEncoding("utf-8");
34 response.setCharacterEncoding("utf-8");
35 response.setContentType("text/html; charset=utf-8");
36
37 String pid=request.getParameter("pid");
38 int intpid=Integer.parseInt(pid);
39 MethodDal m=new MethodDal();
40 m.deletePhone(intpid);
41 response.sendRedirect("message.jsp?code=8");
42 }
43
44 /**
45 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
46 */
47 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
48 // TODO Auto-generated method stub
49 doGet(request, response);
50 }
51 }
然后是JSP前端页面
注册:
1 <%@ page language="java" contentType="text/html; charset=utf-8"
2 pageEncoding="utf-8"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
7 <title>Insert title here</title>
8 </head>
9 <body>
10
11 <form action="ZhueServlet" method="post">
12 用户名:<input type="text" name="username"><br>
13 密码:<input type="password" name="password"><br>
14 确认密码:<input type="password" name="password1"><br>
15 真实姓名:<input type="text" name="realname"><br>
16 <input type="submit" value="提交"><br>
17 </form>
18 </body>
19 </html>
登录:
1 <%@ page language="java" contentType="text/html; charset=utf-8"
2 pageEncoding="utf-8"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
7 <title>Insert title here</title>
8 </head>
9 <body>
10
11 <form action="DengluServlet" method="post">
12 用户名:<input type="text" name="username"><br>
13 密码:<input type="password" name="password"><br>
14 <input type="submit" value="提交"><br>
15 <a href='zhuce.jsp'>注册</a>
16 </form>
17 </body>
18 </html>
主页,显示页:
1 <%@ page language="java" contentType="text/html; charset=utf-8"
2 pageEncoding="utf-8" import="com.util.*,java.util.*" %>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <title>通讯录</title>
7 <%
8 //首先要验证有没有登录,如果没有登录跳转到登录界面
9 Object obj1=session.getAttribute("name");
10 String uname=(String)obj1;
11 if (obj1 == null) {
12 response.sendRedirect("denglu.jsp");
13 }
14 %>
15 </head>
16 <body >
17 <h1 style="font-size:38px;">欢迎</h1>
18 <a style="font-size:26px;" href="">查看联系人</a>
19 <a style="font-size:26px;" href="indextianjia.jsp">添加</a>
20
21 <form action="indexselect.jsp" method="post" >
22 <input type="text" name="select"/>
23 <input type="submit" value="查询(跳转页面)">
24 </form>
25 <table border="1" align="center" width="60%">
26 <tr>
27 <th>客户编码</th>
28 <th>姓名</th>
29 <th>电话</th>
30 <th>添加时间</th>
31 <th>编辑</th>
32 <th>删除</th>
33 </tr>
34 <%
35 MethodDal m=new MethodDal();
36 List<PhoneText> list=m.selectAllphone(uname);
37
38 if(list!=null){
39
40 for(PhoneText l: list ){
41 out.print("<tr>");
42 out.print("<td>"+l.getPid()+"</td>");
43 out.print("<td>"+l.getPhname()+"</td>");
44 out.print("<td>"+l.getPhone()+"</td>");
45 out.print("<td>"+l.getPtime()+"</td>");
46 out.print("<td><a href='indexupdate.jsp?pid="+l.getPid()+"'>编辑</td><br>");
47 out.print("<td><a href='DeleteServlet?pid="+l.getPid()+"'>删除</td><br>");
48 out.print("</tr>");
49 }
50 }
51 %>
52 </table>
53 </body>
54 </html>
55 <script>
56 </script>
添加联系人:
1 <%@ page language="java" contentType="text/html; charset=utf-8"
2 pageEncoding="utf-8" import="com.util.*,java.util.*" %>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <title>list.jsp</title>
7 </head>
8
9 <body >
10 <h1 style="font-size:38px;">欢迎</h1>
11 <a style="font-size:26px;" href="indextest1.jsp">查看联系人</a>
12 <a style="font-size:26px;" href="indextianjia">添加</a>
13
14 <form action="TianjiaServlet" method="post">
15 用户名:<input type="text" name="puname" value="
16 <% Object obj1=session.getAttribute("name");
17 String uname=(String)obj1;
18 out.print(uname);
19 %>"><br>
20 联系人姓名:<input type="text" name="phname"><br>
21 联系人电话:<input type="text" name="phone"><br>
22 <input type="submit" value="提交"><br>
23 </form>
24 </table>
25 </body>
26 </html>
查询页面:
1 <%@ page language="java" contentType="text/html; charset=utf-8"
2 pageEncoding="utf-8" import="com.util.*,java.util.*"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <title>list.jsp</title>
7 </head>
8 <body>
9 <div id="select">
10 <h1 style="font-size: 38px;">查询结果:</h1>
11 <a style="font-size: 26px;"> <a style="font-size: 26px;"
12 href="indextianjia.jsp">添加</a>
13
14 <table border="1" align="center" width="60%">
15 <tr>
16 <th>客户编码</th>
17 <th>姓名</th>
18 <th>电话</th>
19 <th>添加时间</th>
20 <th>编辑</th>
21
22 </tr>
23 <%
24 //获取用户名用来筛选
25 Object obj1 = session.getAttribute("name");
26 String uname = (String) obj1;
27 String selecttext = request.getParameter("select");
28 MethodDal m = new MethodDal();
29 List<PhoneText> list = m.selectPhone(selecttext, uname);
30 if (list != null) {
31
32 for (PhoneText l : list) {
33 out.print("<tr>");
34 out.print("<td>" + l.getPid() + "</td>");
35 out.print("<td>" + l.getPhname() + "</td>");
36 out.print("<td>" + l.getPhone() + "</td>");
37 out.print("<td>" + l.getPtime() + "</td>");
38 out.print("<td> </td>" + "<br>");
39 out.print("</tr>");
40 }
41 }
42 %>
43 </table>
44 </div>
45 </body>
46 </html>
model包,用户类,通讯录类
1 package com.hanqi.maya.model;
2
3 import java.io.Serializable;
4 import java.util.Date;
5
6 /**
7 * 联系人实体类
8 * @author 9 */
10 public class ContactBook implements Serializable {
11
12 private static final long serialVersionUID = 1L;
13 /**
14 * 主键
15 */
16 private Integer id;
17 /**
18 * 联系人姓名
19 */
20 private String name;
21 /**
22 * 电话号码
23 */
24 private String tel;
25 /**
26 * 性别
27 */
28 private String sex;
29 /**
30 * 添加时间
31 */
32 private Date addtime;
33 /**
34 * 分组信息
35 */
36 private String tcgroup;
37 /**
38 * 所属用户
39 */
40 private String username;
41
42 public ContactBook() {
43 super();
44 }
45
46 public ContactBook(String name, String tel, String sex, String tcgroup) {
47 super();
48 this.name = name;
49 this.tel = tel;
50 this.sex = sex;
51 this.tcgroup = tcgroup;
52 }
53
54 public ContactBook(Integer id, String name, String tel, String sex, Date addtime, String tcgroup, String username) {
55 super();
56 this.id = id;
57 this.name = name;
58 this.tel = tel;
59 this.sex = sex;
60 this.addtime = addtime;
61 this.tcgroup = tcgroup;
62 this.username = username;
63 }
64
65 public Integer getId() {
66 return id;
67 }
68
69 public void setId(Integer id) {
70 this.id = id;
71 }
72
73 public String getName() {
74 return name;
75 }
76
77 public void setName(String name) {
78 this.name = name;
79 }
80
81 public String getTel() {
82 return tel;
83 }
84
85 public void setTel(String tel) {
86 this.tel = tel;
87 }
88
89 public String getSex() {
90 return sex;
91 }
92
93 public void setSex(String sex) {
94 this.sex = sex;
95 }
96
97 public Date getAddtime() {
98 return addtime;
99 }
100
101 public void setAddtime(Date addtime) {
102 this.addtime = addtime;
103 }
104
105 public String getTcgroup() {
106 return tcgroup;
107 }
108
109 public void setTcgroup(String tcgroup) {
110 this.tcgroup = tcgroup;
111 }
112
113 public String getUsername() {
114 return username;
115 }
116
117 public void setUsername(String username) {
118 this.username = username;
119 }
120
121 @Override
122 public String toString() {
123 return "ContactBook [id=" + id + ", name=" + name + ", tel=" + tel + ", sex=" + sex + ", addtime=" + addtime
124 + ", tcgroup=" + tcgroup + ", username=" + username + "]";
125 }
126
127 }
1 package com.hanqi.maya.model;
2
3 import java.io.Serializable;
4 import java.util.Date;
5
6 /**
7 * 用户实体类
8 * @author 9 */
10 public class ContactUser implements Serializable {
11
12 private static final long serialVersionUID = 1L;
13 /**
14 * 用户名
15 */
16 private String username;
17 /**
18 * 用户密码
19 */
20 private String password;
21 /**
22 * 姓名
23 */
24 private String realname;
25 /**
26 * 注册时间
27 */
28 private Date createtime;
29
30 public ContactUser() {
31 }
32
33 public ContactUser(String username, String password, String realname, Date createtime) {
34 this.username = username;
35 this.password = password;
36 this.realname = realname;
37 this.createtime = createtime;
38 }
39
40 public ContactUser(String username, String password, String realname) {
41 this.username = username;
42 this.password = password;
43 this.realname = realname;
44 }
45
46 public String getUsername() {
47 return username;
48 }
49
50 public void setUsername(String username) {
51 this.username = username;
52 }
53
54 public String getPassword() {
55 return password;
56 }
57
58 public void setPassword(String password) {
59 this.password = password;
60 }
61
62 public String getRealname() {
63 return realname;
64 }
65
66 public void setRealname(String realname) {
67 this.realname = realname;
68 }
69
70 public Date getCreatetime() {
71 return createtime;
72 }
73
74 public void setCreatetime(Date createtime) {
75 this.createtime = createtime;
76 }
77
78 }
util包 包含数据库设置,日期转换工具,数据库操作
1 package com.hanqi.maya.util;
2
3 import java.sql.Connection;
4 import java.sql.DriverManager;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7 import java.sql.Statement;
8
9 /**
10 * 数据库驱动连接类
11 * @author ZBK
12 */
13 public class DBHelper {
14 /**
15 * 数据库用户名
16 */
17 public static final String USERNAME = "test";
18 /**
19 * 数据库密码
20 */
21 public static final String PASSWORD = "test";
22 /**
23 * 数据库驱动类
24 */
25 public static final String DRIVER = "oracle.jdbc.OracleDriver";
26 /**
27 * 数据库地址URL
28 */
29 public static final String URL = "jdbc:oracle:thin:@localhost:1521:xe";
30
31 /**
32 * 获取数据库连接
33 * @return
34 */
35 public static Connection getConnection() {
36 Connection conn = null;
37 try {
38 Class.forName(DRIVER);
39 conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
40 } catch (ClassNotFoundException e) {
41 e.printStackTrace();
42 } catch (SQLException e) {
43 e.printStackTrace();
44 }
45 return conn;
46 }
47
48 /**
49 * 释放资源
50 * @param conn 数据库连接对象
51 * @param sm Statement对象
52 * @param rs ResultSet结果集对象
53 */
54 public static void destroy(Connection conn, Statement sm, ResultSet rs) {
55 if (conn != null) {
56 try {
57 conn.close();
58 } catch (SQLException e) {
59 e.printStackTrace();
60 }
61 conn = null;
62 }
63 if (sm != null) {
64 try {
65 sm.close();
66 } catch (SQLException e) {
67 e.printStackTrace();
68 }
69 sm = null;
70 }
71 if (rs != null) {
72 try {
73 rs.close();
74 } catch (SQLException e) {
75 e.printStackTrace();
76 }
77 rs = null;
78 }
79 }
80
81 /**
82 * 验证前台传入的参数是否为空
83 * @param args
84 * @return
85 */
86 public static boolean checkParam(String... args) {
87 for (String s : args) {
88 if (s == null || s.trim().length() < 1) {
89 return false;
90 }
91 }
92 return true;
93 }
94 }
1 package com.hanqi.maya.util;
2
3 import java.text.ParseException;
4 import java.text.SimpleDateFormat;
5 import java.util.Date;
6
7 /**
8 * 日期转换器
9 * @author10 */
11 public class DateConvertor {
12
13 private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
14
15 /**
16 * 将字符串转换成Date类型
17 * @param str
18 * @return
19 */
20 public static Date putString2Date(String str) {
21 Date date = null;
22 if(str.trim().length()<=0) {
23 return null;
24 }
25 try {
26 date = new Date(sdf.parse(str).getTime());
27 } catch (ParseException e) {
28 e.printStackTrace();
29 }
30 return date;
31 }
32
33 /**
34 * 将日期类型转换成字符串
35 * @param dd
36 * @return
37 */
38 public static String putDate2String(Date dd) {
39 return sdf.format(dd);
40 }
41 }
servelt处理逻辑
登录逻辑:
1 package com.hanqi.maya.servlet;
2
3 import java.io.IOException;
4 import javax.servlet.ServletException;
5 import javax.servlet.annotation.WebServlet;
6 import javax.servlet.http.HttpServlet;
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
9
10 import com.hanqi.maya.model.ContactUser;
11 import com.hanqi.maya.util.DBHelper;
12 import com.hanqi.maya.util.DataBaseMethodDal;
13
14 /**
15 * Servlet implementation class LoginServlet
16 */
17 @WebServlet("/LoginServlet")
18 public class LoginServlet extends HttpServlet {
19 private static final long serialVersionUID = 1L;
20
21 /**
22 * @see HttpServlet#HttpServlet()
23 */
24 public LoginServlet() {
25 super();
26 // TODO Auto-generated constructor stub
27 }
28
29 /**
30 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
31 * response)
32 */
33 protected void doGet(HttpServletRequest request, HttpServletResponse response)
34 throws ServletException, IOException {
35 request.setCharacterEncoding("utf-8");
36 response.setCharacterEncoding("utf-8");
37 response.setContentType("text/html; charset=utf-8");
38
39 String username = request.getParameter("username");
40 String password = request.getParameter("password");
41
42 DataBaseMethodDal dbmd = new DataBaseMethodDal();
43
44 if (DBHelper.checkParam(username, password)) {
45 ContactUser user = dbmd.selectUser(username, password);
46 if (user != null) {
47 request.getSession().setAttribute("currentUser", user);
48 r(response, "ShowContactInfoServlet", -1);
49 } else {
50 r(response, "message.jsp", 5);
51 }
52 } else {
53 r(response, "message.jsp", 1);
54 }
55 }
56
57 /**
58 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
59 * response)
60 */
61 protected void doPost(HttpServletRequest request, HttpServletResponse response)
62 throws ServletException, IOException {
63 // TODO Auto-generated method stub
64 doGet(request, response);
65 }
66
67 public static void r(HttpServletResponse response, String page, int code) {
68 try {
69 response.sendRedirect(page + "?code=" + code);
70 } catch (IOException e) {
71 e.printStackTrace();
72 }
73 }
74
75 }
注册逻辑
package com.hanqi.maya.servlet;
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 com.hanqi.maya.model.ContactUser;
import com.hanqi.maya.util.DBHelper;
import com.hanqi.maya.util.DataBaseMethodDal;
/**
* Servlet implementation class RegisterServlet
*/
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public RegisterServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
String password1 = request.getParameter("password1");
String realname = request.getParameter("realname");
DataBaseMethodDal dbmd = new DataBaseMethodDal();
if(DBHelper.checkParam(username, password, password1)) {
if(password.equals(password1)) {
ContactUser cu = new ContactUser(username, password, realname);
int a = dbmd.insertUser(cu);
if(a > 0) {
r(response, "message.jsp", 3);
} else {
r(response, "message.jsp", 4);
}
} else {
r(response, "message.jsp", 2);
}
} else {
r(response, "message.jsp",1);
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
public static void r(HttpServletResponse response, String page, int code) {
try {
response.sendRedirect(page+"?code="+code);
} catch (IOException e) {
e.printStackTrace();
}
}
}
添加和修改
package com.hanqi.maya.servlet;
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 com.hanqi.maya.model.ContactBook;
import com.hanqi.maya.model.ContactUser;
import com.hanqi.maya.util.DBHelper;
import com.hanqi.maya.util.DataBaseMethodDal;
/**
* Servlet implementation class InsertContactBookServlet
*/
@WebServlet("/InsertContactBookServlet")
public class InsertContactBookServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public InsertContactBookServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
ContactUser cu = (ContactUser) request.getSession().getAttribute("currentUser");
String id = request.getParameter("id");
String cname = request.getParameter("cname");
String tel = request.getParameter("tel");
String sex = request.getParameter("sex");
String tcgroup = request.getParameter("tcgroup");
DataBaseMethodDal dbmd = new DataBaseMethodDal();
ContactBook cb = new ContactBook(cname, tel, sex, tcgroup);
if (id == null || id.trim().length() <= 0) {
if (DBHelper.checkParam(cname, tel, sex, tcgroup)) {
int a = dbmd.insertBook(cb, cu.getUsername());
if (a > 0) {
r(response, "ShowContactInfoServlet", 1);
} else {
r(response, "message.jsp", 4);
}
} else {
r(response, "message.jsp", 1);
}
} else {
cb.setId(Integer.parseInt(id));
int a = dbmd.updateBook(cb, cu.getUsername());
if (a > 0) {
r(response, "ShowContactInfoServlet", 1);
} else {
r(response, "message.jsp", 4);
}
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
public static void r(HttpServletResponse response, String page, int code) {
try {
response.sendRedirect(page + "?code=" + code);
} catch (IOException e) {
e.printStackTrace();
}
}
}
显示
package com.hanqi.maya.servlet;
import java.io.IOException;
import java.util.List;
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 com.hanqi.maya.model.ContactBook;
import com.hanqi.maya.model.ContactUser;
import com.hanqi.maya.util.DataBaseMethodDal;
/**
* Servlet implementation class ShowContactInfoServlet
*/
@WebServlet("/ShowContactInfoServlet")
public class ShowContactInfoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ShowContactInfoServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
DataBaseMethodDal dbmd = new DataBaseMethodDal();
ContactUser cu = (ContactUser)request.getSession().getAttribute("currentUser");
String isSearch = request.getParameter("isSearch");
ContactBook cb = null;
if(isSearch!=null&&"do".equals(isSearch)) {
String cname = request.getParameter("cname");
String tel = request.getParameter("tel");
String sex = request.getParameter("sex");
String tcgroup = request.getParameter("tcgroup");
cb = new ContactBook(cname, tel, sex, tcgroup);
}
if(cu!=null) {
List<ContactBook> cbList = dbmd.selectAllContactInfo(cb, cu.getUsername());
if(cbList!=null) {
request.getSession().setAttribute("cbList", cbList);
}
r(response,"index.jsp",0);
} else {
r(response,"message.jsp",0);
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
public static void r(HttpServletResponse response, String page, int code) {
try {
response.sendRedirect(page + "?code=" + code);
} catch (IOException e) {
e.printStackTrace();
}
}
}
1 package com.hanqi.maya.util;
2
3 import java.sql.Connection;
4 import java.sql.PreparedStatement;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7 import java.util.ArrayList;
8 import java.util.List;
9
10 import com.hanqi.maya.model.ContactBook;
11 import com.hanqi.maya.model.ContactUser;
12
13 /**
14 * 调用数据库连接进行数据操作的类
15 *
16 * @author 17 */
18 public class DataBaseMethodDal {
19 private Connection conn;
20 private PreparedStatement ps;
21 private ResultSet rs;
22
23 /**
24 * 初始化数据库链接
25 */
26 public void init(String sql) {
27 conn = DBHelper.getConnection();
28 try {
29 ps = conn.prepareStatement(sql);
30 } catch (SQLException e) {
31 e.printStackTrace();
32 }
33 }
34
35 /**
36 * 注册用户信息
37 *
38 * @param cu
39 * @return
40 */
41 public int insertUser(ContactUser cu) {
42 String sql = "insert into tc_user values(?,?,?,sysdate)";
43
44 init(sql);
45 int a = -1;
46 try {
47 ps.setString(1, cu.getUsername());
48 ps.setString(2, cu.getPassword());
49 ps.setString(3, cu.getRealname());
50 a = ps.executeUpdate();
51 } catch (SQLException e) {
52 e.printStackTrace();
53 }
54 return a;
55 }
56
57 public ContactUser selectUser(String username, String password) {
58 String sql = "select * from tc_user t where t.username=? and t.password=?";
59 init(sql);
60 ContactUser user = null;
61 try {
62 ps.setString(1, username);
63 ps.setString(2, password);
64 rs = ps.executeQuery();
65 while (rs.next()) {
66 user = new ContactUser(rs.getString("username"), rs.getString("password"), rs.getString("realname"),
67 rs.getDate("createtime"));
68 }
69 } catch (SQLException e) {
70 e.printStackTrace();
71 }
72 return user;
73 }
74
75 public List<ContactBook> selectAllContactInfo(ContactBook book, String username) {
76 String sqlplus = "";
77 if (book != null) {
78 if (book.getName().trim().length() > 0) {
79 sqlplus += " and t.cname like '%" + book.getName() + "%' ";
80 }
81 if (book.getTel().trim().length() > 0) {
82 sqlplus += " and t.tel like '%" + book.getTel() + "%' ";
83 }
84 if (book.getSex().trim().length() > 0) {
85 sqlplus += " and t.sex = '" + book.getSex() + "' ";
86 }
87 if (book.getTcgroup().trim().length() > 0) {
88 sqlplus += " and t.tcgroup = '" + book.getTcgroup() + "' ";
89 }
90 }
91 String sql = "select * from TC_CONTACT t where t.username = ?" + sqlplus;
92 System.out.println(sql);
93 init(sql);
94 List<ContactBook> cbList = null;
95 try {
96 ps.setString(1, username);
97 rs = ps.executeQuery();
98 if (rs != null) {
99 cbList = new ArrayList<ContactBook>();
100 while (rs.next()) {
101 ContactBook cb = new ContactBook(rs.getInt("id"), rs.getString("cname"), rs.getString("tel"),
102 rs.getString("sex"), rs.getTimestamp("addtime"), rs.getString("tcgroup"),
103 rs.getString("username"));
104 cbList.add(cb);
105 }
106 }
107 } catch (SQLException e) {
108 e.printStackTrace();
109 }
110 return cbList;
111 }
112
113 public int insertBook(ContactBook cb, String username) {
114 String sql = "insert into TC_CONTACT values(test.nextval, ?,?,?,sysdate,?,?)";
115 init(sql);
116 int a = -1;
117 try {
118 ps.setString(1, cb.getName());
119 ps.setString(2, cb.getTel());
120 ps.setString(3, cb.getSex());
121 ps.setString(4, cb.getTcgroup());
122 ps.setString(5, username);
123 a = ps.executeUpdate();
124 } catch (SQLException e) {
125 e.printStackTrace();
126 }
127 return a;
128 }
129
130 public int delContactBook(String ids) {
131 String sql = "delete TC_CONTACT t where t.id in (" + ids + ")";
132 init(sql);
133 int a = -1;
134 try {
135 a = ps.executeUpdate();
136 } catch (SQLException e) {
137 e.printStackTrace();
138 }
139 return a;
140 }
141
142 public int updateBook(ContactBook cb, String username) {
143 String sql = "update TC_CONTACT t set t.cname=?, t.sex=?, t.tel=?, t.tcgroup=? "
144 + "where t.username=? and t.id=?";
145 init(sql);
146 int a = -1;
147 try {
148 ps.setString(1, cb.getName());
149 ps.setString(2, cb.getSex());
150 ps.setString(3, cb.getTel());
151 ps.setString(4, cb.getTcgroup());
152 ps.setString(5, username);
153 ps.setInt(6, cb.getId());
154 a = ps.executeUpdate();
155 } catch (SQLException e) {
156 e.printStackTrace();
157 }
158 return a;
159 }
160 }
删除
1 package com.hanqi.maya.servlet;
2
3 import java.io.IOException;
4
5 import javax.servlet.ServletException;
6 import javax.servlet.annotation.WebServlet;
7 import javax.servlet.http.HttpServlet;
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
10
11 import com.hanqi.maya.util.DataBaseMethodDal;
12
13 /**
14 * Servlet implementation class DelContactBookServlet
15 */
16 @WebServlet("/DelContactBookServlet")
17 public class DelContactBookServlet extends HttpServlet {
18 private static final long serialVersionUID = 1L;
19
20 /**
21 * @see HttpServlet#HttpServlet()
22 */
23 public DelContactBookServlet() {
24 super();
25 // TODO Auto-generated constructor stub
26 }
27
28 /**
29 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
30 * response)
31 */
32 protected void doGet(HttpServletRequest request, HttpServletResponse response)
33 throws ServletException, IOException {
34 String ids = request.getParameter("ids");
35 if (ids != null && ids.trim().length() > 0) {
36 DataBaseMethodDal dbmd = new DataBaseMethodDal();
37 int a = dbmd.delContactBook(ids);
38 if (a > 0) {
39 response.sendRedirect("ShowContactInfoServlet");
40 } else {
41 response.sendRedirect("message.jsp?code=6");
42 }
43 } else {
44 response.sendRedirect("message.jsp?code=4");
45 }
46 }
47
48 /**
49 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
50 * response)
51 */
52 protected void doPost(HttpServletRequest request, HttpServletResponse response)
53 throws ServletException, IOException {
54 // TODO Auto-generated method stub
55 doGet(request, response);
56 }
57
58 }
JSP页面:
注册页面:
-
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7 <title>Insert title here</title>
8 </head>
9 <body>
10 <form action="RegisterServlet" method="post">
11 <table>
12 <tr>
13 <td>用户名: </td>
14 <td><input type="text" name="username" /></td>
15 </tr>
16 <tr>
17 <td>密码: </td>
18 <td><input type="text" name="password" /></td>
19 </tr>
20 <tr>
21 <td>确认密码: </td>
22 <td><input type="text" name="password1" /></td>
23 </tr>
24 <tr>
25 <td>姓名: </td>
26 <td><input type="text" name="realname" /></td>
27 </tr>
28 <tr>
29 <td><input type="submit" value="注册" /></td>
30 <td><a href="login.jsp">返回登陆</a></td>
31 </tr>
32 </table>
33 </form>
34 </body>
35 </html>
登录页面:
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7 <title>Insert title here</title>
8 </head>
9 <body>
10 <form action="LoginServlet" method="post">
11 <table>
12 <tr>
13 <td>用户名: </td>
14 <td><input type="text" name="username" /></td>
15 </tr>
16 <tr>
17 <td>密码: </td>
18 <td><input type="text" name="password" /></td>
19 </tr>
20 <tr>
21 <td><input type="submit" value="登录" /></td>
22 <td><a href="register.jsp">前往注册</a></td>
23 </tr>
24 </table>
25 </form>
26 </body>
27 </html>
主页:
主页有两个from表单,一个用来进行添加或者修改,另一个用来查询
进行修改和查询的表单有一个隐藏域,name是id,添加的时候id是空的,后台判断id是空的就进行添加,点击修改会触发点击事件 loadForm(id, cname, tel, sex, tcgroup) 然后通过 var f = document.getElementById("addOrUpdateForm"); f.id.value=id; 将信息获取到表单中,然后进行修改
在查询的表单中,有一个隐藏域,如果点击查询,隐藏域会被提交,后台就能接收到它的参数说明是查询操作,如果不能,说明没有点击查询,是第一次登录,后台接收到参数之后,进行判断,如果是查询,
1 <%@ page language="java" contentType="text/html; charset=utf-8"
2 pageEncoding="utf-8"%>
3 <%@ page import="com.hanqi.maya.model.ContactUser"%>
4 <%@ page import="com.hanqi.maya.model.ContactBook"%>
5 <%@ page import="java.util.ArrayList"%>
6 <%@ page import="com.hanqi.maya.util.DateConvertor"%>
7 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
8 <html>
9 <head>
10 <%
11 ArrayList<ContactBook> list = null;
12 ContactUser cu = (ContactUser) session.getAttribute("currentUser");
13 String realname = "没有登录";
14 if (cu == null) {
15 response.sendRedirect("message.jsp?code=0");
16 } else {
17 realname = cu.getRealname();
18 list = (ArrayList<ContactBook>) session.getAttribute("cbList");
19 }
20 %>
21 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
22 <title>Insert title here</title>
23 </head>
24 <body>
25 欢迎, [<%=realname%>]
26 <hr>
27 <form id="addOrUpdateForm" action="InsertContactBookServlet" method="post">
28 <input type="hidden" name="id" />
29 <table>
30 <tr>
31 <td>联系人名称:</td>
32 <td><input type="text" name="cname" /></td>
33 </tr>
34 <tr>
35 <td>电话:</td>
36 <td><input type="text" name="tel" /></td>
37 </tr>
38 <tr>
39 <td>性别:</td>
40 <td><select name="sex">
41 <option value="">未选择</option>
42 <option value="男">男</option>
43 <option value="女">女</option>
44 <option value="其他">其他</option>
45 </select></td>
46 </tr>
47 <tr>
48 <td>分组:</td>
49 <td><select name="tcgroup">
50 <option value="">未选择</option>
51 <option value="同事">同事</option>
52 <option value="朋友">朋友</option>
53 <option value="同学">同学</option>
54 <option value="家人">家人</option>
55 </select></td>
56 </tr>
57 <tr>
58 <td><input type="submit" value="保存"></td>
59 <td><input type="button" value="重置" /></td>
60 </tr>
61 </table>
62 </form>
63 <hr>
64 <h2>查询联系人</h2>
65 <form action="ShowContactInfoServlet" method="post">
66 <input type="hidden" name="isSearch" value="do" />
67 联系人名称:<input type="text" name="cname" />
68 电话:<input type="text" name="tel" />
69 性别:
70 <select name="sex">
71 <option value="">未选择</option>
72 <option value="男">男</option>
73 <option value="女">女</option>
74 <option value="其他">其他</option>
75 </select>
76 分组: <select name="tcgroup">
77 <option value="">未选择</option>
78 <option value="同事">同事</option>
79 <option value="朋友">朋友</option>
80 <option value="同学">同学</option>
81 <option value="家人">家人</option>
82 </select>
83 <input type="submit" value="查询" />
84 <input type="button" id="btn_delMultiple" value="删除选中记录" onclick="confirmMultiDel()" />
85 </form>
86 <hr>
87 <%
88 if (list != null && list.size() > 0) {
89 out.print("<table style='text-align:center;' width='70%' cellpadding='0' cellspacing='0' border='1'>");
90 out.print("<tr><th>联系人姓名</th><th>电话号码</th><th>性别</th><th>分组</th><th>添加时间</th><th>管理</th><th><input type='checkbox' id='leader' onclick='getMultiDel()'></th></tr>");
91 for (ContactBook c : list) {
92 out.print("<tr>");
93 out.print("<td>" + c.getName() + "</td><td>" + c.getTel() + "</td><td>" + c.getSex() + "</td><td>"
94 + c.getTcgroup() + "</td><td>" + DateConvertor.putDate2String(c.getAddtime())
95 + "</td><td><a href='DelContactBookServlet?id="+c.getId()+"' onclick='return confirmDel()'>删除</a> <a href='javascript:void(0)' onclick='loadForm(\""+c.getId()+"\",\""+c.getName()+"\", \""+c.getTel()+"\", \""+c.getSex()+"\", \""+c.getTcgroup()+"\")'>修改</a></td><td><input value='"+c.getId()+"' class='select' type='checkbox'></td>");
96 out.print("</tr>");
97 }
98 out.print("</table>");
99 } else {
100 out.print("没有任何记录 !");
101 }
102 %>
103
104 <script type="text/javascript">
105 function confirmDel() {
106 var r = confirm("确定删除吗?");
107 return r;
108 }
109
110 function getMultiDel() {
111 var c = document.getElementById("leader");
112 var r = c.checked;
113 var checks = document.getElementsByClassName("select");
114 for(var i=0;i<checks.length;i++) {
115 checks[i].checked = r;
116 }
117 }
118
119 function loadForm(id, cname, tel, sex, tcgroup) {
120 var f = document.getElementById("addOrUpdateForm");
121 f.id.value=id;
122 f.cname.value = cname;
123 f.tel.value = tel;
124 f.sex.value = sex;
125 f.tcgroup.value = tcgroup;
126 }
127
128 function confirmMultiDel() {
129 var checks = document.getElementsByClassName("select");
130 var arrayObjs = [];
131 for(var j=0;j<checks.length;j++) {
132 if(checks[j].checked) {
133 arrayObjs.push(checks[j].value);
134 }
135 }
136
137 var r1 = confirm("选中了"+arrayObjs.length+"条数据, 确定要删除吗 ?");
138 if(r1) {
139 window.location.href="DelContactBookServlet?ids="+arrayObjs;
140 }
141 }
142
143 </script>
144 </body>
145 </html>
提示页:
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7 <title>Insert title here</title>
8 </head>
9 <body>
10 <%
11 String code = request.getParameter("code");
12 if ("0".equals(code)) {
13 out.print("<h1>请先登录 !</h1>");
14 }
15 if ("1".equals(code)) {
16 out.print("<h1>输入正确的参数 !</h1>");
17 }
18 if ("2".equals(code)) {
19 out.print("<h1>两次输入的密码不一致 !</h1>");
20 }
21 if ("3".equals(code)) {
22 out.print("<h1>注册成功 !</h1>");
23 }
24 if ("4".equals(code)) {
25 out.print("<h1>后台出现异常 !</h1>");
26 }
27 if ("5".equals(code)) {
28 out.print("<h1>用户不存在或者密码错误 !</h1>");
29 }
30 if ("6".equals(code)) {
31 out.print("<h1>删除联系人信息失败 !</h1>");
32 }
33 %>
34 <hr>
35 <a href="login.jsp">登录</a>
36 <a href="register.jsp">注册</a>
37 <a href="ShowContactInfoServlet">前往主页</a>
38 </body>
39 </html>
1.可以在一个页面设置多个表单进行不同的操作
2.可以设置点击事件,然后使用 var f = document.getElementById("addOrUpdateForm"); f.id.value=id; 将信息获取到指定id 的表单或者表格中