File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

Filter Issue

alex zevenbergen
Greenhorn

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

java.net.URL 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
Alex
Carl Trusiak
Sheriff

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 Java.net


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

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.getServletContextName();

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
Thanks
Alex
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.

ram.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Filter Issue
 
Similar Threads
How can I read property files under tomcat
getting the value of .properties file
Reading properties file in jar META-INF
Reading a text file from a jar file?
Loading resource from a war.