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.

4
  • Why not write a JSP file and include the CSS file using <link> there? Commented May 13, 2014 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? Commented May 13, 2014 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. Commented May 13, 2014 at 17:42
  • I made edits to the post. Hopefully that clears it up more. @KamikazeScotsman Commented May 13, 2014 at 18:25

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

But how to include the cssTag string in the Servlet Class.
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>")

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.