共4个文件
requestLogList.jsp
RequestInfoController.java
RequestInfoBean.java
RequestInfoService.java
1、requestLogList.jsp
功能点:
1.1、总个数:页面加载时COUNT(1)查询总数
1.2、总页数:总个数 %(取模)每页显示数量,如果正好结果为0时,总数除以每页显示数量为总页数,不能整除就是总数除以每页显示数量的加上1为总页数。
如:rs.getInt("TTL_CNT") % Constant.UNIT_CNT == 0 ? rs.getInt("TTL_CNT") / Constant.UNIT_CNT : rs.getInt("TTL_CNT") / Constant.UNIT_CNT + 1;
1.3、首页:点击 首页 时指向第一页
1.4、上一页:当前页 - 1(当前页保存在session当中)
1.5、上一页:当前页 + 1
1.6、尾页:总页数
1.7、直接访问:直接用js方式调用后台,传递页数
<%@ page language="java"contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core"PRefix="c"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><metahttp-equiv="Content-Type"content="text/html; charset=UTF-8"><title>访问日志查看</title><styletype="text/CSS">body{margin-left:100px;}table, th, td{border:1px solid black;}</style></head><body><%@ include file="top.jsp"%><h2>访问日志查询</h2><formaction="/web01/requestInfoController"method="get"><inputtype="submit"value="Search"><inputtype="hidden"name="callTp"value="requestInfoList"><
br/><table><tr><th>NO</th><th>characterEncoding</th><th>contentType</th><th>contextPath</th><th>localAddr</th><th>localName</th><th>localPort</th><th>method</th><th>remoteAddr</th><th>remoteHost</th><th>remotePort</th><th>remoteUser</th><th>requestURI</th><th>requestedSessionId</th><th>locale</th><th>regiDt</th></tr><c:forEachitems="${requestScope.requestInfoList}"var="requestInfo"><tr><td><c:outvalue="${requestInfo.rowSeq }"default=" "/></td><td><c:outvalue="${requestInfo.characterEncoding }"default=" "/></td><td><c:outvalue="${requestInfo.contentType }"default=" "/></td><td><c:outvalue="${requestInfo.contextPath }"default=" "/></td><td><c:outvalue="${requestInfo.localAddr }"default=" "/></td><td><c:outvalue="${requestInfo.localName }"default=" "/></td><td><c:outvalue="${requestInfo.localPort }"default=" "/></td><td><c:outvalue="${requestInfo.method }"default=" "/></td><td><c:outvalue="${requestInfo.remoteAddr }"default=" "/></td><td><c:outvalue="${requestInfo.remoteHost }"default=" "/></td><td><c:outvalue="${requestInfo.remotePort }"default=" "/></td><td><c:outvalue="${requestInfo.remoteUser }"default=" "/></td><td><c:outvalue="${requestInfo.requestURI }"default=" "/></td><td><c:outvalue="${requestInfo.requestedSessionId }"default=" "/></td><td><c:outvalue="${requestInfo.locale }"default=" "/></td><td><c:outvalue="${requestInfo.regiDt }"default=" "/></td></tr></c:forEach></table></form>总个数:<b>${sessionScope.ttlCnt}</b>页<
br>总页数:<b>${sessionScope.ttlPage}</b>页<
br><ahref="/web01/requestInfoController?callTp=requestInfoPageList&now_page_num=1">首页 |</a><c:choose><c:whentest="${sessionScope.now_page_num==1}">上一页</c:when><c:otherwise><ahref="/web01/requestInfoController?callTp=requestInfoPageList&now_page_num=${sessionScope.now_page_num - 1}">上一页 |</a></c:otherwise></c:choose><c:choose><c:whentest="${sessionScope.now_page_num==sessionScope.ttlPage}">下一页</c:when><c:otherwise><ahref="/web01/requestInfoController?callTp=requestInfoPageList&now_page_num=${sessionScope.now_page_num + 1}">下一页 |</a></c:otherwise></c:choose><ahref="/web01/requestInfoController?callTp=requestInfoPageList&now_page_num=${sessionScope.ttlPage}">尾页</a> <scripttype="text/Javascript">functionpageNum_Change(){varnow_page_num=document.getElementById("now_page_num").value;
window.open("/web01/requestInfoController?callTp=requestInfoPageList&now_page_num="+now_page_num,"_self");
}</script>直接访问:<inputid="now_page_num"value="${sessionScope.now_page_num}"><inputtype="button"value="go"onclick="pageNum_Change()"><%@ include file="bottom.jsp"%></body></html>
2、RequestInfoController.java
packagecom.test.system.controller;importjava.io.IOException;importjava.util.ArrayList;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;importcom.test.system.bean.RequestInfoBean;importcom.test.system.service.RequestInfoService;/*** Servlet implementation class RequestInfoController*/@WebServlet("/RequestInfoController")publicclassRequestInfoControllerextendsHttpServlet {privatestaticfinallongserialVersionUID = 1L;/***@seeHttpServlet#HttpServlet()*/publicRequestInfoController() {super();
}/***@seeHttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)*/protectedvoiddoGet(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {
String callTp= request.getParameter("callTp");if(callTp.equals("requestInfoList")) {intnow_page_num = 1;
RequestInfoService ris=newRequestInfoService();
ArrayList<RequestInfoBean> requestInfoList = ris.getRequestInfoList("", now_page_num);
HttpSession session=request.getSession();//当前页面(第一次查询时设置成第一页)session.setAttribute("now_page_num", now_page_num);//总页数intttlPage =ris.getTtlPage();
session.setAttribute("ttlPage", ttlPage);//获取总数intttlCnt =ris.getTtlCount();
session.setAttribute("ttlCnt", ttlCnt);
request.setAttribute("requestInfoList", requestInfoList);
request.getRequestDispatcher("/view/requestLogList.jsp").forward(request, response);
}elseif(callTp.equals("requestInfoPageList")) {
RequestInfoService ris=newRequestInfoService();
ArrayList<RequestInfoBean> requestInfoList = ris.getRequestInfoList("", Integer.parseInt(request.getParameter("now_page_num")));
HttpSession session=request.getSession();session.setAttribute("now_page_num", request.getParameter("now_page_num"));//总页数intttlPage =ris.getTtlPage();
session.setAttribute("ttlPage", ttlPage);//获取总数intttlCnt =ris.getTtlCount();
session.setAttribute("ttlCnt", ttlCnt);
request.setAttribute("requestInfoList", requestInfoList);
request.getRequestDispatcher("/view/requestLogList.jsp").forward(request, response);
}
}/***@seeHttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/protectedvoiddoPost(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {this.doGet(request, response);
}
}
3、RequestInfoBean.java
packagecom.test.system.bean;publicclassRequestInfoBean {privateString characterEncoding = "";privateString contentType = "";privateString contextPath = "";privateString localAddr = "";privateString localName = "";privateintlocalPort = 0;privateString method = "";privateString remoteAddr = "";privateString remoteHost = "";privateintremotePort = 0;privateString remoteUser = "";privateString requestURI = "";privateString requestedSessionId = "";privateString locale = "";privateString regiDt = "";privateintRowSeq = 0;publicString getCharacterEncoding() {returncharacterEncoding;
}publicvoidsetCharacterEncoding(String characterEncoding) {this.characterEncoding =characterEncoding;
}publicString getContentType() {returncontentType;
}publicvoidsetContentType(String contentType) {this.contentType =contentType;
}publicString getContextPath() {returncontextPath;
}publicvoidsetContextPath(String contextPath) {this.contextPath =contextPath;
}publicString getLocalAddr() {returnlocalAddr;
}publicvoidsetLocalAddr(String localAddr) {this.localAddr =localAddr;
}publicString getLocalName() {returnlocalName;
}publicvoidsetLocalName(String localName) {this.localName =localName;
}publicintgetLocalPort() {returnlocalPort;
}publicvoidsetLocalPort(intlocalPort) {this.localPort =localPort;
}publicString getMethod() {returnmethod;
}publicvoidsetMethod(String method) {this.method =method;
}publicString getRemoteAddr() {returnremoteAddr;
}publicvoidsetRemoteAddr(String remoteAddr) {this.remoteAddr =remoteAddr;
}publicString getRemoteHost() {returnremoteHost;
}publicvoidsetRemoteHost(String remoteHost) {this.remoteHost =remoteHost;
}publicintgetRemotePort() {returnremotePort;
}publicvoidsetRemotePort(intremotePort) {this.remotePort =remotePort;
}publicString getRemoteUser() {returnremoteUser;
}publicvoidsetRemoteUser(String remoteUser) {this.remoteUser =remoteUser;
}publicString getRegiDt() {returnregiDt;
}publicvoidsetRegiDt(String regiDt) {this.regiDt =regiDt;
}publicString getRequestURI() {returnrequestURI;
}publicvoidsetRequestURI(String requestURI) {this.requestURI =requestURI;
}publicString getRequestedSessionId() {returnrequestedSessionId;
}publicvoidsetRequestedSessionId(String requestedSessionId) {this.requestedSessionId =requestedSessionId;
}publicString getLocale() {returnlocale;
}publicvoidsetLocale(String locale) {this.locale =locale;
}publicintgetRowSeq() {returnRowSeq;
}publicvoidsetRowSeq(introwSeq) {
RowSeq=rowSeq;
}
}
4、RequestInfoService.java
UNIT_CNT:每页显示数量(常量提取到Constant.java中)
now_page_num:当天页面
packagecom.test.system.service;importjava.sql.Connection;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.util.ArrayList;importjavax.servlet.http.HttpServletRequest;importcom.test.common.Constant;importcom.test.common.dao.BaseDao;importcom.test.system.bean.RequestInfoBean;publicclassRequestInfoService {privateintidx = 1;publicRequestInfoService() {
}//保存request信息publicvoidsaveRequestInfo(HttpServletRequest request){
Connection conn=null;
PreparedStatement pstmt=null;
BaseDao baseDao=newBaseDao();try{
conn=baseDao.dbConnection();
}catch(SQLException e1) {
e1.printStackTrace();
}
StringBuffer sqlBf=newStringBuffer();
sqlBf.setLength(0);
sqlBf.append("INSERT INTO REQUEST_INFO (REQUEST_INFO_SEQ \n");
sqlBf.append(" , CHARACTER_ENCODING \n");
sqlBf.append(" , CONTENT_TYPE \n");
sqlBf.append(" , CONTEXT_PATH \n");
sqlBf.append(" , LOCAL_ADDR \n");
sqlBf.append(" , LOCAL_NAME \n");
sqlBf.append(" , LOCAL_PORT \n");
sqlBf.append(" , METHOD \n");
sqlBf.append(" , REMOTE_ADDR \n");
sqlBf.append(" , REMOTE_HOST \n");
sqlBf.append(" , REMOTE_PORT \n");
sqlBf.append(" , REMOTE_USER \n");
sqlBf.append(" , REQUEST_URI \n");
sqlBf.append(" , REQUESTED_SESSION_ID \n");
sqlBf.append(" , LOCALE \n");
sqlBf.append(" , REGI_DT) \n");
sqlBf.append("VALUES(SEQ_REQUEST_INFO.NEXTVAL \n");
sqlBf.append(" , ? \n");
sqlBf.append(" , ? \n");
sqlBf.append(" , ? \n");
sqlBf.append(" , ? \n");
sqlBf.append(" , ? \n");
sqlBf.append(" , ? \n");
sqlBf.append(" , ? \n");
sqlBf.append(" , ? \n");
sqlBf.append(" , ? \n");
sqlBf.append(" , ? \n");
sqlBf.append(" , ? \n");
sqlBf.append(" , ? \n");
sqlBf.append(" , ? \n");
sqlBf.append(" , ? \n");
sqlBf.append(" , SYSDATE) \n");
System.out.println(sqlBf.toString());try{
pstmt=conn.prepareStatement(sqlBf.toString());
idx= 1;
pstmt.setString(idx++, request.getCharacterEncoding());
pstmt.setString(idx++, request.getContentType());
pstmt.setString(idx++, request.getContextPath());
pstmt.setString(idx++, request.getLocalAddr());
pstmt.setString(idx++, request.getLocalName());
pstmt.setInt(idx++, request.getLocalPort());
pstmt.setString(idx++, request.getMethod());
pstmt.setString(idx++, request.getRemoteAddr());
pstmt.setString(idx++, request.getRemoteHost());
pstmt.setInt(idx++, request.getRemotePort());
pstmt.setString(idx++, request.getRemoteUser());
pstmt.setString(idx++, request.getRequestURI());
pstmt.setString(idx++, request.getRequestedSessionId());
pstmt.setString(idx++, request.getLocale().toString());inti =pstmt.executeUpdate();if(i == 1) {
System.out.println("##### save request success \n");
}else{
System.out.println("##### save request fail \n");
}
}catch(SQLException e) {
e.printStackTrace();
}try{
baseDao.dbDisconnection();
}catch(SQLException e) {
e.printStackTrace();
}
}//查询ListpublicArrayList<RequestInfoBean> getRequestInfoList(String str,intnow_page_num){
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
ArrayList<RequestInfoBean> requestInfoList =newArrayList<RequestInfoBean>();
BaseDao baseDao=newBaseDao();try{
conn=baseDao.dbConnection();
}catch(SQLException e1) {
e1.printStackTrace();
}
StringBuffer sqlBf=newStringBuffer();
sqlBf.setLength(0);
sqlBf.append("SELECT A.* \n");
sqlBf.append("FROM (SELECT T1.* \n");
sqlBf.append(" , ROWNUM AS ROW_SEQ \n");
sqlBf.append(" FROM (SELECT CHARACTER_ENCODING \n");
sqlBf.append(" , CONTENT_TYPE \n");
sqlBf.append(" , CONTEXT_PATH \n");
sqlBf.append(" , LOCAL_ADDR \n");
sqlBf.append(" , LOCAL_NAME \n");
sqlBf.append(" , LOCAL_PORT \n");
sqlBf.append(" , METHOD \n");
sqlBf.append(" , REMOTE_ADDR \n");
sqlBf.append(" , REMOTE_HOST \n");
sqlBf.append(" , REMOTE_PORT \n");
sqlBf.append(" , REMOTE_USER \n");
sqlBf.append(" , REQUEST_URI \n");
sqlBf.append(" , REQUESTED_SESSION_ID \n");
sqlBf.append(" , LOCALE \n");
sqlBf.append(" , TO_CHAR(REGI_DT, 'YYYY/MM/DD HH24:MI:SS') REGI_DT \n");
sqlBf.append(" FROM REQUEST_INFO \n");
sqlBf.append(" ORDER BY REQUEST_INFO_SEQ DESC \n");
sqlBf.append(" ) T1 \n");
sqlBf.append(" WHERE ROWNUM < (? * ?) + 1 \n");
sqlBf.append(" ) A \n");
sqlBf.append("WHERE A.ROW_SEQ > (? * (? - 1)) \n");
sqlBf.append("ORDER BY A.ROW_SEQ \n");try{
pstmt=conn.prepareStatement(sqlBf.toString());
pstmt.setInt(1, Constant.UNIT_CNT);
pstmt.setInt(2, now_page_num);
pstmt.setInt(3, Constant.UNIT_CNT);
pstmt.setInt(4, now_page_num);
rs=pstmt.executeQuery();while(rs.next()) {
RequestInfoBean rib=newRequestInfoBean();
rib.setCharacterEncoding(rs.getString("CHARACTER_ENCODING"));
rib.setContentType(rs.getString("CONTENT_TYPE"));
rib.setContextPath(rs.getString("CONTEXT_PATH"));
rib.setLocalAddr(rs.getString("LOCAL_ADDR"));
rib.setLocalName(rs.getString("LOCAL_NAME"));
rib.setLocalPort(rs.getInt("LOCAL_PORT"));
rib.setMethod(rs.getString("METHOD"));
rib.setRemoteAddr(rs.getString("REMOTE_ADDR"));
rib.setRemoteHost(rs.getString("REMOTE_HOST"));
rib.setRemotePort(rs.getInt("REMOTE_PORT"));
rib.setRemoteUser(rs.getString("REMOTE_USER"));
rib.setRequestURI(rs.getString("REQUEST_URI"));
rib.setRequestedSessionId(rs.getString("REQUESTED_SESSION_ID"));
rib.setLocale(rs.getString("LOCALE"));
rib.setRegiDt(rs.getString("REGI_DT"));
rib.setRowSeq(rs.getInt("ROW_SEQ"));
requestInfoList.add(rib);
}
}catch(SQLException e) {
e.printStackTrace();
}try{
baseDao.dbDisconnection();
}catch(SQLException e) {
e.printStackTrace();
}returnrequestInfoList;
}//获取记录总数publicintgetTtlCount(){intttlCnt = 0;//Total CountConnection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
BaseDao baseDao=newBaseDao();try{
conn=baseDao.dbConnection();
}catch(SQLException e1) {
e1.printStackTrace();
}
StringBuffer sqlBf=newStringBuffer();
sqlBf.setLength(0);
sqlBf.append("SELECT COUNT(1) TTL_CNT \n");
sqlBf.append("FROM REQUEST_INFO \n");try{
pstmt=conn.prepareStatement(sqlBf.toString());
rs=pstmt.executeQuery();if(rs.next()) {
ttlCnt= rs.getInt("TTL_CNT");
}
}catch(SQLException e) {
e.printStackTrace();
}try{
baseDao.dbDisconnection();
}catch(SQLException e) {
e.printStackTrace();
}returnttlCnt;
}//获取页数publicintgetTtlPage(){intttlPage = 0;//Total CountConnection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
BaseDao baseDao=newBaseDao();try{
conn=baseDao.dbConnection();
}catch(SQLException e1) {
e1.printStackTrace();
}
StringBuffer sqlBf=newStringBuffer();
sqlBf.setLength(0);
sqlBf.append("SELECT COUNT(1) TTL_CNT \n");
sqlBf.append("FROM REQUEST_INFO \n");try{
pstmt=conn.prepareStatement(sqlBf.toString());
rs=pstmt.executeQuery();if(rs.next()) {
ttlPage= rs.getInt("TTL_CNT") % Constant.UNIT_CNT == 0 ? rs.getInt("TTL_CNT") / Constant.UNIT_CNT : rs.getInt("TTL_CNT") / Constant.UNIT_CNT + 1;
}
}catch(SQLException e) {
e.printStackTrace();
}try{
baseDao.dbDisconnection();
}catch(SQLException e) {
e.printStackTrace();
}returnttlPage;
}
}
测试结果: