上一篇文章后几张图片不清晰,现补充代码如下:
一、 index.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">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
This is my index page. <br>
<form action="StudentServlet" method="Post">
教师入口:<br>
账号:<input type="text" name="TeacherName"/><br><br>
密码:<input type="password" name="TeacherPassword"><br><br>
<input type="submit" value="登录" />
<a href="#">注册</a>
</form>
</body>
</html>
二、StudentServlet 代码:
package pers.ms.rfeng.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import pers.ms.rfeng.dao.TeacherDao;
import pers.ms.rfeng.dao.TeacherDaoImpl;
import pers.ms.rfeng.model.PageBean;
import pers.ms.rfeng.model.Student;
import pers.ms.rfeng.service.StudentService;
public class StudentServlet extends HttpServlet {
private StudentService studentService = new StudentService();
private int i = 0;
/**
* 获取 pc
* @param request
* @return
*/
private int getPc(HttpServletRequest request) {
int pc = 1;
//初始化 pc 为 1
String parm = request.getParameter("pc");
if (parm != null && !parm.trim().isEmpty()) {
//if 语句的判断条件不懂
pc = Integer.parseInt(parm);
}
return pc;
}
/**
* 获取 URL
*
* @param request
* @return
*/
private String getUrl(HttpServletRequest request) {
String url = request.getRequestURI() + "?" + request.getQueryString();
int index = url.lastIndexOf("&pc=");
if (index != -1) {
url = url.substring(0, index);
}
return url;
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
int pc = getPc(request);
String url = getUrl(request);
PageBean<Student> pageBean = studentService.findByPage(pc);
pageBean.setUrl(url);
/**
* 保存当前查询内容
* 跳转到内容界面 message.jsp
*/
request.setAttribute("pb", pageBean);
request.getRequestDispatcher("/message.jsp").forward(request, response);
}
三、message.jsp 代码如下:
<%@ 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>My JSP 'message.jsp' starting page</title>
</head>
<body>
This is my message page. <br>
<table>
<tr>
<td>编号</td><td>名字</td><td>性别</td><td>年龄</td><td>地址</td><td>学分</td>
</tr>
<c:forEach items="${pb.beanlist }" var="student">
<tr>
<td>${student.id }</td>
<td>${student.name }</td>
<td>${student.gender }</td>
<td>${student.age }</td>
<td>${student.address }</td>
<td>${student.grade }</td>
</tr>
</c:forEach>
</table>
<%@ include file="/tab.jsp" %>
</body>
</html>
四、tab.jsp 代码如下:
<%@ 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>My JSP 'tab.jsp' starting page</title>
</head>
<body>
<c:choose>
<c:when test="${pb.pc eq null or pb.pc eq 1 }">last page</c:when>
<c:otherwise><a href="${pb.url }&pc=${pb.pc -1 }">last page</a></c:otherwise>
</c:choose>
<c:choose>
<c:when test="${pb.tp <6 }">
<c:set value="1" var="begin"></c:set>
<c:set value="${pb.tp }" var="end"></c:set>
</c:when>
<c:otherwise>
<c:set value="${pb.pc-2 }" var="begin"></c:set>
<c:set value="${pb.pc+3 }" var="end"></c:set>
<c:if test="${pb.pc <3 }">
<c:set value="1" var="begin"></c:set>
<c:set value="6" var="end"></c:set>
</c:if>
<c:if test="${pb.pc +3 >pb.tp }">
<c:set value="${pb.tp-5 }" var="begin"></c:set>
<c:set value="${pb.tp }" var="end"></c:set>
</c:if>
</c:otherwise>
</c:choose>
<c:forEach begin="${begin }" end="${end }" var="page">
<c:choose>
<c:when test="${pb.pc eq page }">${page }</c:when>
<c:otherwise><a href="${pb.url }&pc=${page}">${page }</a></c:otherwise>
</c:choose>
</c:forEach>
<c:choose>
<c:when test="${pb.pc eq pb.tp or pb.pc eq null }">next page</c:when>
<c:otherwise><a href="${pb.url }&pc=${pb.pc +1}">next page</a></c:otherwise>
</c:choose>
</body>
</html>