Servlets 我可以在servlet编码中使用CSS吗?

Servlets 我可以在servlet编码中使用CSS吗?,servlets,Servlets,有没有一种方法可以在servlet编码中使用CSS?如果您想让它相对于上下文路径,也可以将链接编写为 <LINK REL="StyleSheet" HREF="<%=request.getContextPath()%>/util/CSS/Style.css" TYPE="text/css"> 其中,/util/CSS是上下文路径下的文件夹(例如,典型Tomcat设置中的/webapp/examples) 但是,您必须确保准确键入整个路径(即区分大小写) 希望这个额

有没有一种方法可以在servlet编码中使用CSS?

如果您想让它相对于上下文路径,也可以将链接编写为

<LINK REL="StyleSheet" HREF="<%=request.getContextPath()%>/util/CSS/Style.css" TYPE="text/css"> 

其中,/util/CSS是上下文路径下的文件夹(例如,典型Tomcat设置中的/webapp/examples)

但是,您必须确保准确键入整个路径(即区分大小写)


希望这个额外的澄清能有所帮助。

如果你想让它相对于上下文路径,你也可以将链接写为

<LINK REL="StyleSheet" HREF="<%=request.getContextPath()%>/util/CSS/Style.css" TYPE="text/css"> 

其中,/util/CSS是上下文路径下的文件夹(例如,典型Tomcat设置中的/webapp/examples)

但是,您必须确保准确键入整个路径(即区分大小写)


希望这个额外的澄清能有所帮助。

是的!有一种在servlet中使用css的方法。这可以通过以下方式实现:;首先创建PrintWriter类的对象,
“PrintWriter out=response.getWriter();”
在此步骤之后,您可以使用“
out.print({}”)”在花括号内,您可以编写ID或类的代码。在
“out.print()”
之后或之前,您可以编写servlet代码。不要忘记关闭
和其他标签。

是!有一种在servlet中使用css的方法。这可以通过以下方式实现:;首先创建PrintWriter类的对象,
“PrintWriter out=response.getWriter();”
在此步骤之后,您可以使用“
out.print({}”)”在花括号内,您可以编写ID或类的代码。在
“out.print()”
之后或之前,您可以编写servlet代码。不要忘记关闭
和其他标记。

CSS可以通过直接在生成的HTML中包含样式在Servlet中使用:

try (PrintWriter out = response.getWriter()) {
  out.println("<!DOCTYPE html>");
  out.println("<html>");
  out.println("<head>");
  out.println("<meta charset=\"utf-8\">");  // escape the quote marks
  out.println("<title>Glassfish HTML Testing</title>");
  out.println("<style>");     // start style
  // enclose style attributes withing the <style> </style> elements
  out.println("h1 {");        // note leading brace
  out.println("color:blue;");
  out.println("background-color:yellow;");
  out.println("border: 1px solid black;");
  out.println("}");          // note trailing brace for h1 style
  // add styles for other elements here using similar structure
  // note that separate lines are used for clarity -
  // all of the above could be one println
  out.println("</style>");  // terminate style
  out.println("</head>");
  out.println("<body>");
  out.println("<h1>Servlet located at " + request.getContextPath() + "</h1>");
  out.println("</body>");
  out.println("</html>");
}  // end of try-with-resources block
try(PrintWriter out=response.getWriter()){
out.println(“”);
out.println(“”);
out.println(“”);
out.println(“”;//转义引号
out.println(“Glassfish HTML测试”);
out.println(“”;//开始样式
//将样式属性包含在元素中
out.println(“h1{”);//注意前面的大括号
out.println(“颜色:蓝色;”);
out.println(“背景色:黄色;”);
out.println(“边框:1px纯黑;”);
out.println(“}”);//注意h1样式的尾随大括号
//使用类似的结构在此处添加其他元素的样式
//请注意,为了清晰起见,使用了单独的行-
//以上所有内容都可以是一个println
out.println(“”;//终止样式
out.println(“”);
out.println(“”);
println(“位于“+request.getContextPath()+”的Servlet);
out.println(“”);
out.println(“”);
}//使用资源块重试结束

上述代码应该在Servlet的processRequest方法中(假设生成的页面适合GET和POST请求)

CSS可以通过直接在生成的HTML中包含样式在Servlet中使用:

try (PrintWriter out = response.getWriter()) {
  out.println("<!DOCTYPE html>");
  out.println("<html>");
  out.println("<head>");
  out.println("<meta charset=\"utf-8\">");  // escape the quote marks
  out.println("<title>Glassfish HTML Testing</title>");
  out.println("<style>");     // start style
  // enclose style attributes withing the <style> </style> elements
  out.println("h1 {");        // note leading brace
  out.println("color:blue;");
  out.println("background-color:yellow;");
  out.println("border: 1px solid black;");
  out.println("}");          // note trailing brace for h1 style
  // add styles for other elements here using similar structure
  // note that separate lines are used for clarity -
  // all of the above could be one println
  out.println("</style>");  // terminate style
  out.println("</head>");
  out.println("<body>");
  out.println("<h1>Servlet located at " + request.getContextPath() + "</h1>");
  out.println("</body>");
  out.println("</html>");
}  // end of try-with-resources block
try(PrintWriter out=response.getWriter()){
out.println(“”);
out.println(“”);
out.println(“”);
out.println(“”;//转义引号
out.println(“Glassfish HTML测试”);
out.println(“”;//开始样式
//将样式属性包含在元素中
out.println(“h1{”);//注意前面的大括号
out.println(“颜色:蓝色;”);
out.println(“背景色:黄色;”);
out.println(“边框:1px纯黑;”);
out.println(“}”);//注意h1样式的尾随大括号
//使用类似的结构在此处添加其他元素的样式
//请注意,为了清晰起见,使用了单独的行-
//以上所有内容都可以是一个println
out.println(“”;//终止样式
out.println(“”);
out.println(“”);
println(“位于“+request.getContextPath()+”的Servlet);
out.println(“”);
out.println(“”);
}//使用资源块重试结束

上面的代码应该在Servlet的processRequest方法中(假设生成的页面适合GET和POST请求)

您确实应该发布一些代码。很难理解“从HTML页面调用servlet”是什么意思。这是JSP吗?“CSS代替HTML”是什么意思?servlet生成的HTML页面是普通HTML。在正常情况下,它必须在标题中链接到css样式表,就像每个html页面一样。@Raj:你的编辑并没有让问题更容易理解。请试着解释你在做什么,而不仅仅是删除部分。你真的应该发布一些代码。很难理解“从HTML页面调用servlet”是什么意思。这是JSP吗?“CSS代替HTML”是什么意思?servlet生成的HTML页面是普通HTML。在正常情况下,它必须在标题中链接到css样式表,就像每个html页面一样。@Raj:你的编辑并没有让问题更容易理解。请试着解释你在做什么,而不仅仅是删除部分。请详细说明1)我需要在哪里写链接标签。。它是一个额外的文件还是在servlet程序本身中?2) 我在eclipse oxygen中使用tomcat v8.5,那么在tomcat文件夹中的CSS文件夹在哪里设置?@amod0017请详细说明1)我需要在哪里编写链接标签。。它是一个额外的文件还是在servlet程序本身中?2) 我在eclipse oxygen中使用tomcat v8.5,那么在tomcat文件夹中的CSS文件夹应该设置在哪里?@amod0017I尝试过,但没有在输出中显示正确的设计和仅显示样式CSS代码。我尝试过,但没有在输出中显示正确的设计和仅显示样式CSS代码