File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Filter Issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Filter Issue" Watch "Filter Issue" New topic

Filter Issue

alex zevenbergen

Joined: May 31, 2006
Posts: 2
Hi all

i'm trying to create a filter that takes in a request with a resource loaction as a parameter, removes this and then gets the absolute url of the the resource(eg - /myapp/images/mypic.gif)

i've tried 2 lines but both return null url= this.getClass().getClassLoader().getResource(/myapp/images/mypic.gif");

InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("/myapp/images/mypic.gif");

Does anybody see anything wrong with these lines?

Also after this i need to rewrite the url and send it to the next filter in the chain - does anyone have any hints for doing this?

Thanks in advance
Carl Trusiak

Joined: Jun 13, 2000
Posts: 3340
ClassLoader.getResourceAsStream looks in the classpath of the JVM to find files. Your webapps web content area is not in the Classpath unless you specifically add it at server start time which I do not recommend.

What you should be using is SevletContext getResourceAsStream

Also, the root of your webapp is the same directory that contains WEB-INF directory so more than likely the path sould be /images/mypic.gif

You can determine this programmaticly using ServletContext getServletContextName

I don't quite get why you feel you need to change the URL of the request but, it's fairly simple to do. It's actually using the RequestDispatcher forward to send the request to a new resource within your webapp ServletContext getRequestDispatcher If you need to get fancier than than, I suggest you look at URLRewrite on

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
alex zevenbergen

Joined: May 31, 2006
Posts: 2
Thanks for that.

My biggest problem is that the context is always retured as NULL.

ServletContext ctx = this.getServletContext();

ctx always returns NULL

basically i need to get the current web module that is running.
i tried getting it off the session but that also returned NULL

HttpServletRequest httpRequest = (HttpServletRequest)request;
HttpSession session =httpRequest.getSession(false);
AppContext ctx =(AppContext)session.getAttribute (WebConstants.App_CONTEXT_KEY);

AppContext and webconstants are both defined java classes

Note - all code is in the dofilter() of a filter
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

If that code is in your doFilter() method, its quite surprising that it compiled in first place. Which is the method getServletContext() in your code refreeing to? Do you have such a method of your own in the Servlet class?

The way to get a handle to the ServletContext object in your doFilter() method is through the FilterConfig object, a reference to which you should have saved in your init() method.

After you get a handle to the context object, use the getResourceAsStream() method as mentioned in the previous post.

I agree. Here's the link:
subject: Filter Issue
It's not a secret anymore!