使用技术:

Oracle 数据库

前端后台: Servlet + jsp + JDBC + html + css + js

前端界面自定, 但一定实现需要的功能

实现功能:

用户可以登录

登录之后可以看到自己的存下来的联系人信息, 联系人字段自定(至少包含字段: 联系人姓名, 联系人电话, 存入的时间)

如果直接访问电话本页面需要提醒登录

添加联系人

记录一条数据到数据库,

删除联系人

修改联系人信息

查询联系人

按照名称模糊查询

按照电话号码模糊查询

增删改查的操作不管成功与失败都要有提示消息

第一版本:布置了项目之后自己写的,很多地方因为经验不足,知识储备不足,实现的方法不合适,样式也没有设置

第二版本:上课讲解,实现的方法更加规范

第一版本:

修改功能跳转到其他页面实现

com.util 工具包  包含 通讯录类,用户类,数据库JDBC设置,数据库操作方法类

 //JDBC设置
 package com.util;

 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.SQLException;

 import com.util.JdbcConnectionUtil;

 public class JdbcConnectionUtil {
     private static final String USERNAME = "test";
     private static final String PASSWORD = "test";
     private static final String URL = "jdbc:oracle:thin:@localhost:1521:xe";
     private static final String DRIVERCLASSNAME = "oracle.jdbc.OracleDriver";

     public static Connection getConnection(){
         Connection conn=null;
         try {
             Class.forName(DRIVERCLASSNAME);
             conn=DriverManager.getConnection(URL,USERNAME,PASSWORD);
         } catch (ClassNotFoundException e) {
             e.printStackTrace();
         } catch (SQLException e) {
             e.printStackTrace();
         }
         return conn;
     }
     public static void destroy(Connection conn){
         if(conn!=null){
             try {
                 conn.close();
                 conn=null;
             } catch (SQLException e) {
                 e.printStackTrace();
             }

         }
     }
     public static void main(String[] args) {
         System.out.println(JdbcConnectionUtil.getConnection());
     }
 }
 //数据库操作方法
 package com.util;

 import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;

 import com.util.JdbcConnectionUtil;
 import com.util.User;

 public class MethodDal {
     private Connection con;
     private PreparedStatement pste;
     private ResultSet rs;

     // 向用户表表中添加用户
     public int insertData(User user) {
         init();
         int i = -1;
         String sql = "insert into puser values(?,?)";
         try {
             pste = con.prepareStatement(sql);
             pste.setString(1, user.getUsername());
             pste.setString(2, user.getPassword());
             i = pste.executeUpdate();
         } catch (SQLException e) {
             e.printStackTrace();
         }
         close();
         return i;
     }

     // 查现有用户名
     public String selectName(String name) {
         init();
         String sql = "select * from puser p where p.pname=?";
         try {
             pste = con.prepareStatement(sql);
             pste.setString(1, name);
             rs = pste.executeQuery();
             while (rs.next()) {
                 String pname = rs.getString("pname");
                 if (pname != null) {
                     return pname;
                 }
             }
         } catch (SQLException e) {
             e.printStackTrace();
         }
         close();
         return "no";
     }

     // 根据姓名查密码
     public String selectPwd(String name) {
         init();
         String sql = "select * from puser p where p.pname=?";
         try {
             pste = con.prepareStatement(sql);
             pste.setString(1, name);
             rs = pste.executeQuery();
             while (rs.next()) {
                 String pwd = rs.getString("ppassword");
                 return pwd;
             }
         } catch (SQLException e) {
             e.printStackTrace();
         }
         close();
         return "wu";
     }

     // 添加电话本信息
     public int insertPhone(PhoneText p) {
         init();
         int i = -1;
         String sql = "insert into phonetext values(tablexulie.nextval,?,?,?,?)";
         try {
             long l = new Date().getTime();
             pste = con.prepareStatement(sql);
             pste.setString(1, p.getPuname().trim());
             pste.setString(2, p.getPhname().trim());
             pste.setString(3, p.getPhone().trim());
             pste.setDate(4, new java.sql.Date(l));

             i = pste.executeUpdate();
         } catch (SQLException e) {
             e.printStackTrace();
         }
         close();
         return i;
     }

     // 查询电话本信息
     public List<PhoneText> selectPhone(String text,String uname) {
         init();
         String sql = "select p.pid,p.phname,p.phone,p.ptime from phonetext p where p.phname like '%" + text
                 + "%' or p.phone like '%" + text + "%' and p.puname=? order by p.ptime desc";// '
         init();
         List<PhoneText> list = new ArrayList<PhoneText>();
         try {
             pste = con.prepareStatement(sql);
             pste.setString(1,uname);

             // pste.setString(1,text);

             rs = pste.executeQuery();
             while (rs.next()) {

                 PhoneText ph = new PhoneText();
                 ph.setPid(rs.getInt(1));
                 ph.setPhname(rs.getString(2));
                 ph.setPhone(rs.getString(3));
                 ph.setPtime(rs.getDate(4));
                 list.add(ph);
             }

         } catch (SQLException e) {
             e.printStackTrace();
         }
         close();
         return list;
     }

     // 查询所所属用户名的通讯录
     public List<PhoneText> selectAllphone(String username) {
         String sql = "select pid,phname,phone,ptime from phonetext where puname='"+username+"' order by ptime desc ";
         init();
         List<PhoneText> list = new ArrayList<PhoneText>();
         try {
             pste = con.prepareStatement(sql);
             rs = pste.executeQuery();
             while (rs.next()) {

                 PhoneText ph = new PhoneText();
                 ph.setPid(rs.getInt(1));
                 ph.setPhname(rs.getString(2));
                 ph.setPhone(rs.getString(3));
                 ph.setPtime(rs.getDate(4));
                 list.add(ph);
             }

         } catch (SQLException e) {
             e.printStackTrace();
         }
         close();
         return list;
     }

     // 更新表中的数据
     public int updateData(int id, String phname,String phone) {
         init();
         int i = -1;
         String sql = "update phonetext p set p.phname=?,p.phone=? where p.pid=?  ";
         try {
             pste = con.prepareStatement(sql);
             pste.setString(1, phname);
             pste.setString(2, phone);
             pste.setInt(3, id);
             i = pste.executeUpdate();
         } catch (SQLException e) {
             e.printStackTrace();
         }
         close();
         return i;
     }
     // 删除记录
     public int deletePhone(int id) {
         init();
         int i = -1;
         String sql = "delete phonetext  where pid=?";
         try {
             pste = con.prepareStatement(sql);
             pste.setInt(1, id);
             i = pste.executeUpdate();
         } catch (SQLException e) {
             e.printStackTrace();
         }
         close();
         return i;
     }
     // 查id信息
         public List<PhoneText> selectId(int id) {
             init();
             String sql = "select p.pid,p.phname,p.phone,p.ptime from phonetext p where p.pid=?";
             PhoneText ph = new PhoneText();
             List<PhoneText> list = new ArrayList<PhoneText>();
             try {
                 pste = con.prepareStatement(sql);
                 pste.setInt(1, id);
                 rs = pste.executeQuery();

                 while (rs.next()) {

                     ph.setPid(rs.getInt(1));
                     ph.setPhname(rs.getString(2));
                     ph.setPhone(rs.getString(3));
                     ph.setPtime(rs.getDate(4));
                     list.add(ph);
                 }

             } catch (SQLException e) {
                 e.printStackTrace();
             }
             close();
             return list;
         }

     // 初始化链接
     public void init() {
         con = JdbcConnectionUtil.getConnection();
     }

     public void close() {
         JdbcConnectionUtil.destroy(con);
     }
 }
 //通讯录
 package com.util;

 import java.sql.Date;

 public class PhoneText {

     private String puname;
     private String phname;
     private String phone;
     private Date ptime;

     public PhoneText(){}

     public PhoneText(int pid,String puname,String phname,String phone,Date ptime){
         super();
         this.pid=pid;
         this.puname=puname;
         this.phname=phname;
         this.phone=phone;
         this.ptime=ptime;

     }
     public int getPid() {
         return pid;
     }

     public void setPid(int pid) {
         this.pid = pid;
     }
     private int pid;

     public String getPuname() {
         return puname;
     }
     public void setPuname(String puname) {
         this.puname = puname;
     }
     public String getPhname() {
         return phname;
     }
     public void setPhname(String phname) {
         this.phname = phname;
     }
     public String getPhone() {
         return phone;
     }
     public void setPhone(String phone) {
         this.phone = phone;
     }
     public Date getPtime() {
         return ptime;
     }
     public void setPtime(Date ptime) {
         this.ptime = ptime;
     }
 }
 //用户类
 package com.util;

 public class User {
     private String username;
     private String password;

     public String getUsername() {
         return username;
     }

     public void setUsername(String username) {
         this.username = username;
     }

     public String getPassword() {
         return password;
     }

     public void setPassword(String password) {
         this.password = password;
     }
 }

