자바 웹 개인 주소록 시스템 디자인

31998 단어 자바web통신 록
본 논문 의 사례 는 자바 웹 개인 주소록 시스템 의 구체 적 인 코드 를 공유 하여 여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.
지금부터 캡 처:

다음 코드 붙 여 넣 기:
우선 디 렉 터 리 구조:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <title>add.jsp</title>
 <script type="text/javascript" src="<c:url value='/jquery/contactor.js'/>"></script> 
 
  <style type="text/css">
 body{
  text-align: center;
  font-size:38px;
 }
   *{
   font-family:    ;
   font-size:18px;
   color:#F0F";
 }
 h1{
  text-align: center;
  font-size:38px;
 }
 </style>
  
 </head>
 
 <body >
 
 <h1 style="font-size:38px;">       </h1>
 
 <form action="<c:url value='/ContactorServlet?userId=${userId } '/>" method="post" onsubmit="return checkForm()">
  <input type="hidden" name="method" value="4">
  <table border="1" width="50%" align="center">
   <tr>
    <td>  </td>
    <td>
     <input type="text" name="name" id="name" onfocus="nameFocus()" onblur="nameBlur()"/>
     <div id="nameId"></div>
    </td>
   </tr>
   <tr>
    <td>  </td>
    <td>
     <input type="text" name="tel" id="tel" onfocus="telFocus()" onblur="telBlur()"/>
     <div id="telId"></div>
    </td>
   </tr>
   <tr>
    <td>  </td>
    <td>
     <input type="text" name="address" id="address" />
    </td>
   </tr>  
    <tr>
    <td>  </td>
    <td>
     <input type="text" name="zipcode" />
    </td>
   </tr>
  
     
  </table>
  <input style="margin-right:200px;" type="submit" value="  "/> 
    
 </form>
 </body>
</html>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <title>edit.jsp</title>

 <script type="text/javascript" src="<c:url value='/jquery/contactor.js'/>"></script> 
 
 
  <style type="text/css">
 body{
  text-align: center;
  font-size:38px;
 }
   *{
   font-family:    ;
   font-size:18px;
   color:#F0F";
 }
 h1{
  text-align: center;
  font-size:38px;
 }
 </style>
  
 </head>
 
 <body > 
 <h1 style="font-size:38px;">       </h1>
 <form action="<c:url value='/ContactorServlet'/>" method="post" onsubmit="return checkForm()">
  <input type="hidden" name="method" value="6">
  <input type="hidden" name="id" value="${contactor.id }">
  <table border="1" width="50%" align="center">
   <tr>
    <td>  </td>
    <td>
     <input type="text" name="name" value="${contactor.name }" id="name" onfocus="nameFocus()" onblur="nameBlur()"/>
     <div id="nameId"></div>
    </td>
   </tr>
   <tr>
    <td>  </td>
    <td>
     <input type="text" name="tel" value="${contactor.tel }" id="tel" onfocus="telFocus()" onblur="telBlur()" />
     <div id="telId"></div>
    </td>
   </tr>
   <tr>
    <td>  </td>
    <td>
     <input type="text" id="address" name="address" value="${contactor.address }" />
    </td>
   </tr>  
    <tr>
    <td>  </td>
    <td>
     <input type="text" name="zipcode" value="${contactor.zipcode }" />
    </td>
   </tr> 
   
  </table>
  <input style="margin-right:200px;" type="submit" value="  "/>
     
   
 </form>
 </body>
</html>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <title>home.jsp</title>
 </head>
 
<frameset rows="100%,*">
 
 <frame name="body" src="<c:url value='/jsp/top.jsp'/>" />
