- See Also:
- Top Examples, Source Code
public void destroy()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[252]Hit Counter Servlet Filter
By Anonymous on 2004/10/06 09:09:02 Rate
public final class HitCounterFilter implements Filter {
private FilterConfig filterConfig = null;
public void init ( FilterConfig filterConfig )
throws ServletException {
this.filterConfig = filterConfig;
}
public void destroy ( ) {
this.filterConfig = null;
}
public void doFilter ( ServletRequest request,
ServletResponse response, FilterChain chain )
throws IOException, ServletException {
if ( filterConfig == null ) return;
StringWriter sw = new StringWriter ( ) ;
PrintWriter writer = new PrintWriter ( sw ) ;
Counter counter =
( Counter ) filterConfig.getServletContext ( ) .getAttribute ( "hitCounter" ) ;
writer.println ( "The number of hits is: " +
counter.incCounter ( ) ) ;
// Log the resulting string
writer.flush ( ) ;
filterConfig.getServletContext ( ) .log ( sw.getBuffer ( ) .toString ( ) ) ;
...
chain.doFilter ( request, wrapper ) ;
...
}
}
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOException,
ServletException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1018]Logging servlet Filter
By jnajvarek { at } artin { dot } cz on 2004/12/15 04:59:09 Rate
package com.drei.mnp;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.text.*;
import java.util.Date;
import java.util.Enumeration;
public class LoggingFilter implements Filter {
protected FilterConfig filterConfig;
protected String fileDir;
protected int sequence = 0;
public void init ( FilterConfig filterConfig ) throws ServletException {
this.filterConfig = filterConfig;
readConfig ( ) ;
}
public void destroy ( ) {
this.filterConfig = null;
}
public void doFilter ( ServletRequest request, ServletResponse response, FilterChain chain ) throws java.io.IOException, ServletException {
HttpServletRequest req = ( HttpServletRequest ) request;
HttpServletResponse res = ( HttpServletResponse ) response;
// pre request action
try {
// open file name
BufferedWriter out = new BufferedWriter (
new FileWriter (
fileDir+"servlet_log_"+
( ( new SimpleDateFormat ( "yyyy-MM-dd HH-mm-ss" ) ) .format ( new Date ( ) ) +
"_"+ ( new DecimalFormat ( "000000" ) ) .format ( sequence ) +
".txt" ) ,true ) ) ;
sequence++;
// log request content
out.write ( "***********************************************" ) ;
out.newLine ( ) ;
out.write ( "Request URL:"+req.getRequestURL ( ) .toString ( ) ) ;
out.newLine ( ) ;
out.write ( "Request query string:"+req.getQueryString ( ) ) ;
out.newLine ( ) ;
Enumeration e = req.getHeaderNames ( ) ;
if ( e!=null ) {
out.write ( "Request headers:" ) ;
out.newLine ( ) ;
while ( e.hasMoreElements ( ) ) {
String headerName = ( e.nextElement ( ) ) .toString ( ) ;
out.write ( " "+headerName+" = "+req.getHeader ( headerName ) ) ;
out.newLine ( ) ;
}
}
// create request wrapper
StringRequestWrapper requestWrapper = new StringRequestWrapper ( req ) ;
// create response wrapper
StringResponseWrapper responseWrapper = new StringResponseWrapper ( res ) ;
// call next filter in the chain :
chain.doFilter ( requestWrapper, responseWrapper ) ;
// post request action
out.write ( "Request body:" ) ;
out.newLine ( ) ;
out.write ( requestWrapper.getBody ( ) ) ;
out.newLine ( ) ;
out.write ( "----------------------------------------------" ) ;
out.newLine ( ) ;
out.write ( "Response body:" ) ;
out.newLine ( ) ;
out.write ( responseWrapper.toString ( ) ) ;
out.newLine ( ) ;
out.close ( ) ;
// write output to response if writer has been used
if ( responseWrapper.writerUsed ) {
response.getWriter ( ) .print ( responseWrapper.toString ( ) ) ;
}
} catch ( IOException e ) {
System.out.println ( e.toString ( ) ) ;
}
}
private void readConfig ( ) {
if ( filterConfig != null ) {
fileDir = filterConfig.getInitParameter ( "LoggingDirectory" ) ;
}
}
}
----------------------------
package com.drei.mnp;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.*;
public class StringResponseWrapper extends HttpServletResponseWrapper {
StringWriter writer = new StringWriter ( ) ;
LoggingServletOutputStream out;
boolean writerUsed = false;
public StringResponseWrapper ( HttpServletResponse response ) throws IOException {
super ( response ) ;
out = new LoggingServletOutputStream ( writer ) ;
}
public PrintWriter getWriter ( ) {
writerUsed = true;
return new PrintWriter ( writer ) ;
}
public ServletOutputStream getOutputStream ( ) throws IOException {
out.setOriginalOutputStream ( getResponse ( ) .getOutputStream ( ) ) ;
return out;
}
public String toString ( ) {
return writer.toString ( ) ;
}
}
-------------------------------
package com.drei.mnp;
import java.io.IOException;
import java.io.Writer;
import javax.servlet.ServletOutputStream;
public class LoggingServletOutputStream extends ServletOutputStream {
private ServletOutputStream out;
private Writer log;
public LoggingServletOutputStream ( Writer logWriter ) {
super ( ) ;
log = logWriter;
}
public void write ( int c ) throws IOException {
log.write ( c ) ;
out.write ( c ) ;
}
public void setOriginalOutputStream ( ServletOutputStream outputStream ) {
out = outputStream;
}
}
--------------------------------
package com.drei.mnp;
import java.io.IOException;
import javax.servlet.ServletInputStream;
public class LoggingServerInputStream extends ServletInputStream {
private ServletInputStream in;
private StringBuffer buffer = new StringBuffer ( ) ;
public LoggingServerInputStream ( ServletInputStream inputStream ) {
super ( ) ;
in = inputStream;
} ;
public int read ( ) throws IOException {
int chr = in.read ( ) ;
if ( chr!=-1 ) {
buffer.append ( ( char ) chr ) ;
}
return chr;
}
public String getContent ( ) {
return buffer.toString ( ) ;
}
}
---------------------------------
package com.drei.mnp;
import java.io.*;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
public class StringRequestWrapper extends HttpServletRequestWrapper {
private LoggingServerInputStream in;
public StringRequestWrapper ( HttpServletRequest request ) throws IOException {
super ( request ) ;
in = new LoggingServerInputStream ( request.getInputStream ( ) ) ;
}
public ServletInputStream getInputStream ( ) {
return in;
}
public String getBody ( ) {
return in.getContent ( ) ;
}
}
public void init(FilterConfig filterConfig)
throws ServletException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples