자바 독학===Filter 류 의 응용,사이트 수량 통계

package filterStatistic;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
//             
public class FilterFlux extends HttpServlet implements Filter {

    private static int flux = 0;

    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public synchronized void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain filterChain) throws
            ServletException, IOException {
       this.flux++;
       request.setAttribute("flux",String.valueOf(flux)); //      request   
       filterChain.doFilter(request, response);
    }

    public void destroy() {
    }
}

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <filter>
  	<filter-name>filterflux</filter-name>
  	<filter-class>filterStatistic.FilterFlux</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>filterflux</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

index.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>               </title>
</head>

<body>
<div align="center">

  <table width="429" height="388" border="0" cellpadding="0" cellspacing="0" background="images/background.jpg">
    <tr align="center">
      <td ><%=request.getAttribute("flux")%> </td>
    </tr>
  </table>
  <br>

</div>
</body>
</html>

입력http://localhost:8080/filter/
새로 고침 을 실행 할 때마다 2????
원본 코드 첨부 파일 참조

좋은 웹페이지 즐겨찾기