servlet IndexSer + JSP页面 index.jsp
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import entity.MembersInfo;
import biz.MembersInfoBiz;
import biz.impl.MembersInfoBizImpl;
public class IndexSer extends HttpServlet {
public IndexSer() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 接收时设置的编码
request.setCharacterEncoding("utf-8");
// 转发时设置的编码
response.setCharacterEncoding("utf-8");
// 以 超文本格式 方式转发
response.setContentType("text/html");
// 获取了一个输出流
PrintWriter out = response.getWriter();
MembersInfoBiz membersInfoBiz = new MembersInfoBizImpl();
// 获取当前的页数,如果为空则访问第一页
String pageIndexStr = request.getParameter("pageIndex");
int pageIndex = pageIndexStr != null ? Integer.parseInt(pageIndexStr)
: 1;
String mid = request.getParameter("mid");
String mname = request.getParameter("mname");
String mgender = request.getParameter("mgender");
String mage = request.getParameter("mage");
// 为“更改信息成功”作一个标记
Boolean flag = false;
// id、姓名、性别以及年龄不为空,则插入一条数据
if (mid != null && mname != null && mgender != null && mage != null) {
String maddress = request.getParameter("maddress");
String memail = request.getParameter("memail");
MembersInfo membersInfo = new MembersInfo(Integer.parseInt(mid),
mname, mgender, Integer.parseInt(mage), maddress, memail);
membersInfoBiz.update(membersInfo);
// 修改标记
flag = true;
}
// 分页列表,每页显示四条信息
List<MembersInfo> membersInfos = membersInfoBiz.getMembersInfoByPage(
pageIndex, 4);
request.setAttribute("flag", flag);
request.setAttribute("pageIndex", pageIndex);
request.setAttribute("membersInfos", membersInfos);
request.getRequestDispatcher("index.jsp").forward(request, response);
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
public void init() throws ServletException {
}
}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<%
if(request.getAttribute("membersInfos")==null || request.getAttribute("membersInfos").equals("")){
response.sendRedirect("index");
}
%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>首页分页显示</title>
<link rel="stylesheet" type="text/css" href="css/basic.css" />
<style type="text/css">
table {
margin: 40px auto 0 auto;
}
table tr * {
line-height: 21px;
padding: 0 6px;
}
table th {
background: #F90;
text-align: center;
}
table .tdBg {
background: #f90;
}
table td a {
color: #039;
text-decoration: underline;
}
table td a:hover {
font-weight: bold;;
}
div {
text-align: center;
margin-top: 20px;
}
div a {
font-family: "微软雅黑";
font-size: 18px;
padding-right: 60px;
color: #333;
}
div a:hover {
color: #039;
text-decoration: underline;
}
</style>
</head>
<body>
<c:if test="${flag eq true }">
<div style="color: #c00;">更改信息成功</div>
</c:if>
<table border="1">
<tr>
<th colspan="6">俱乐部会员信息</th>
</tr>
<tr>
<th>编号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>家庭地址</th>
<th>Email</th>
</tr>
<c:forEach items="${membersInfos }" var="item" varStatus="i">
<tr class="<c:if test="${ i.index %2 ne 0}">tdBg</c:if>">
<td><a href="update?mid=${item.mid}">${item.mid }</a></td>
<td>${item.mname }</td>
<td>${item.mgender }</td>
<td>${item.mage }</td>
<td>${item.maddress }</td>
<td>${item.memail }</td>
</tr>
</c:forEach>
</table>
<div>
<a href="index?pageIndex=${pageIndex-1 }">上一页</a><a
href="index?pageIndex=${pageIndex+1 }">下一页</a>
</div>
</body>
</html>
servlet UpdateSer + JSP页面 update.jsp
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import entity.MembersInfo;
import biz.MembersInfoBiz;
import biz.impl.MembersInfoBizImpl;
public class UpdateSer extends HttpServlet {
public UpdateSer() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 接收时设置的编码
request.setCharacterEncoding("utf-8");
// 转发时设置的编码
response.setCharacterEncoding("utf-8");
// 以 超文本格式 方式转发
response.setContentType("text/html");
// 获取了一个输出流
PrintWriter out = response.getWriter();
// 获取id
String mid = request.getParameter("mid");
MembersInfoBiz membersInfoBiz = new MembersInfoBizImpl();
// 根据 id 查询信息
MembersInfo membersInfo = membersInfoBiz.getMembersInfoByMid(Integer
.parseInt(mid));
request.setAttribute("membersInfo", membersInfo);
request.getRequestDispatcher("update.jsp").forward(request, response);
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
public void init() throws ServletException {
}
}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>修改页面</title>
<link rel="stylesheet" type="text/css" href="css/basic.css" />
<style type="text/css">
table {
margin: 40px auto 0 auto;
}
table tr * {
line-height: 21px;
padding: 0 6px;
}
table input[type="submit"] {
margin-left: 40px;
}
table input[type="reset"] {
margin-left: 20px;
}
</style>
<script type="text/javascript">
//根据 id 获取元素
function $(id) {
return document.getElementById(id);
}
//开机加载
window.onload = function() {
//为表单注入 onsubmit 事件
document.updateForm.onsubmit = function() {
return check();
};
};
//提交表单前验证
function check() {
var mname = $("mname");
var mage = $("mage");
if ("" == mname.value || "" == mage.value) {
alert("会员姓名、性别和年龄不能为空");
return false;
}
return true;
}
</script>
</head>
<body>
<form action="index" method="post" name="updateForm">
<table border="1" bgcolor="#33FFFF">
<tr>
<th colspan="2">会员信息<input type="hidden" name="mid"
value="${membersInfo.mid }" />
</th>
</tr>
<tr>
<td>姓名</td>
<td><input type="text" value="${membersInfo.mname }"
name="mname" id="mname" /></td>
</tr>
<tr>
<td>性别</td>
<td><input type="radio" value="男" name="mgender"
checked="checked" />男<input type="radio" value="女" name="mgender" />女</td>
</tr>
<tr>
<td>年龄</td>
<td><input type="text" value="${membersInfo.mage }" name="mage"
id="mage" /></td>
</tr>
<tr>
<td>家庭地址</td>
<td><input type="text" value="${membersInfo.maddress }"
name="maddress" /></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" value="${membersInfo.memail }"
name="memail" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="修改" /><input
type="reset" value="重置" /></td>
</tr>
</table>
</form>
</body>
</html>
效果图: