将 CSS 文件附加到 Tomcat 中的 Java Servlet
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23636304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Attaching a CSS file to a Java Servlet in Tomcat
提问by KyleCrowley
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.
我想将样式表附加到我的 servlet 而不是必须使用<style>
标签。
I am using Apache Tomcat 7 in Eclipse and I am manually writing the html code (via a PrintWriter).
我在 Eclipse 中使用 Apache Tomcat 7,我正在手动编写 html 代码(通过 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.
我试过将.css
文件放在 WebApp 的 ROOT 中。我试过把它放在css中。没有任何工作。
Can someone point me in the right direction?
有人可以指出我正确的方向吗?
Here is some code that I tried.
这是我尝试过的一些代码。
Attempt 1 (css is in a folder. WebContent/css:
尝试 1(css 在一个文件夹中。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):
尝试 2(css 在 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
为了解释:我想引用/WebContent/css/styles2.css
在DownloadServlet.java
How I am doing that:
我是怎么做的:
In the method 'doGet', I am initializing a `PrintWriter'. I'm printing out:
在方法“doGet”中,我正在初始化一个“PrintWriter”。我正在打印:
<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.
文本“这里是链接需要去的地方”的位置,这就是我需要 css 文件链接的地方。我已经尝试了上面的方法,但我没有运气。
回答by user2610529
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.
只是一个猜测:尝试String cssTag = "<link rel='stylesheet' type='text/css' href='/css/styles.css'>";
浏览器将在服务器根目录的子文件夹中查找 css 文件,在您的情况下是 WebContent 目录。在链接 HTML 标签内的资源时,您通常不需要调用 request.getContextPath()。
回答by susan097
First you create the css file, suppose style.css
in the folder name css
inside the WebContent directory of your project.
首先创建 css 文件,假设style.css
在css
项目的 WebContent 目录内的文件夹名称中。
Then, you must know the tomcat server
path where the .css
file is located.
然后,您必须知道文件tomcat server
所在的路径.css
。
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>")