</frameset>
</html>


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <title>list.jsp</title> 
  <style type="text/css">
 body{
  text-align: center;
  font-size:38px;
 }
   *{
   font-family:    ;
   font-size:18px;
   color:#F0F";
 }
 h1{
  text-align: center;
  font-size:38px;
 }
 a:link{
  color: #999;
  text-decoration: none;
 }
 a:VISITED {
 text-decoration: none;
 color: #c60;
}
 a:HOVER {
 text-decoration: none;
 color: #00F;
}
 </style>
  
 </head>
 
 <body >
 <h1 style="font-size:38px;">          </h1>
 <a style="font-size:26px;" href="<c:url value='/ContactorServlet?method=3&userId=${userId }'/>">     </a>
 <a style="font-size:26px;" href="<c:url value='/jsp/add.jsp'/>">     </a>
 <a  style="font-size:26px;" href="<c:url value='/jsp/top.jsp'/>">    </a>
 
 <table border="1" align="center" width="60%">
  <tr>
   <th>  </th>
   <th>  </th>
   <th>  </th>
   <th>  </th>
   <th>  1</th>
   <th>  2</th>
   
  </tr>
  <c:forEach items="${contactorList }" var="c">
  <tr>
   <td>${c.name }</td>
   <td>${c.tel }</td>
   <td>${c.address }</td>
   <td>${c.zipcode }</td>
   
   <td><a href="<c:url value='/ContactorServlet?method=5&id=${c.id }'/>">  </a>
   </td>
   <td>
    <a href="<c:url value='/ContactorServlet?method=7&id=${c.id }'/>" >  </a>
   </td>
  </tr>
  </c:forEach>
 </table>
 </body>
</html>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <title>top.jsp</title>
  <style type="text/css">
 body{
  text-align: center;
  font-size:38px;
 }
   *{
   font-family:    ;
   font-size:18px;
   color:#F0F";
 }
 h1{
  text-align: center;
  font-size:38px;
 }
 </style>
 
 </head>
 
 <body >
 <h1 style="font-size:38px;">     </h1>
  
 <form action="<c:url value='/ContactorServlet'/>" method="post">
   <input type="hidden" name="method" value="1"> 
     :<input type="text" name="username" /><br/>
    &nbsp;  :<input type="password" name="password" /><br/>
  <input type="submit" value="  ">
 </form>
 </body>
</html>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <title>top.jsp</title>
  <style type="text/css">
 body{
  text-align: center;
  font-size:38px;
 }
   *{
   font-family:    ;
   font-size:18px;
   color:#F0F";
 }
 h1{
  text-align: center;
  font-size:38px;
 }
 </style>
 </head>
 
 <body >
 <h1 style="font-size:38px;">     </h1>
  
 <form action="<c:url value='/ContactorServlet'/>" method="post">
  <input type="hidden" name="method" value="2"> 
     :<input type="text" name="username"/><br/>
    &nbsp;  :<input type="password" name="password"/><br/>
  <input type="submit" value="  ">
 <a href="<c:url value='/jsp/register.jsp'/>" target="body" >  </a>
 </form>
 </body>
</html>
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

contactor.js

//            ,        
function nameFocus() {
 var nameId = document.getElementById("nameId");
 nameId.innerHTML = "         ";
}
//            ,        
function nameBlur() {
 var name = document.getElementById("name");
 var nameId = document.getElementById("nameId");
 var reg= /^[\u4e00-\u9fa5]+$/;
 if(name.value == ""){
  nameId.innerHTML = "<font color='red'>        </font>";
  return false;
 }
 if(reg.test(name.value) == false){
  nameId.innerHTML = "<font color='red'>              </font>";
  return false;
 }
 nameId.innerHTML = "<font color='green'>        </font>";
 return true;
 
}
//            ,        
function telFocus() {
 var telId = document.getElementById("telId");
 telId.innerHTML = "       ";
}

//            ,        
function telBlur() {
 var tel = document.getElementById("tel");
 var telId = document.getElementById("telId");
 var reg= /^(13|15|17|18)\d{9}$/;
 if(tel.value == ""){
  telId.innerHTML = "<font color='red'>        </font>";
  return false;
 }
 if(reg.test(tel.value) == false){
  telId.innerHTML = "<font color='red'>         </font>";
  return false;
 }
 telId.innerHTML = "<font color='green'>        </font>";
 return true;
 
}

//               
function checkForm() {
 var flagname = nameBlur();
 var flagtel = telBlur();
 if(flagname == true && flagtel == true ){
  return true;
 }else{
  alert("  ,         !")
  return false;
 }
 
 
}

package cn.edu.aynu.rjxy.bean;
/**
 *       
 * @author Administrator
 *
 */
