title: Attaching a CSS file to a Java Servlet in Tomcat
body: <p>After a lot of looking around on various forums and such, I was not able to find an answer to my question.</p>
  
  <p>I want to attach a stylesheet to my servlet instead of having to use <code>&lt;style&gt;</code> tags.</p>
  
  <p>I am using Apache Tomcat 7 in Eclipse and I am manually writing the html code (via a PrintWriter). </p>
  
  <p>I've tried putting the <code>.css</code> file in the ROOT of the WebApp. I've tried putting it in the css. Nothing is working. </p>
  
  <p>Can someone point me in the right direction?</p>
  
  <p>Here is some code that I tried.</p>
  
  <p>Attempt 1 (css is in a folder. WebContent/css:</p>
  
  <pre><code>    String cssLocation = request.getContextPath() + "/WebContent/css/styles2.css";
      String cssTag = "&lt;link rel='stylesheet' type='text/css' href='" + cssLocation + "'&gt;";
  </code></pre>
  
  <p>Attempt 2 (css is in the ROOT):</p>
  
  <pre><code>    String cssLocation = request.getContextPath() + "/styles2.css";
      String cssTag = "&lt;link rel='stylesheet' type='text/css' href='" + cssLocation + "'&gt;";
  </code></pre>
  
  <p>Neither of those worked. </p>
  
  <p>EDIT: Here is my directory structure:</p>
  
  <pre><code>PROJECT ROOT
      src
          testPackage
              DownloadServlet.java
      WebContent
          css
              styles2.css
          files
          fonts
          js
          META-INF
          WEB-INF
          index.html
  </code></pre>
  
  <p>To explain: I am trying to reference <code>/WebContent/css/styles2.css</code> in <code>DownloadServlet.java</code></p>
  
  <p>How I am doing that:</p>
  
  <p>In the method 'doGet', I am initializing a `PrintWriter'. I'm printing out:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
     HERE IS WHERE THE LINK NEEDS TO GO
  &lt;/head&gt;
  &lt;body&gt;
  ...
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>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. </p>