2

After a lot of looking around on various forums and such, I was not able to find an answer to my question.

I want to attach a stylesheet to my servlet instead of having to use <style> tags.

I am using Apache Tomcat 7 in Eclipse and I am manually writing the html code (via a PrintWriter).

I've tried putting the .css file in the ROOT of the WebApp. I've tried putting it in the css. Nothing is working.

Can someone point me in the right direction?

Here is some code that I tried.

Attempt 1 (css is in a folder. WebContent/css:

    String cssLocation = request.getContextPath() + "/WebContent/css/styles2.css";
    String cssTag = "<link rel='stylesheet' type='text/css' href='" + cssLocation + "'>";

Attempt 2 (css is in the ROOT):

    String cssLocation = request.getContextPath() + "/styles2.css";
    String cssTag = "<link rel='stylesheet' type='text/css' href='" + cssLocation + "'>";

Neither of those worked.

EDIT: Here is my directory structure:

PROJECT ROOT
    src
        testPackage
            DownloadServlet.java
    WebContent
        css
            styles2.css
        files
        fonts
        js
        META-INF
        WEB-INF
        index.html

To explain: I am trying to reference /WebContent/css/styles2.css in DownloadServlet.java

How I am doing that:

In the method 'doGet', I am initializing a `PrintWriter'. I'm printing out:

<html>
<head>
   HERE IS WHERE THE LINK NEEDS TO GO
</head>
<body>
...
</body>
</html>

Where the text "HERE IS WHERE THE LINK NEEDS TO GO" is, that is where I need the link to the css file. I've tried the methods above, but I had no luck.

KyleCrowley
  • 900
  • 6
  • 14
  • 31
  • Why not write a JSP file and include the CSS file using `` there? – Luiggi Mendoza May 13 '14 at 16:08
  • Some context might be nice. This really isn't a lot to go on. As far as I can tell, both those should work. Could we get a little more code around it or the page source when you go to the page in the browser? – RyNo May 13 '14 at 16:13
  • This is a simple matter of figuring out what the proper path is relative to your serlvet. Which we can't do since you didn't tell us your folder structure or servlet mapping. – developerwjk May 13 '14 at 17:42
  • I made edits to the post. Hopefully that clears it up more. @KamikazeScotsman – KyleCrowley May 13 '14 at 18:25

2 Answers2

2

Just a guess: Try String cssTag = "<link rel='stylesheet' type='text/css' href='/css/styles.css'>"; The browser will look for the css file in the sub-folder of the root directory of the server, which is in your case the WebContent-directory. You usually don't need to call request.getContextPath() when linking resources inside HTML tags.

user2610529
  • 491
  • 3
  • 10
1

First you create the css file, suppose style.css in the folder name css inside the WebContent directory of your project.

Then, you must know the tomcat server path where the .css file is located.

String cssTag="<link rel='stylesheet' type='text/css' href='css/style.css'>"
    PrintWriter out = res.getWriter();
    out.println("<html>");
    out.println("<head><title>Title Name</title>"+cssTag+"</head>");
    out.println("<body>");
           /*

           Your  code 
           */
    out.println("</body></html>")
susan097
  • 3,500
  • 1
  • 23
  • 30