============================
//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") ;
}
}
阅读(877) | 评论(0) | 转发(0) |