0

I have Java Web Application with next structure:

WEB-INF/
    lib/
    classes/
        contoller/
             MainServlet.class
             MainFilter. class
    web.xml
META-INF/
styles/
   default.css
enter.jsp
end.jsp

Using MainFilter, I tried to load default.css file and send it back to browser. But I was unsuccessful((( My code:

public void doFilter(ServletRequest req, ServletResponse resp,
            FilterChain chain) throws IOException, ServletException {
        String file = null;
        PrintWriter out = resp.getWriter();
        HttpServletRequest request = (HttpServletRequest ) req;
        String requestURI =  request.getRequestURI();
        BufferedReader in = null;

        String[] url= requestURI.split("\\/");
        for(String string: url) {
            if (string.contains("css")) {
                file = string;
            }
        }

if( file != null) {
            try {
                in = new BufferedReader(new FileReader(file));
//other irrelevant code
            } catch (FileNotFoundException e) {
                System.out.println("Was not able to open " + file);
            }
      }
}

I tried "default.css" as file variable, "styles/default.css". I tried to put default.css with jsp files. My MainFilter continues generating FileNotFound exception((( Can you help? What pelative path I should put so my filter could find css file?

ovod
  • 1,118
  • 4
  • 18
  • 33
  • CSS files are static files and should'nt go through servlet. – Jos Jul 29 '15 at 18:32
  • This is why I created ServletFilter. It is not right? The case is my Servlet processes / url - all requests go to my servlet. And I wanted so css be processed by Filter. – ovod Jul 29 '15 at 18:34
  • can you pls look into this.. http://stackoverflow.com/a/3582215/3981536 – Jos Jul 29 '15 at 18:34
  • unless you are doing some changes in css file before serving it to browser, your java code should'nt bother about the css files (even though that is not necessary almost always). So, you can give mappings in your config file to treat them separately. – Jos Jul 29 '15 at 18:37
  • Ok. thank you. And what if I want to add content to css, how my class can access css file? – ovod Jul 29 '15 at 18:40
  • That would be a very rare scenario and I would suggest not to do it. because static files like css/js/images etc are expected to be cached and reused at browser (and even in server, i guess). Instead of changing the css itself, you can code some logic in jsp to change the class names of desired elements before serving them, and keeping multiple classes/defenitions in your css, all the time. But if you have to do it, then you have to treat it like a request to download a file (a url that maps to a servlet, which will return a response with an attached file). Again, that would be bad. – Jos Jul 29 '15 at 18:46
  • So your proposition? – ovod Jul 29 '15 at 18:48
  • files like css/js/images etc should be considered static resources and shouldn't be modified at runtime. – Jos Jul 29 '15 at 18:54
  • also, this will help you with file not found.. http://stackoverflow.com/questions/12883861/how-to-access-a-file-under-web-inf-folder-in-java-class – Jos Jul 29 '15 at 18:54

0 Answers0