com.servlet 各种servlet   处理 注册,登录,添加,删除,修改 的逻辑

 //注册逻辑
 package com.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.util.MethodDal;
 import com.util.User;

 /**
  * Servlet implementation class ZhueServlet
  */
 @WebServlet("/ZhueServlet")
 public class ZhueServlet extends HttpServlet {

     private static final long serialVersionUID = 1L;

     /**
      * @see HttpServlet#HttpServlet()
      */
     public ZhueServlet() {
         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");
         //调用方法判断传入的参数有没有空,都不为空才可以执行下去
         if(checkParam(username,password,password1)){
             if(password.equals(password1)){
                 MethodDal m=new MethodDal();
                 if(m.selectName(username).equals("no")){//调用方法根据用户名查询,如果返回no说明数据库没有此用户名,可以注册
                     User user=new User();//实例化用户类并添加信息
                     user.setUsername(username);
                     user.setPassword(password);
                     m.insertData(user);//将实例化的用户传到添加用户的方法
                     response.sendRedirect("message.jsp?code=1");
                 }else{
                     response.sendRedirect("message.jsp?code=4");
                 }
             }else{
                 response.sendRedirect("message.jsp?code=2");
             }
         }else{
             response.sendRedirect("message.jsp?code=3");
         }
     }

     /**
      * @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);
     }
     //判断传入的参数有没有空的方法,只要有空的就返回false
     public boolean checkParam(String... args){//这样传参数代表参数个数不确定,传几个都可以
         for(String s : args){
             if("".equals(s)||s==null){
                 return false;
             }
         }
         return true;
     }
 }
 //登录逻辑
 package com.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 javax.servlet.http.HttpSession;

 import com.util.MethodDal;
 import com.util.User;

 /**
  * Servlet implementation class DengluServlet
  */
 @WebServlet("/DengluServlet")
 public class DengluServlet extends HttpServlet {
     private static final long serialVersionUID = 1L;

     /**
      * @see HttpServlet#HttpServlet()
      */
     public DengluServlet() {
         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");

         MethodDal m=new MethodDal();
         //调用方法判断传入的参数有没有空,都不为空才可以执行下去
         if(checkParam(username,password)){
             if(!(m.selectName(username).equals("no"))){//调用方法根据用户名查询,如果返回不为no说明数据库有此用户
                 if((m.selectPwd(username)).equals(password)){//根据用户名查询密码,并对比输入的密码和数据库的密码
                     //用request.getSession()创建出客户端的session对象并存内容,用作主页的验证
                     HttpSession session=request.getSession();
                     session.setAttribute("name", username);

                     response.sendRedirect("indextest1.jsp");
                 }else{
                     response.sendRedirect("message.jsp?code=4");
                 }
             }else{
                 response.sendRedirect("message.jsp?code=5");
             }
         }else{
             response.sendRedirect("message.jsp?code=3");
         }
     }

     /**
      * @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);
     }
     //判断传入的参数有没有空的方法,只要有空的就返回false
     public boolean checkParam(String... args){//这样传参数代表参数个数不确定,传几个都可以
         for(String s : args){
             if("".equals(s)||s==null){
                 return false;
             }
         }
         return true;
     }
 }
 //添加联系人逻辑
 package com.servlet;

 import java.io.IOException;
 import java.util.Date;

 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.util.MethodDal;
 import com.util.PhoneText;

 /**
  * Servlet implementation class TianjiaServlet
  */
 @WebServlet("/TianjiaServlet")
 public class TianjiaServlet extends HttpServlet {
     private static final long serialVersionUID = 1L;

     /**
      * @see HttpServlet#HttpServlet()
      */
     public TianjiaServlet() {
         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 puname=request.getParameter("puname");
         String phname=request.getParameter("phname");
         String phone=request.getParameter("phone");

         MethodDal m=new MethodDal();
         //调用方法判断传入的参数有没有空,都不为空才可以执行下去
         if(checkParam(puname,phname,phone)){
             MethodDal me=new MethodDal();
             PhoneText ph=new PhoneText();
             ph.setPhname(phname);
             ph.setPhone(phone);
             ph.setPuname(puname);

             me.insertPhone(ph);
             response.sendRedirect("message.jsp?code=7");
         }else{
             response.sendRedirect("message.jsp?code=3");
         }
     }

     /**
      * @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);
     }
     //判断传入的参数有没有空的方法,只要有空的就返回false
         public boolean checkParam(String... args){//这样传参数代表参数个数不确定,传几个都可以
             for(String s : args){
                 if("".equals(s)||s==null){
                     return false;
                 }
             }
             return true;
         }
 }
 //修改联系人数据逻辑
 package com.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.util.MethodDal;

 /**
  * Servlet implementation class UpdateServlet
  */
 @WebServlet("/UpdateServlet")
 public class UpdateServlet extends HttpServlet {
     private static final long serialVersionUID = 1L;

     /**
      * @see HttpServlet#HttpServlet()
      */
     public UpdateServlet() {
         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 phname=request.getParameter("phname");
         String phone=request.getParameter("phone");
         String pid=request.getParameter("pid");
         //将获取的ID转换为int类型
         int intpid=Integer.parseInt(pid);

         MethodDal m=new MethodDal();
         //调用修改的方法
         int i=m.updateData(intpid, phname, phone);
         if(i==-1){
             response.sendRedirect("message.jsp?code=10");
         }else{
             response.sendRedirect("message.jsp?code=9");
         }
     }

     /**
      * @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);
     }
 }
 //删除联系人的逻辑
 package com.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.util.MethodDal;
 import com.util.User;

 /**
  * Servlet implementation class DeleteServlet
  */
 @WebServlet("/DeleteServlet")
 public class DeleteServlet extends HttpServlet {
     private static final long serialVersionUID = 1L;

     /**
      * @see HttpServlet#HttpServlet()
      */
     public DeleteServlet() {
         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 pid=request.getParameter("pid");
         int intpid=Integer.parseInt(pid);
         MethodDal m=new MethodDal();
         m.deletePhone(intpid);
         response.sendRedirect("message.jsp?code=8");
     }

     /**
      * @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);
     }
 }

然后是JSP前端页面

注册:

 <%@ page language="java" contentType="text/html; charset=utf-8"
     pageEncoding="utf-8"%>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 <title>Insert title here</title>
 </head>
 <body>

     <form action="ZhueServlet" method="post">
         用户名:<input type="text" name="username"><br>
         密码:<input type="password" name="password"><br>
         确认密码:<input type="password" name="password1"><br>
         真实姓名:<input type="text" name="realname"><br>
         <input type="submit" value="提交"><br>
     </form>
 </body>
 </html>

登录:

 <%@ page language="java" contentType="text/html; charset=utf-8"
     pageEncoding="utf-8"%>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 <title>Insert title here</title>
 </head>
 <body>

     <form action="DengluServlet" method="post">
         用户名:<input type="text" name="username"><br>
         密码:<input type="password" name="password"><br>
         <input type="submit" value="提交"><br>
         <a href='zhuce.jsp'>注册</a>
     </form>
 </body>
 </html>

主页,显示页:

 <%@ page language="java" contentType="text/html; charset=utf-8"
     pageEncoding="utf-8" import="com.util.*,java.util.*" %>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <html>
   <head>
     <title>通讯录</title>
     <%
     //首先要验证有没有登录,如果没有登录跳转到登录界面
     Object obj1=session.getAttribute("name");
     String uname=(String)obj1;
     if (obj1 == null) {
         response.sendRedirect("denglu.jsp");
     }
 %>
   </head>
   <body >
     <h1 style="font-size:38px;">欢迎</h1>
     <a  style="font-size:26px;" href="">查看联系人</a>
     <a  style="font-size:26px;" href="indextianjia.jsp">添加</a>

     <form action="indexselect.jsp" method="post" >
         <input type="text" name="select"/>
         <input type="submit" value="查询(跳转页面)">
     </form>
     <table border="1" align="center" width="60%">
         <tr>
             <th>客户编码</th>
             <th>姓名</th>
             <th>电话</th>
             <th>添加时间</th>
             <th>编辑</th>
             <th>删除</th>
         </tr>
 <%
         MethodDal m=new MethodDal();
         List<PhoneText> list=m.selectAllphone(uname);

         if(list!=null){

             for(PhoneText l: list ){
                 out.print("<tr>");
                 out.print("<td>"+l.getPid()+"</td>");
                 out.print("<td>"+l.getPhname()+"</td>");
                 out.print("<td>"+l.getPhone()+"</td>");
                 out.print("<td>"+l.getPtime()+"</td>");
                 out.print("<td><a href='indexupdate.jsp?pid="+l.getPid()+"'>编辑</td><br>");
                 out.print("<td><a href='DeleteServlet?pid="+l.getPid()+"'>删除</td><br>");
                 out.print("</tr>");
             }
         }
 %>
     </table>
   </body>
 </html>
 <script>
 </script>

添加联系人:

 <%@ page language="java" contentType="text/html; charset=utf-8"
     pageEncoding="utf-8" import="com.util.*,java.util.*" %>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <html>
   <head>
     <title>list.jsp</title>
   </head>

   <body >
     <h1 style="font-size:38px;">欢迎</h1>
     <a  style="font-size:26px;" href="indextest1.jsp">查看联系人</a>
     <a  style="font-size:26px;" href="indextianjia">添加</a>

      <form action="TianjiaServlet" method="post">
          用户名:<input type="text" name="puname" value="
          <% Object obj1=session.getAttribute("name");
         String uname=(String)obj1;
         out.print(uname);
         %>"><br>
         联系人姓名:<input type="text" name="phname"><br>
         联系人电话:<input type="text" name="phone"><br>
         <input type="submit" value="提交"><br>
     </form>
     </table>
   </body>
 </html>

查询页面:

 <%@ page language="java" contentType="text/html; charset=utf-8"
     pageEncoding="utf-8" import="com.util.*,java.util.*"%>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <html>
 <head>
 <title>list.jsp</title>
 </head>
 <body>
     <div id="select">
         <h1 style="font-size: 38px;">查询结果:</h1>
         <a style="font-size: 26px;"> <a style="font-size: 26px;"
             href="indextianjia.jsp">添加</a>

             <table border="1" align="center" width="60%">
                 <tr>
                     <th>客户编码</th>
                     <th>姓名</th>
                     <th>电话</th>
                     <th>添加时间</th>
                     <th>编辑</th>

                 </tr>
                 <%
                     //获取用户名用来筛选
                     Object obj1 = session.getAttribute("name");
                     String uname = (String) obj1;
                     String selecttext = request.getParameter("select");
                     MethodDal m = new MethodDal();
                     List<PhoneText> list = m.selectPhone(selecttext, uname);
                     if (list != null) {

                         for (PhoneText l : list) {
                             out.print("<tr>");
                             out.print("<td>" + l.getPid() + "</td>");
                             out.print("<td>" + l.getPhname() + "</td>");
                             out.print("<td>" + l.getPhone() + "</td>");
                             out.print("<td>" + l.getPtime() + "</td>");
                             out.print("<td> </td>" + "<br>");
                             out.print("</tr>");
                         }
                     }
                 %>
             </table>
     </div>
 </body>
 </html>

第二版本:

model包,用户类,通讯录类

 package com.hanqi.maya.model;

 import java.io.Serializable;
 import java.util.Date;

 /**
  * 联系人实体类
   */
 public class ContactBook implements Serializable {

     private static final long serialVersionUID = 1L;
     /**
      * 主键
      */
     private Integer id;
     /**
      * 联系人姓名
      */
     private String name;
     /**
      * 电话号码
      */
     private String tel;
     /**
      * 性别
      */
     private String sex;
     /**
      * 添加时间
      */
     private Date addtime;
     /**
      * 分组信息
      */
     private String tcgroup;
     /**
      * 所属用户
      */
     private String username;

     public ContactBook() {
         super();
     }

     public ContactBook(String name, String tel, String sex, String tcgroup) {
         super();
         this.name = name;
         this.tel = tel;
         this.sex = sex;
         this.tcgroup = tcgroup;
     }

     public ContactBook(Integer id, String name, String tel, String sex, Date addtime, String tcgroup, String username) {
         super();
         this.id = id;
         this.name = name;
         this.tel = tel;
         this.sex = sex;
         this.addtime = addtime;
         this.tcgroup = tcgroup;
         this.username = username;
     }

     public Integer getId() {
         return id;
     }

     public void setId(Integer id) {
         this.id = id;
     }

     public String getName() {
         return name;
     }

     public void setName(String name) {
         this.name = name;
     }

     public String getTel() {
         return tel;
     }

     public void setTel(String tel) {
         this.tel = tel;
     }

     public String getSex() {
         return sex;
     }

     public void setSex(String sex) {
         this.sex = sex;
     }

     public Date getAddtime() {
         return addtime;
     }

     public void setAddtime(Date addtime) {
         this.addtime = addtime;
     }

     public String getTcgroup() {
         return tcgroup;
     }

     public void setTcgroup(String tcgroup) {
         this.tcgroup = tcgroup;
     }

     public String getUsername() {
         return username;
     }

     public void setUsername(String username) {
         this.username = username;
     }

     @Override
     public String toString() {
         return "ContactBook [id=" + id + ", name=" + name + ", tel=" + tel + ", sex=" + sex + ", addtime=" + addtime
                 + ", tcgroup=" + tcgroup + ", username=" + username + "]";
     }

 }
 package com.hanqi.maya.model;

 import java.io.Serializable;
 import java.util.Date;

 /**
  * 用户实体类
   */
 public class ContactUser implements Serializable {

     private static final long serialVersionUID = 1L;
     /**
      * 用户名
      */
     private String username;
     /**
      * 用户密码
      */
     private String password;
     /**
      * 姓名
      */
     private String realname;
     /**
      * 注册时间
      */
     private Date createtime;

     public ContactUser() {
     }

     public ContactUser(String username, String password, String realname, Date createtime) {
         this.username = username;
         this.password = password;
         this.realname = realname;
         this.createtime = createtime;
     }

     public ContactUser(String username, String password, String realname) {
         this.username = username;
         this.password = password;
         this.realname = realname;
     }

     public String getUsername() {
         return username;
     }

     public void setUsername(String username) {
         this.username = username;
     }

     public String getPassword() {
         return password;
     }

     public void setPassword(String password) {
         this.password = password;
     }

     public String getRealname() {
         return realname;
     }

     public void setRealname(String realname) {
         this.realname = realname;
     }

     public Date getCreatetime() {
         return createtime;
     }

     public void setCreatetime(Date createtime) {
         this.createtime = createtime;
     }

 }

util包  包含数据库设置,日期转换工具,数据库操作

 package com.hanqi.maya.util;

 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;

 /**
  * 数据库驱动连接类
  * @author ZBK
  */
 public class DBHelper {
     /**
      * 数据库用户名
      */
     public static final String USERNAME = "test";
     /**
      * 数据库密码
      */
     public static final String PASSWORD = "test";
     /**
      * 数据库驱动类
      */
     public static final String DRIVER = "oracle.jdbc.OracleDriver";
     /**
      * 数据库地址URL
      */
     public static final String URL = "jdbc:oracle:thin:@localhost:1521:xe";

     /**
      * 获取数据库连接
      * @return
      */
     public static Connection getConnection() {
         Connection conn = null;
         try {
             Class.forName(DRIVER);
             conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
         } catch (ClassNotFoundException e) {
             e.printStackTrace();
         } catch (SQLException e) {
             e.printStackTrace();
         }
         return conn;
     }

     /**
      * 释放资源
      * @param conn 数据库连接对象
      * @param sm Statement对象
      * @param rs ResultSet结果集对象
      */
     public static void destroy(Connection conn, Statement sm, ResultSet rs) {
         if (conn != null) {
             try {
                 conn.close();
             } catch (SQLException e) {
                 e.printStackTrace();
             }
             conn = null;
         }
         if (sm != null) {
             try {
                 sm.close();
             } catch (SQLException e) {
                 e.printStackTrace();
             }
             sm = null;
         }
         if (rs != null) {
             try {
                 rs.close();
             } catch (SQLException e) {
                 e.printStackTrace();
             }
             rs = null;
         }
     }

     /**
      * 验证前台传入的参数是否为空
      * @param args
      * @return
      */
     public static boolean checkParam(String... args) {
         for (String s : args) {
             if (s == null || s.trim().length() < 1) {
                 return false;
             }
         }
         return true;
     }
 }
 package com.hanqi.maya.util;

 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Date;

 /**
  * 日期转换器
   */
 public class DateConvertor {

     private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

     /**
      * 将字符串转换成Date类型
      * @param str
      * @return
      */
     public static Date putString2Date(String str) {
         Date date = null;
         if(str.trim().length()<=0) {
             return null;
         }
         try {
             date = new Date(sdf.parse(str).getTime());
         } catch (ParseException e) {
             e.printStackTrace();
         }
         return date;
     }

     /**
      * 将日期类型转换成字符串
      * @param dd
      * @return
      */
     public static String putDate2String(Date dd) {
         return sdf.format(dd);
     }
 }

servelt处理逻辑

登录逻辑:

 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 LoginServlet
  */
 @WebServlet("/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 {
         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");

         DataBaseMethodDal dbmd = new DataBaseMethodDal();

         if (DBHelper.checkParam(username, password)) {
             ContactUser user = dbmd.selectUser(username, password);
             if (user != null) {
                 request.getSession().setAttribute("currentUser", user);
                 r(response, "ShowContactInfoServlet", -1);
             } else {
                 r(response, "message.jsp", 5);
             }
         } 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.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();
        }
    }

}
 package com.hanqi.maya.util;

 import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.List;

 import com.hanqi.maya.model.ContactBook;
 import com.hanqi.maya.model.ContactUser;

 /**
  * 调用数据库连接进行数据操作的类
  *
   */
 public class DataBaseMethodDal {
     private Connection conn;
     private PreparedStatement ps;
     private ResultSet rs;

     /**
      * 初始化数据库链接
      */
     public void init(String sql) {
         conn = DBHelper.getConnection();
         try {
             ps = conn.prepareStatement(sql);
         } catch (SQLException e) {
             e.printStackTrace();
         }
     }

     /**
      * 注册用户信息
      *
      * @param cu
      * @return
      */
     public int insertUser(ContactUser cu) {
         String sql = "insert into tc_user values(?,?,?,sysdate)";

         init(sql);
         int a = -1;
         try {
             ps.setString(1, cu.getUsername());
             ps.setString(2, cu.getPassword());
             ps.setString(3, cu.getRealname());
             a = ps.executeUpdate();
         } catch (SQLException e) {
             e.printStackTrace();
         }
         return a;
     }

     public ContactUser selectUser(String username, String password) {
         String sql = "select * from tc_user t where t.username=? and t.password=?";
         init(sql);
         ContactUser user = null;
         try {
             ps.setString(1, username);
             ps.setString(2, password);
             rs = ps.executeQuery();
             while (rs.next()) {
                 user = new ContactUser(rs.getString("username"), rs.getString("password"), rs.getString("realname"),
                         rs.getDate("createtime"));
             }
         } catch (SQLException e) {
             e.printStackTrace();
         }
         return user;
     }

     public List<ContactBook> selectAllContactInfo(ContactBook book, String username) {
         String sqlplus = "";
         if (book != null) {
             if (book.getName().trim().length() > 0) {
                 sqlplus += " and t.cname like '%" + book.getName() + "%' ";
             }
             if (book.getTel().trim().length() > 0) {
                 sqlplus += " and t.tel like '%" + book.getTel() + "%' ";
             }
             if (book.getSex().trim().length() > 0) {
                 sqlplus += " and t.sex = '" + book.getSex() + "' ";
             }
             if (book.getTcgroup().trim().length() > 0) {
                 sqlplus += " and t.tcgroup = '" + book.getTcgroup() + "' ";
             }
         }
         String sql = "select * from TC_CONTACT t where t.username = ?" + sqlplus;
         System.out.println(sql);
         init(sql);
         List<ContactBook> cbList = null;
         try {
             ps.setString(1, username);
             rs = ps.executeQuery();
             if (rs != null) {
                 cbList = new ArrayList<ContactBook>();
                 while (rs.next()) {
                     ContactBook cb = new ContactBook(rs.getInt("id"), rs.getString("cname"), rs.getString("tel"),
                             rs.getString("sex"), rs.getTimestamp("addtime"), rs.getString("tcgroup"),
                             rs.getString("username"));
                     cbList.add(cb);
                 }
             }
         } catch (SQLException e) {
             e.printStackTrace();
         }
         return cbList;
     }

     public int insertBook(ContactBook cb, String username) {
         String sql = "insert into TC_CONTACT values(test.nextval, ?,?,?,sysdate,?,?)";
         init(sql);
         int a = -1;
         try {
             ps.setString(1, cb.getName());
             ps.setString(2, cb.getTel());
             ps.setString(3, cb.getSex());
             ps.setString(4, cb.getTcgroup());
             ps.setString(5, username);
             a = ps.executeUpdate();
         } catch (SQLException e) {
             e.printStackTrace();
         }
         return a;
     }

     public int delContactBook(String ids) {
         String sql = "delete TC_CONTACT t where t.id in (" + ids + ")";
         init(sql);
         int a = -1;
         try {
             a = ps.executeUpdate();
         } catch (SQLException e) {
             e.printStackTrace();
         }
         return a;
     }

     public int updateBook(ContactBook cb, String username) {
         String sql = "update TC_CONTACT t set t.cname=?, t.sex=?, t.tel=?, t.tcgroup=? "
                 + "where t.username=? and t.id=?";
         init(sql);
         int a = -1;
         try {
             ps.setString(1, cb.getName());
             ps.setString(2, cb.getSex());
             ps.setString(3, cb.getTel());
             ps.setString(4, cb.getTcgroup());
             ps.setString(5, username);
             ps.setInt(6, cb.getId());
             a = ps.executeUpdate();
         } catch (SQLException e) {
             e.printStackTrace();
         }
         return a;
     }
 }

删除

 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.util.DataBaseMethodDal;

 /**
  * Servlet implementation class DelContactBookServlet
  */
 @WebServlet("/DelContactBookServlet")
 public class DelContactBookServlet extends HttpServlet {
     private static final long serialVersionUID = 1L;

     /**
      * @see HttpServlet#HttpServlet()
      */
     public DelContactBookServlet() {
         super();
         // TODO Auto-generated constructor stub
     }

     /**
      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
      *      response)
      */
     protected void doGet(HttpServletRequest request, HttpServletResponse response)
             throws ServletException, IOException {
         String ids = request.getParameter("ids");
         if (ids != null && ids.trim().length() > 0) {
             DataBaseMethodDal dbmd = new DataBaseMethodDal();
             int a = dbmd.delContactBook(ids);
             if (a > 0) {
                 response.sendRedirect("ShowContactInfoServlet");
             } else {
                 response.sendRedirect("message.jsp?code=6");
             }
         } else {
             response.sendRedirect("message.jsp?code=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);
     }

 }

JSP页面:

注册页面:

-

 <%@ page language="java" contentType="text/html; charset=UTF-8"
     pageEncoding="UTF-8"%>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <title>Insert title here</title>
 </head>
 <body>
     <form action="RegisterServlet" method="post">
         <table>
             <tr>
                 <td>用户名: </td>
                 <td><input type="text" name="username" /></td>
             </tr>
             <tr>
                 <td>密码: </td>
                 <td><input type="text" name="password" /></td>
             </tr>
             <tr>
                 <td>确认密码: </td>
                 <td><input type="text" name="password1" /></td>
             </tr>
             <tr>
                 <td>姓名: </td>
                 <td><input type="text" name="realname" /></td>
             </tr>
             <tr>
                 <td><input type="submit" value="注册" /></td>
                 <td><a href="login.jsp">返回登陆</a></td>
             </tr>
         </table>
     </form>
 </body>
 </html>

登录页面:

 <%@ page language="java" contentType="text/html; charset=UTF-8"
     pageEncoding="UTF-8"%>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <title>Insert title here</title>
 </head>
 <body>
     <form action="LoginServlet" method="post">
         <table>
             <tr>
                 <td>用户名: </td>
                 <td><input type="text" name="username" /></td>
             </tr>
             <tr>
                 <td>密码: </td>
                 <td><input type="text" name="password" /></td>
             </tr>
             <tr>
                 <td><input type="submit" value="登录" /></td>
                 <td><a href="register.jsp">前往注册</a></td>
             </tr>
         </table>
     </form>
 </body>
 </html>

主页:

主页有两个from表单,一个用来进行添加或者修改,另一个用来查询

进行修改和查询的表单有一个隐藏域,name是id,添加的时候id是空的,后台判断id是空的就进行添加,点击修改会触发点击事件  loadForm(id, cname, tel, sex, tcgroup)  然后通过  var f = document.getElementById("addOrUpdateForm");   f.id.value=id;   将信息获取到表单中,然后进行修改

在查询的表单中,有一个隐藏域,如果点击查询,隐藏域会被提交,后台就能接收到它的参数说明是查询操作,如果不能,说明没有点击查询,是第一次登录,后台接收到参数之后,进行判断,如果是查询,

 <%@ page language="java" contentType="text/html; charset=utf-8"
     pageEncoding="utf-8"%>
 <%@ page import="com.hanqi.maya.model.ContactUser"%>
 <%@ page import="com.hanqi.maya.model.ContactBook"%>
 <%@ page import="java.util.ArrayList"%>
 <%@ page import="com.hanqi.maya.util.DateConvertor"%>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <html>
 <head>
     <%
         ArrayList<ContactBook> list = null;
         ContactUser cu = (ContactUser) session.getAttribute("currentUser");
         String realname = "没有登录";
         if (cu == null) {
             response.sendRedirect("message.jsp?code=0");
         } else {
             realname = cu.getRealname();
             list = (ArrayList<ContactBook>) session.getAttribute("cbList");
         }
     %>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 <title>Insert title here</title>
 </head>
 <body>
     欢迎, [<%=realname%>]
     <hr>
     <form id="addOrUpdateForm" action="InsertContactBookServlet" method="post">
         <input type="hidden" name="id" />
         <table>
             <tr>
                 <td>联系人名称:</td>
                 <td><input type="text" name="cname" /></td>
             </tr>
             <tr>
                 <td>电话:</td>
                 <td><input type="text" name="tel" /></td>
             </tr>
             <tr>
                 <td>性别:</td>
                 <td><select name="sex">
                         <option value="">未选择</option>
                         <option value="男">男</option>
                         <option value="女">女</option>
                         <option value="其他">其他</option>
                 </select></td>
             </tr>
             <tr>
                 <td>分组:</td>
                 <td><select name="tcgroup">
                         <option value="">未选择</option>
                         <option value="同事">同事</option>
                         <option value="朋友">朋友</option>
                         <option value="同学">同学</option>
                         <option value="家人">家人</option>
                 </select></td>
             </tr>
             <tr>
                 <td><input type="submit" value="保存"></td>
                 <td><input type="button" value="重置" /></td>
             </tr>
         </table>
     </form>
     <hr>
     <h2>查询联系人</h2>
     <form action="ShowContactInfoServlet" method="post">
         <input type="hidden" name="isSearch" value="do" />
         联系人名称:<input type="text" name="cname" />
         电话:<input type="text" name="tel" />
         性别:
         <select name="sex">
             <option value="">未选择</option>
             <option value="男">男</option>
             <option value="女">女</option>
             <option value="其他">其他</option>
         </select>
         分组: <select name="tcgroup">
             <option value="">未选择</option>
             <option value="同事">同事</option>
             <option value="朋友">朋友</option>
             <option value="同学">同学</option>
             <option value="家人">家人</option>
         </select>
         <input type="submit" value="查询" />
         <input type="button" id="btn_delMultiple" value="删除选中记录" onclick="confirmMultiDel()" />
     </form>
     <hr>
     <%
         if (list != null && list.size() > 0) {
             out.print("<table style='text-align:center;' width='70%' cellpadding='0' cellspacing='0' border='1'>");
             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>");
             for (ContactBook c : list) {
                 out.print("<tr>");
                 out.print("<td>" + c.getName() + "</td><td>" + c.getTel() + "</td><td>" + c.getSex() + "</td><td>"
                         + c.getTcgroup() + "</td><td>" + DateConvertor.putDate2String(c.getAddtime())
                         + "</td><td><a href='DelContactBookServlet?id="+c.getId()+"' onclick='return confirmDel()'>删除</a>&nbsp;<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>");
                 out.print("</tr>");
             }
             out.print("</table>");
         } else {
             out.print("没有任何记录 !");
         }
     %>

 <script type="text/javascript">
 function confirmDel() {
     var r = confirm("确定删除吗?");
     return r;
 }

 function getMultiDel() {
     var c = document.getElementById("leader");
     var r = c.checked;
     var checks = document.getElementsByClassName("select");
     for(var i=0;i<checks.length;i++) {
         checks[i].checked = r;
     }
 }

 function loadForm(id, cname, tel, sex, tcgroup) {
     var f = document.getElementById("addOrUpdateForm");
     f.id.value=id;
     f.cname.value = cname;
     f.tel.value = tel;
     f.sex.value = sex;
     f.tcgroup.value = tcgroup;
 }

 function confirmMultiDel() {
     var checks = document.getElementsByClassName("select");
     var arrayObjs = [];
     for(var j=0;j<checks.length;j++) {
         if(checks[j].checked) {
             arrayObjs.push(checks[j].value);
         }
     }

     var r1 = confirm("选中了"+arrayObjs.length+"条数据, 确定要删除吗 ?");
     if(r1) {
         window.location.href="DelContactBookServlet?ids="+arrayObjs;
     }
 }

 </script>
 </body>
 </html>

提示页:

 <%@ page language="java" contentType="text/html; charset=UTF-8"
     pageEncoding="UTF-8"%>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <title>Insert title here</title>
 </head>
 <body>
     <%
         String code = request.getParameter("code");
         if ("0".equals(code)) {
             out.print("<h1>请先登录 !</h1>");
         }
         if ("1".equals(code)) {
             out.print("<h1>输入正确的参数 !</h1>");
         }
         if ("2".equals(code)) {
             out.print("<h1>两次输入的密码不一致 !</h1>");
         }
         if ("3".equals(code)) {
             out.print("<h1>注册成功 !</h1>");
         }
         if ("4".equals(code)) {
             out.print("<h1>后台出现异常 !</h1>");
         }
         if ("5".equals(code)) {
             out.print("<h1>用户不存在或者密码错误 !</h1>");
         }
         if ("6".equals(code)) {
             out.print("<h1>删除联系人信息失败 !</h1>");
         }
     %>
     <hr>
     <a href="login.jsp">登录</a>
     <a href="register.jsp">注册</a>
     <a href="ShowContactInfoServlet">前往主页</a>
 </body>
 </html>

总结:

1.可以在一个页面设置多个表单进行不同的操作

2.可以设置点击事件,然后使用   var f = document.getElementById("addOrUpdateForm");   f.id.value=id;   将信息获取到指定id 的表单或者表格中

3.

JavaWeb项目之电话本,两个版本,以及总结反思的更多相关文章

  1. idea maven javaweb项目迁移时的maven和版本报错问题解决(可解决同类错误)

    项目中代码红线报版本不支持xx语法,只需要将java版本设置为当前机器使用的java版本即可 这里我使用的是idea自带的maven,如果是自己安装的maven需要在 home directory 处 ...

  2. ubuntu16.04环境下在docker上部署javaweb项目简单案例

    因为一些原因,接触到了docker,经过一番研究,总算是有了一些自己的看法,有什么不对的地方,希望多多指教. 废话不多说,首先我这里使用的虚拟机安装的是ubuntu16.04版本,其他版本应该也可以. ...

  3. idea 社区版本创建javaweb项目 使用jetty

    idea社区版本 创建javaweb项目后使用jetty启动 <dependencies> <dependency> <groupId>javax.servlet& ...

  4. jdbc电话本项目

    整体思路:在登陆之后才能查看自己的电话本,电话本中包含用户名,联系人名字,电话,性别,分类: 1.登陆注册页面--数据库User表,注册登陆使用 2.电话本的前段显示,用表格和表单, 3.创建存取的电 ...

  5. 微信电话本可免费拨打网络电话 通话一分钟约300K流量

    微信电话本新版本于昨日晚间发布,这是一款智能通讯增强软件,通话双方都下载此APP并开通免费通话功能就能使用微信电话本拨打免费网络电话,在对方无法接通情况下还能将音频转向语音信箱,微信电话本目前支持An ...

  6. C语言实现电话本 动态开辟 信息存储于文件

    下面是我用C写的一个电话本小项目,实现的功能有:添加 删除 修改 查找 排序 清空 显示,功能还是比较全的,内存也是动态开辟的.能存储于本地,能从本地读出并显示 头文件部分代码,contact.h: ...

  7. JavaEE——Intellij Idea 创建JavaWeb项目

    原文:JavaEE--Intellij Idea 创建JavaWeb项目 折腾Tomcat折腾了两个晚上,第一个晚上怎么都进不了Tomcat的首页,第二个晚上进去了,但是新建的Web项目,在浏览器中运 ...

  8. 用Eclipse 创建一个 简单的 Maven JavaWeb 项目

    使用Maven 创建一个简单的 javaWeb 项目: 本篇属于 创建 JavaWeb 项目的第三篇: 建议阅读本篇之前 阅读 用 Eclipse 创建一个简单的web项目  ;本篇是这这篇文章的基础 ...

  9. eclipse弃坑记第一篇之在idea上配置Tomcat环境并创建Javaweb项目的详细步骤原创

    IntelliJ IDEA是一款功能强大的开发工具,在代码自动提示.重构.J2EE支持.各类版本工具(如git.svn.github).maven等方面都有很好的应用. IntelliJ IDEA有免 ...

随机推荐

  1. jenkins~管道Pipeline的使用,再见jenkinsUI

    Pipeline在Jenkins里的作用 最近一直在使用jenkins进行自动化部署的工作,开始觉得很爽,省去了很多重复的工作,它帮助我自动拉服务器的代码,自动还原包包,自动编译项目,自动发布项目,自 ...

  2. 容易被忽视的后端服务 chunked 性能问题

    容易被忽视的后端服务 chunked 性能问题 标签(空格分隔): springboot springmvc chunked 背景 spring boot 创建的默认 spring mvc 项目 集成 ...

  3. Server SQL2008对文件的基础操作(1)

    1.一个文件的基本框架为:文件名.文件地址.文件大小.文件最大的大小.文件的增量(Filegrowth). 2.文件有mdf.ndf.ldf 三种文件的区别. 3.文件组可以进行文件的管理 FileG ...

  4. spring bean的创建过程

    spring的核心容器包括:core.beans.context.express language四个模块.所以对于一个简单的spring工程,最基本的就是依赖以下三个jar包即可: <depe ...

  5. js中对节点属性的操作和对节点的操作

    常用的节点属性操作方法 1.setAttribute(name,value):给某个节点添加一个属性 2.getAttribute(name):获取某个节点属性的值. 3.removeAttribut ...

  6. 推荐一个非常牛逼的并发httpful的库(php)

    MultiHttp :结合了libcurl多线程.httpful方式的PHP库 This is high performance curl wrapper written in pure PHP. I ...

  7. 负载均衡之 nginx

    什么是负载均衡负载均衡(Load Balance)是分布式系统架构设计中必须考虑的因素之一,它通常是指,将请求/数据[均匀]分摊到多个操作单元上执行,负载均衡的关键在于[均匀].在使用nginx负载均 ...

  8. apply/call/bind的区别与用法

    apply 方法/call 方法 obj.call(thisObj, arg1, arg2, ...);obj.apply(thisObj, [arg1, arg2, ...]); 两者作用一致,都是 ...

  9. 前端开发者常用的9个JavaScript图表库

    当前,数据可视化已经成为数据科学领域非常重要的一部分.不同网络系统中产生的数据,都需要经过适当的可视化处理,以便更好的呈现给用户读取和分析. 对任何一个组织来说,如果能够充分的获取数据.可视化数据和分 ...

  10. QScintilla 编译

    直接上英文了,不翻译了.看不懂的可以留言哈. ———————————————————————————————————————— Installation As supplied QScintilla ...