0

I am using the following code to dynamically add css files to my web project. However, when I write out the absolute path to the css file, it return something outside my web project. For example, for testing, I have added css/style.css to the cssAssets ArrayList. When I check the condition of f.isFile(), it writes out home/*username*/css/styles.css instead of the relative path from the index.jsp file to the css directory. It should be going to workspace/projectName/WebContent/css/styles.css. Any help is appreciated.

public String buildHead()
{
    htmlHead = new StringBuilder();
    htmlHead.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">");
    htmlHead.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
    htmlHead.append("<meta http-equiv=\"cache-control\" content=\"max-age=0\" />");
    htmlHead.append("<meta http-equiv=\"cache-control\" content=\"no-cache\" />");
    htmlHead.append("<meta http-equiv=\"expires\" content=\"Tue, 01 Jan 1980 1:00:00 GMT\" />");
    htmlHead.append("<meta http-equiv=\"pragma\" content=\"no-cache\" />");
    if (this.title.length() > 0)
        htmlHead.append("<title>" + this.title + "</title>");
    else
        htmlHead.append("<title>MMJ Ceo </title>");

    for (String css : this.cssAssets)
    {
        File f = new File(css);

        if (f.isFile())
        {
            htmlHead.append("<link rel='stylesheet' href=\"" + css + "\"/>");
        }
        else
            return f.getAbsolutePath();
    }
    for(String js : this.jsAssets)
    {
        File f = new File(js);
        if(f.isFile())
            htmlHead.append("<script type='text/javascript' src='" + js + "'></script>");
    }
    htmlHead.append("</head>");
    return htmlHead.toString();
}

Update

This code snippet is from my Page class located in the projectName.src.com.projectName.view, the css files are located in projectName/WebContent/css/style.css, and it is called from projectName/WebContent/index.jsp

Mike
  • 1,718
  • 3
  • 30
  • 58

1 Answers1

1

Q: What IDE are you using? It sounds like Eclipse, in which case, "Yes: you should put your css/ directory under "WebContent/" (just as your "index.jsp" is also under "WebContent/").

... BUT ....

The root path of your running servlet is different from the actual, physical path on your web server. Look here:

Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • I am using Eclipse. Thanks for pointing me in the right direction. I am going to run a quick test with what you suggested. – Mike Mar 02 '13 at 22:58