Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2509783
  • 博文数量: 709
  • 博客积分: 12251
  • 博客等级: 上将
  • 技术积分: 7905
  • 用 户 组: 普通用户
  • 注册时间: 2005-07-17 00:00
个人简介

实现有价值的IT服务

文章存档

2012年(7)

2011年(147)

2009年(3)

2008年(5)

2007年(74)

2006年(431)

2005年(42)

分类: Java

2006-08-21 15:49:57

============================
//HelloFilter.java,在WEB-INF/classes/myfilter/HelloFilter.class
package myfilter;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloFilter implements Filter{
static int classCount = 0 ;
int count = 0 ;
static Hashtable instances = new Hashtable() ;

 public void init(FilterConfig config) throws ServletException{
 }
 public void doFilter(
  ServletRequest request,
  ServletResponse response,
  FilterChain chain) throws IOException,ServletException{
  System.out.println("Hello Filter!!") ;
  chain.doFilter(request,response) ;
  System.out.println("===========") ;
  
  count ++ ;
  System.out.println("Since loading,this servlet has been accessed " + count + " times.") ;
  instances.put(this,this) ;
  System.out.println("There are currently "+ instances.size() + " instances.");
  classCount ++ ;
  System.out.println("Across all instances,this servlet class has been " + "accessed " + classCount + " times.");

  }
 
 public void destroy(){
 }
}
 
==========
对应的web.xml
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "">
   
 
 
  Hello
  myfilter.HelloFilter
 

 
 
  Hello
  /*
 

===================================================
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class LoginCheck extends HttpServlet{
static int classCount = 0 ;
int count = 0 ;
static Hashtable instances = new Hashtable() ;
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{
res.setContentType("text/html") ;
//PrintWriter out = res.getWriter() ;
System.out.println("====================" + "
") ;
count ++ ;
System.out.println("Since loading,this servlet has been accessed " + count + " times." + "
") ;
instances.put(this,this) ;
System.out.println("There are currently "+ instances.size() + " instances." + "
") ;
classCount ++ ;
System.out.println("Across all instances,this servlet class has been " + "accessed " + classCount + " times." + "
");
System.out.println("method = " + req.getMethod() + "
");
System.out.println("URI = " + req.getRequestURI() + "
");
System.out.println("proto = " + req.getProtocol() + "
");
System.out.println("path = " + req.getServletPath() + "
");
System.out.println("pathinfo = " + req.getPathInfo() + "
");
System.out.println("pathtran = " + req.getPathTranslated() + "
");
System.out.println("querystring = " + req.getQueryString() + "
");
System.out.println("contentlength = " + req.getContentLength() + "
");
System.out.println("contenttype = " + req.getContentType() + "
");
System.out.println("servername = " + req.getServerName() + "
");
System.out.println("serverport = " + req.getServerPort() + "
");
System.out.println("remoteuser = " + req.getRemoteUser() + "
");
System.out.println("remoteaddr = " + req.getRemoteAddr() + "
");
System.out.println("remotehost = " + req.getRemoteHost() + "
");
System.out.println("authtype = " + req.getAuthType() + "
") ;
System.out.println("===============" + "
") ;
ServletContext sc = getServletContext();
RequestDispatcher rd = null;
rd = sc.getRequestDispatcher("/servlet/abc"); //定向的页面
rd.forward(req, res);
System.out.println("===========2") ;
}
}
 
阅读(851) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~