码迷,mamicode.com
首页 > 其他好文 > 详细

关于servlet filter

时间:2015-08-16 15:08:49      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:

 

创建一个过滤器实现javax.servlet.Filter接口。

package com.lyq;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class CountFilter implements Filter{
    //来访数量
    private int count;

    public void destroy() {
    }

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        count++;                                                                            //访问数量自增
        //将ServletRequest转换成HttpServletRequest
        HttpServletRequest req = (HttpServletRequest)request;
        //获取ServletContext
        ServletContext context = req.getSession().getServletContext();
        context.setAttribute("count", count);                //将访问数量放入到ServletContext中
        chain.doFilter(request, response);                     //向下传递过滤器
    }

    public void init(FilterConfig filterConfig) throws ServletException {
        String param = filterConfig.getInitParameter("count");  //获取初始化参数
        count = Integer.valueOf(param);                                                //将字符串转换为int
    }
    
}

 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 过滤器声明 -->
  <filter>
        <filter-name>CountFilter</filter-name>
        <filter-class>com.lyq.CountFilter</filter-class>
        <init-param>
            <param-name>count</param-name>
            <param-value>5000</param-value>
        </init-param>
  </filter>
        <filter-mapping>
        <filter-name>CountFilter</filter-name>
        <url-pattern>/index.jsp</url-pattern>
        </filter-mapping>
</web-app>

 

<%@ 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>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>
      <h2>
      欢迎光临,<br>
      你是本站的第【
      <%=application.getAttribute("count") %>
      】位访客!</h2>
  </body>
</html>

 

关于servlet filter

标签:

原文地址:http://www.cnblogs.com/rixiang/p/4734256.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!