public class Contactor {
 private int id;
 private int userId;
 private String name;
 private String tel;
 private String address;
 private String zipcode;
 
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public int getUserId() {
  return userId;
 }
 public void setUserId(int userId) {
  this.userId = userId;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getTel() {
  return tel;
 }
 public void setTel(String tel) {
  this.tel = tel;
 }
 public String getAddress() {
  return address;
 }
 public void setAddress(String address) {
  this.address = address;
 }
 public String getZipcode() {
  return zipcode;
 }
 public void setZipcode(String zipcode) {
  this.zipcode = zipcode;
 }
 
 
}

package cn.edu.aynu.rjxy.bean;
/**
 *      
 * @author Administrator
 *
 */
public class User {
 private int id;
 private String username;
 private String password;
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getUsername() {
  return username;
 }
 public void setUsername(String username) {
  this.username = username;
 }
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 
 
}

package cn.edu.aynu.rjxy.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import cn.edu.aynu.rjxy.bean.Contactor;
import cn.edu.aynu.rjxy.utils.JDBCUtils;

/**
 *  linkman      
 * @author Administrator
 *
 */
public class ContactorDao {
 private QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
 /**
  *      
  * @param c
  * @throws SQLException 
  */
 public void insert(Contactor c,int userId) throws SQLException{
  String sql = "insert into linkman(name,tel,address,zipcode,userId) values(?,?,?,?,?)";
  qr.update(sql, c.getName(),c.getTel(),c.getAddress(),c.getZipcode(),userId);
 }
 /**
  *      
  * @param cid
  * @throws SQLException
  */
 public void delete(int id) throws SQLException{
  String sql = "delete from linkman where id=?";
  qr.update(sql, id);
 }
 /**
  *      
  * @param c
  * @throws SQLException
  */
 public void update(Contactor c,int userId) throws SQLException{
  String sql = "update linkman set name=?,tel=?,address=?,zipcode=?,userId=? where id=?";
  qr.update(sql, c.getName(),c.getTel(),c.getAddress(),c.getZipcode(),userId,c.getId());
 }
 /**
  *   userId     
  * @throws SQLException 
  * 
  */
 public List<Contactor> findAll(int userId) throws SQLException{
  String sql = "select * from linkman where userId = ?";
  List<Contactor> list = qr.query(sql, new BeanListHandler<Contactor>(Contactor.class),userId);
  return list;
  
 }
 /**
  *   id       
  * @throws SQLException 
  * 
  */
 public Contactor findByCid(int id) throws SQLException{
  String sql = "select * from linkman where id=?";
  Contactor c = qr.query(sql, new BeanHandler<Contactor>(Contactor.class), id);
  return c;
 }
 
}

package cn.edu.aynu.rjxy.dao;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import cn.edu.aynu.rjxy.bean.User;
import cn.edu.aynu.rjxy.utils.JDBCUtils;

/**
 *  user    
 * @author Administrator
 *
 */
public class UserDao {
 private QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
 /**
  *             
  * @param cid
  * @return
  * @throws SQLException
  */
 public User findByCid(String username,String password) throws SQLException{
  String sql = "select * from user where username=?&&password=?";
  User u = qr.query(sql, new BeanHandler<User>(User.class), username,password);
  return u;
 }
 /**
  *   
  * @param c
  * @throws SQLException 
  */
 public void register(User u) throws SQLException{
  String sql = "insert into user(username,password) values(?,?)";
  qr.update(sql, u.getUsername(),u.getPassword());
 }
}

package cn.edu.aynu.rjxy.service;

import java.sql.SQLException;
import java.util.List;

import cn.edu.aynu.rjxy.bean.Contactor;
import cn.edu.aynu.rjxy.dao.ContactorDao;

/**
 *        
 * @author Administrator
 *
 */
public class ContactorService {
 //  ContactorDao  
 ContactorDao dao = new ContactorDao();
 /**
  *      
  * @param c
  */
 public void add(Contactor c,int userId){
  try {
   dao.insert(c,userId);
  } catch (SQLException e) {
   throw new RuntimeException(e);
  }
 }
 /**
  *      
  * @param cid
  */
 public void delete(int id){
  try {
   dao.delete(id);
  } catch (SQLException e) {
   throw new RuntimeException(e);
  }
 }
 /**
  *   (  )   
  * @param cid
  */
 public List<Contactor> load(int userId){
  try {
   return dao.findAll(userId);
  } catch (SQLException e) {
   throw new RuntimeException(e);
  }
 }
 /**
  *   (  )   (      )
  * @param cid
  */
 public Contactor loadById(int id){
  try {
   return dao.findByCid(id);
  } catch (SQLException e) {
   throw new RuntimeException(e);
  }
 }
 /**
  *   (  )     
  * @param c
  */
 public void edit(Contactor c,int userId){
  try {
   dao.update(c,userId);
  } catch (SQLException e) {
   throw new RuntimeException(e);
  }
 }
 
}

package cn.edu.aynu.rjxy.service;

import java.sql.SQLException;

import cn.edu.aynu.rjxy.bean.User;
import cn.edu.aynu.rjxy.dao.UserDao;


/**
 * 
 * @author Administrator
 *
 */
public class UserService {
 //  ContactorDao  
 UserDao dao = new UserDao();
 /**
  *             
  * @param cid
  */
 public User login(String username,String password){
  try {
   return dao.findByCid(username,password);
  } catch (SQLException e) {
   throw new RuntimeException(e);
  }
 }
 /**
  *   
  * @param c
  */
 public void register(User u){
  try {
   dao.register(u);
  } catch (SQLException e) {
   throw new RuntimeException(e);
  }
 }
}

package cn.edu.aynu.rjxy.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.edu.aynu.rjxy.bean.Contactor;
import cn.edu.aynu.rjxy.bean.User;
import cn.edu.aynu.rjxy.service.ContactorService;
import cn.edu.aynu.rjxy.service.UserService;
import cn.edu.aynu.rjxy.utils.CommonsUtils;
/**
 *        
 * @author Administrator
 *
 */
public class ContactorServlet extends HttpServlet {
 private ContactorService cs = new ContactorService();
 private UserService us = new UserService();
 private int userId;
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  this.doPost(request, response);
 }

 /**
  *             method       method                      
  */
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  //      
  request.setCharacterEncoding("UTF-8");
  response.setContentType("text/html;charset=UTF-8");
  //      method  ,      int 
  int method = Integer.parseInt(request.getParameter("method"));
  
  switch(method){
   case 1: this.register(request, response);
     break;
   case 2: this.login(request, response);
     break;
   case 3: this.query(request, response);
     break;
   case 4: this.add(request, response);
     break;
   case 5: this.loadForUpdate(request, response);
     break;
   case 6: this.edit(request, response);
     break;
   case 7: this.delete(request, response);
     break;
  }
  
  
 }
 /**
  *        
  * @param request
  * @param response
  * @throws ServletException
  * @throws IOException
  */
 public void register(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
  //  CommonUtils           bean 
  User u = CommonsUtils.toBean(request.getParameterMap(), User.class);
  //        
  us.register(u);
  //        
  response.getWriter().print("    ,            ");
  response.setHeader("Refresh","3;url=http://127.0.0.1:8099/addresslist/jsp/top.jsp"); 

 }
 /**
  *         method=2
  * @param request
  * @param response
  * @throws ServletException
  * @throws IOException
  */
 public void login(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  //            
  String username = request.getParameter("username");
  String password = request.getParameter("password");
  //      
  User u = us.login(username, password);
  if (u != null) {
   HttpSession session = request.getSession();
   session.setAttribute("userId", u.getId());
   response.getWriter().print("    "+u.getId());
   //       pb  list.jsp  
   request.getRequestDispatcher("/jsp/list.jsp").forward(request, response);
  }else{
   response.getWriter().print("    ,         ,     ......");
   response.setHeader("Refresh","3;url=http://127.0.0.1:8099/addresslist/jsp/top.jsp"); 

  }
  
 }
 /**
  *            method=3
  * @param request
  * @param response
  * @throws ServletException
  * @throws IOException
  */
 public void query(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  HttpSession session = request.getSession();
   userId = (Integer) session.getAttribute("userId");
  //      
  List<Contactor> list = cs.load(userId);
  // list   request  
  request.setAttribute("contactorList", list);
  System.out.println(list.size());
  //             list.jsp     
  request.getRequestDispatcher("/jsp/list.jsp").forward(request, response);
  
 }
 /**
  *          
  * @param request
  * @param response
  * @throws ServletException
  * @throws IOException
  */
 public void add(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
  HttpSession session = request.getSession();
  userId = (Integer) session.getAttribute("userId");
  //  CommonUtils           bean 
  Contactor c = CommonsUtils.toBean(request.getParameterMap(), Contactor.class);
  //        
  cs.add(c,userId);
  //        
  response.getWriter().print("    ,          ......");
  response.setHeader("Refresh","3;url=http://127.0.0.1:8099/addresslist/jsp/list.jsp"); 
 }
 /**
  *             
  * @param request
  * @param response
  * @throws ServletException
  * @throws IOException
  */
 public void loadForUpdate(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
  //         cid
  String ids = request.getParameter("id");
  int id = Integer.valueOf(ids);
  //      
  Contactor c = cs.loadById(id);
  // Customer  c   request  
  request.setAttribute("contactor", c);
  //       customer     list.jsp   
  request.getRequestDispatcher("/jsp/edit.jsp").forward(request, response);

 }
 /**
  *             
  * @param request
  * @param response
  * @throws ServletException
  * @throws IOException
  */
 public void edit(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
  HttpSession session = request.getSession();
  userId = (Integer) session.getAttribute("userId");
  //  CommonUtils           bean 
  Contactor c = CommonsUtils.toBean(request.getParameterMap(), Contactor.class);
  //      
  cs.edit(c,userId);
  //            
  response.getWriter().print("    ,          ......");
  response.setHeader("Refresh","3;url=http://127.0.0.1:8099/addresslist/jsp/list.jsp"); 

 }
 /**
  *             
  * @param request
  * @param response
  * @throws ServletException
  * @throws IOException
  */
 public void delete(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
  //      cid
  String cid = request.getParameter("id");
  int id = Integer.valueOf(cid);
  //        
  cs.delete(id);
  //            
  response.getWriter().print("    ,          ......");
  response.setHeader("Refresh","3;url=http://127.0.0.1:8099/addresslist/jsp/list.jsp"); 


}

}

