jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
联系人列表 | ||||
新增联系人 | ||||
姓名 | 电话 | 邮箱 | 关系 | 操作 |
删除 | ||||
删除 | ||||
删除 | ||||
删除 |
servlet页面:
package Servlet;
import impl.ContactsDaoImpl;
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 dao.ContactsDao;
import entity.Contacts;
public class SelectAll extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//处理乱码
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
//得到httpsession对象
HttpSession session=request.getSession();
//得到数组的值
String[] name=request.getParameterValues("name");
String[] phone=request.getParameterValues("phone");
String[] email=request.getParameterValues("email");
String[] contact=request.getParameterValues("contact");
//调用接口的实现方法;
ContactsDao contactsdao=new ContactsDaoImpl();
List<Contacts> list=contactsdao.selectAll();
if (list!=null) {
session.setAttribute("name", name);
response.sendRedirect("show.jsp");
}
}
}
接口的实现方法类页面:
package impl;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import dao.BaseDao;
import dao.ContactsDao;
import entity.Contacts;
public class ContactsDaoImpl extends BaseDao implements ContactsDao {
//查询所有
public List selectAll() {
List list=new ArrayList();
String sql="select * from Contacts";
Contacts contacts=null;
//调用basedao中的公共方法;
rs=this.executeQuery(sql, null);
try {
while (rs.next()) {
contacts=new Contacts(rs.getString("name"), rs.getString("phone"), rs.getString("email"), rs.getString("contact"));
list.add(contacts);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
this.closeAll(con, pstm, rs);
}
return list;
}
}