Get Absolute File Path Using ServletFilter

It’s all begin when i need to get a text file content from a servlet filter, after spent some amount of time googling i’ve found a good solution. This is how i do it.

package com.edw.filter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.apache.log4j.Logger;

 * @author edw
public class MyFilter implements Filter {

    private FilterConfig filterConfig = null;
    private Logger logger = Logger.getLogger(this.getClass());

    public MyFilter() {

    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain)
	throws IOException, ServletException {
	try {
		String pathName = filterConfig.getServletContext().getRealPath("/");
		File file = new File(pathName);


		chain.doFilter(request, response);
	catch(Throwable t) {

    public void destroy() { 

    public void init(FilterConfig filterConfig) { 
		this.filterConfig = filterConfig;


Hope it can help others, thank you (*)


No Comments

Leave a Comment

Please be polite. We appreciate that.
Your email address will not be published and required fields are marked

:-[ (B) (^) (P) (@) (O) (D) :-S ;-( (C) (&) :-$ (E) (~) (K) (I) (L) (8) :-O (T) (G) (F) :-( (H) :-) (*) :-D (N) (Y) :-P (U) (W) ;-)