查询 request 对象的数据

在 EmpController 中调用

        RequestInfoService ris = new RequestInfoService();
        ris.saveRequestInfo(request);

保存 request 数据到数据库中

 

现在把保存在数据库的内容查询出来。

第一步:编写 requestLogList.jsp

<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>访问日志查看</title>

<style type="text/css">
body
{
    margin-left: 100px;
}
table, th, td
{
    border: 1px solid black;
}
</style>
</head>
<body>
<%@ include file="top.jsp" %>
<h2>访问日志查询</h2>
<form action="/web01/requestInfoController" method="get">
<input type="submit" value="Search">
<br/>
<table>
    <tr>
        <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:forEach items="${requestScope.requestInfoList}" var="requestInfo">
    <tr>
        <td>${requestInfo.characterEncoding }</td>
         <td>${requestInfo.contentType }</td>
        <td>${requestInfo.contextPath }</td>
        <td>${requestInfo.localAddr }</td>
        <td>${requestInfo.localName }</td>
        <td>${requestInfo.localPort }</td>
        <td>${requestInfo.method }</td>
        <td>${requestInfo.remoteAddr }</td>
        <td>${requestInfo.remoteHost }</td>
        <td>${requestInfo.remotePort }</td>
        <td>${requestInfo.remoteUser }</td>
        <td>${requestInfo.requestURI }</td>
        <td>${requestInfo.requestedSessionId }</td>
        <td>${requestInfo.locale }</td>
        <td>${requestInfo.regiDt }</td>
    </tr>
    </c:forEach>
</table>
</form>
<%@ include file="bottom.jsp" %>
</body>
</html>

第二步:RequestInfoBean

package com.test.system.bean;

public class RequestInfoBean {
    private String characterEncoding = "";
    private String contentType = "";
    private String contextPath = "";
    private String localAddr = "";
    private String localName = "";
    private int localPort = 0;
    private String method = "";
    private String remoteAddr = "";
    private String remoteHost = "";
    private int remotePort = 0;
    private String remoteUser = "";
    private String requestURI = "";
    private String requestedSessionId = "";
    private String locale = "";
    private String regiDt = "";    
    
    public String getCharacterEncoding() {
        return characterEncoding;
    }

    public void setCharacterEncoding(String characterEncoding) {
        this.characterEncoding = characterEncoding;
    }

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    public String getContextPath() {
        return contextPath;
    }

    public void setContextPath(String contextPath) {
        this.contextPath = contextPath;
    }

    public String getLocalAddr() {
        return localAddr;
    }

    public void setLocalAddr(String localAddr) {
        this.localAddr = localAddr;
    }

    public String getLocalName() {
        return localName;
    }

    public void setLocalName(String localName) {
        this.localName = localName;
    }

    public int getLocalPort() {
        return localPort;
    }

    public void setLocalPort(int localPort) {
        this.localPort = localPort;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getRemoteAddr() {
        return remoteAddr;
    }

    public void setRemoteAddr(String remoteAddr) {
        this.remoteAddr = remoteAddr;
    }

    public String getRemoteHost() {
        return remoteHost;
    }

    public void setRemoteHost(String remoteHost) {
        this.remoteHost = remoteHost;
    }

    public int getRemotePort() {
        return remotePort;
    }

    public void setRemotePort(int remotePort) {
        this.remotePort = remotePort;
    }
    
    public String getRemoteUser() {
        return remoteUser;
    }

    public void setRemoteUser(String remoteUser) {
        this.remoteUser = remoteUser;
    }

    public String getRegiDt() {
        return regiDt;
    }

    public void setRegiDt(String regiDt) {
        this.regiDt = regiDt;
    }

    public String getRequestURI() {
        return requestURI;
    }

    public void setRequestURI(String requestURI) {
        this.requestURI = requestURI;
    }

    public String getRequestedSessionId() {
        return requestedSessionId;
    }

    public void setRequestedSessionId(String requestedSessionId) {
        this.requestedSessionId = requestedSessionId;
    }
    
    public String getLocale() {
        return locale;
    }

    public void setLocale(String locale) {
        this.locale = locale;
    }

}

第三步:编写 RequestInfoController

package com.test.system.controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.test.system.bean.RequestInfoBean;
import com.test.system.service.RequestInfoService;

/**
 * Servlet implementation class RequestInfoController
 */
@WebServlet("/RequestInfoController")
public class RequestInfoController extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RequestInfoController() {
        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        RequestInfoService ris = new RequestInfoService();
        ArrayList<RequestInfoBean> requestInfoList = ris.getRequestInfoList();
        
        request.setAttribute("requestInfoList", requestInfoList);
        request.getRequestDispatcher("/view/requestLogList.jsp").forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

}

第四步:RequestInfoService

    // 查询List
    public ArrayList<RequestInfoBean> getRequestInfoList(){
        
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        
        ArrayList<RequestInfoBean> requestInfoList = new ArrayList<RequestInfoBean>();

        BaseDao baseDao = new BaseDao();
        try {
            conn = baseDao.dbConnection();
        } catch (SQLException e1) {
            e1.printStackTrace();
        }

        StringBuffer sqlBf = new StringBuffer();
        sqlBf.setLength(0);
        
        sqlBf.append("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");

        System.out.println(sqlBf.toString());
        
        try {
            pstmt = conn.prepareStatement(sqlBf.toString());

            rs = pstmt.executeQuery();
            while (rs.next()) {
                RequestInfoBean rib = new RequestInfoBean();
                
                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"));
                
                requestInfoList.add(rib);
            }            
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        try {
            baseDao.dbDisconnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return requestInfoList;
    }    

第五步:测试

访问 http://localhost:8081/web01/view/requestLogList.jsp 点击 search 按钮

 

posted @ 2015-09-17 13:53  bada130  阅读(275)  评论(0编辑  收藏  举报