package cn.edu.aynu.rjxy.utils;

import java.util.Map;
import java.util.UUID;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;

/**
 *   UUID,         bean 
 * @author Administrator
 *
 */
public class CommonsUtils {
 /**
  *     UUID
  * @return
  */
 public static String uuid(){
  return UUID.randomUUID().toString().replace("-", "").toUpperCase();
  
 }
 
 /**
  *         bean 
  */
 public static <T> T toBean(Map data, Class<T> clazz){
  try{
   T bean = clazz.newInstance();
   BeanUtils.populate(bean, data);
   return bean;
  }catch (Exception e) {
   throw new RuntimeException(e);
  }
 }
 
 
}

package cn.edu.aynu.rjxy.utils;

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

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 *         
 * @author Administrator
 *
 */
public class JDBCUtils {
 //    C3P0-config              
 private static DataSource ds = new ComboPooledDataSource();
 //          
 public static DataSource getDataSource(){
  return ds;
 }
 //       
 public static Connection getConnection() throws SQLException{
  return ds.getConnection();
 } 
}

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
 <!--     ,   ComboPooledDataSource      ,          -->
 <default-config>
  <!--      -->
  <property name="jdbcUrl">jdbc:mysql://localhost:3306/linkman?characterEncoding=UTF-8</property>
  <property name="driverClass">com.mysql.jdbc.Driver</property>
  <property name="user">root</property>
  <property name="password">123456</property>
  <!--     ,     Connection   ,       -->
  <property name="acquireIncrement">3</property>
  <!--        ,       Connection   -->
  <property name="initialPoolSize">10</property>
  <!--     Connection  ,       ,    Connection -->
  <property name="minPoolSize">2</property>
  <!--          -->
  <property name="maxPoolSize">10</property>
 </default-config>
</c3p0-config>
이 프로젝트 에 필요 한 jar 패키지:

이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기