June,
Apparently, Jason has updated the filter example. The previous code that I
posted had several compile problems. Here's a new version that works.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class TimerFilter implements Filter {
private FilterConfig config = null;
public void init(FilterConfig config) throws ServletException {
this.config = config;
System.out.println("TimerFilter.init()");
// nothing needs to be done here
}
public void destroy() {
config = null;
}
public void setFilterConfig(FilterConfig config) {
System.out.println("TimerFilter.setFilterConfig()");
this.config = config;
}
public FilterConfig getFilterConfig() {
System.out.println("TimerFilter.getFilterConfig()");
return config;
}
public void doFilter(ServletRequest req,
ServletResponse res,
FilterChain chain)
throws IOException, ServletException {
System.out.println("TimerFilter.doFilter()");
long before = System.currentTimeMillis();
chain.doFilter(req, res); // no chain parameter needed here
long after = System.currentTimeMillis();
String name = "";
if (req instanceof HttpServletRequest) {
name = ((HttpServletRequest)req).getRequestURI();
}
config.getServletContext().log(name + ": " + (after - before) + "ms");
}
}
Since the filter's name has changed, here's the necessary entries in the
web.xml file:
<filter>
<filter-name>timerFilter</filter-name>
<filter-class>TimerFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>timerFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Jason's complete article is found here:
http://www.javaworld.com/javaworld/jw-06-2001/jw-0622-filters.html
Regards,
Richard
At 03:59 PM 4/16/2002 -0600, you wrote:
>Hi Michael,
>
>After reading your reply, I thought it seemed to explain the recursive
>call to the EntryServlet, because I do reference other resources like
>images and other JSPs. So, I ran a test, I wrote a very simple html, like
><html><body> can you see this? </body></html>, which does not reference
>anything in the server, and, arrrrrrrrrrrrgh, I still get my debug msg a
>thousand times and I get java.lang.StackOverflowError. It still makes
>that recursive call to the EntryServlet!!!
>
>June
>
>
>On Tue, 16 Apr 2002 12:56:11 +0200, Michael Weller <[EMAIL PROTECTED]> wrote:
>
> >hi!
> >maybe the reason is that in your html you have references to some other
> >resources. if a client loads your html, it also needs to request these
> >other resources (e.g. images, css-files) from your server, your servlet is
> >mapped to get all requests in its context so it also gets those requesting
> >other files. this would explain n calls to your servlet, if n resources are
> >referenced in the html code your servlet served, not thousands of them...
> >
> >hope this helps!
> >
> >-mw
> >
> >At 15:35 16.04.2002 +1000, you wrote:
> >>Hello all,
> >>I wanted to serve everything in the root
> >>directory(html, jsp) through this EntryServlet, in
> >>which I do some counting and various stuff.
> >>
> >>I thought the way to map a servlet to the root was:
> >>
> >><servlet>
> >> <servlet-name>entry</servlet-name>
> >> <servlet-class>common.EntryServlet</servlet-class>
> >></servlet>
> >>
> >><servlet-mapping>
> >> <servlet-name>entry</servlet-name>
> >> <url-pattern>/*</url-pattern>
> >></servlet-mapping>
> >>
> >>
> >>But when I do that, I think it makes recursive call to
> >>itself, because my debug message in doGet message gets
> >>printed on the tomcat console like a thousand times
> >>and tells me that stack overflowed.
> >>
> >>What am I doing wrong?
> >>
>
>___________________________________________________________________________
>To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
>of the message "signoff SERVLET-INTEREST".
>
>Archives: http://archives.java.sun.com/archives/servlet-interest.html
>Resources: http://java.sun.com/products/servlet/external-resources.html
>LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html