Referencing a resource placed in WEB-INF folder in JSP file returns HTTP 404 on resource

∥☆過路亽.° 提交于 2019-12-02 19:13:12

问题


I have a dynamic web project called BookShopWeb which I created in eclipse, with the following directory structure

/BookShopWeb/|
  |--src
  |---WebContent
                | 
                |---META-INF
                |----WEB-INF---web.xml
                            |
                            |--css--styles.css
                            |--jsp---index.jsp 

In web.xml I set the start page as

<welcome-file-list>

<welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>

In the index.jsp I am including the css as

<head>
<link rel="stylesheet" type="text/css" href="../css/styles.css" />
</head>

The index page when loaded however does not show the css info.I checked the element with firebug and it shows an error report

Apache Tomcat/6.0.29 - Error report..
The requested resource (/css/styles.css) is not available.

Any idea why this occurs?How can I correct this? thanks mark


回答1:


Files in /WEB-INF folder are not public accessible. Put the CSS files one level up, in the WebContent folder, and ensure that they are accessible by entering their URL straight in the browser address bar. Also, the URL which you specify in the <link href> must be relative to the request URL (which you see in the browser address bar when opening the JSP), not to its location on the server disk file system. The best approach is to make it domain-relative by starting with a forward slash /.

<link rel="stylesheet" href="/BookShopWeb/css/styles.css" />

or a bit more dynamically so that you don't need to change your JSPs everytime whenever you change the context path

<link rel="stylesheet" href="${pageContext.request.contextPath}/css/styles.css" />

JSP files can be kept in /WEB-INF, but this way they are only accessible through a dispatching servlet, either homegrown by extending HttpServlet or implicitly by the servletcontainer such as the <welcome-file>.

See also:

  • Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP
  • JSP in /WEB-INF returns "HTTP Status 404 The requested resource is not available"



回答2:


Your directory structure should be

/BookShopWeb/|
  |--src
  |---WebContent
                | 
                |---META-INF
                |----WEB-INF---web.xml
  |
  |--css--styles.css
  |--jsp---index.jsp 

Also You named your css as styles.jsp which is not proper way to declare a css file.

In your web.xml:

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

In your jsp file:

<head>
<link rel="stylesheet" type="text/css" href="./css/styles.css" />
</head>



回答3:


I had the same problem; I tried everything and endly I did it by mysealf: I wrote in all JSPs ...

<head>
<style type="test/css">
<%= MYCLASS.getCSS() %>
</style>
</head>

...

And in MYCLASS i created the public static String getCSS() {...}; From the IDE (Eclipse) i created a folder EXTENDING a folder in D:/... where I placed the css. In the function, the JSP executes the function, that reads the CSS with a given path (where u placed the css, like D:/PROJECT/css/SOMETHING.css) and returns it. So the JSP writes the value of MYCLASS.getCSS() in . The forwarded JSP contains the CSS in his style tags :)

It isn't the best way to do it, but it's the only thing that worked for me. I hope i helped u.

Use a BufferedReader to read the CSS file, it's obvious. !!And!! the server must read it ONCE; at the end of the function, save the read CSS in a variable, so u don't need to read it every time someone visits ur page ;)

Code:

static String css = ""; // CSS FILE, ACCESSIBLE FROM ALL THE CLASS.
...
@SuppressWarnings("resource")
public static String getCSS(ENUM e) {
    BufferedReader br;  // BR

    String s = ""; // FINAL STRING
    File f; // THE CSS FILE

    if(css == "") { // READ ONLY IF String CSS (declared first) IS EMPTY
    try
    {

            f = new File(UR_PATH);  // IF DESKTOP
            if(!f.exists()) // IF f DOESN'T EXISTS
                throw new FileNotFoundException("CSS NOT FOUND!");
        }

        br = new BufferedReader(new FileReader(f)); // INIT BR

        System.out.println("READING CSS...");

        //then; useless comment

        try {

            while(true) {
                String cur = br.readLine(); //current line


                if(cur == null) // if cur is null, stop the BR
                    throw new IOException("ENDED CSS! YUPPIE!");

                else // else add cur (current) to s
                    s += cur;
            }
        }
        catch (IOException e1) { // IO IOException (end of CSS)
            System.out.println("CSS READ!");
            try {
                br.close(); // close br
            } catch (IOException e2) { // if CAN'T CLOSE BR... Error
                // TODO Auto-generated catch block
                e2.printStackTrace();
            }
            System.out.println("\n------------------\nThe server read a CSS; Content:\n"+s+"\n------------------\n");
            css = s;
        }


    }
    catch (FileNotFoundException fnfe)
    {
        System.err.println("\n----------\nFATAL ERROR IN \"Property.java\": WRONG CSS PATH");
        System.exit(-1);

    }

    return s;
    }
    // WATCH THE BEGIN. There was if(css == ""); this part of code will be executed if the program already stored the CSS in the String css.
    else {  // IF ALREADY DECLARED, RETURN CSS
        System.out.println("\n--------------------\nRETURNED CSS; ALREADY READ\n----------------------");
        return css;
    }

}


来源:https://stackoverflow.com/questions/54439864/maven-resource-filtering-corrupts-resource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!