如何在 java servlet 中 link 外部 css?
How to link external css in java servlet?
我有一个 report.jsp 页面,如下所示(注意:我刚刚添加了必要的代码)。
<form name="report" action="../printOrganization" method="post">
<table>
<tr>
<td>
Organization name: <input type="text" name="orgName" />
</td>
</tr>
<tr>
<td>
<input type="submit" value="Submit" name="action" />
</td>
</tr>
</table>
当用户单击 report.jsp 页面的 "Submit" 按钮时。请求被发送到名为 OrganizationServlet 的 servlet。请求由 doPost 方法处理。 OrganizationServlet 中的代码如下所示:
public class OrganizationServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String organizatio = request.getParameter("orgName");
if (organizatio.equals("ABC")) {
printAllOrganization();
}
}
public void printAllOrganization()
{
PrintWriter pw = response.getWriter();
response.setContentType("text/html");
pw.println("<!DOCTYPE html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n");
pw.println("<html>\n");
pw.println("<head>\n");
pw.println("<title> Print Organization </title>\n");
pw.println("<link rel=\"stylesheet\" type=\"text/css\" HREF=\"../styles/myStyle.css\">\n"); // This style sheet doesn't show effect when program run in browser
pw.println("</head>\n");
pw.println("<body>\n");
//printing all organization code is here!
pw.println("</body>\n");
pw.println("</html>\n");
pw.close();
}
web.xml处理请求的部分如下所示:
<servlet>
<servlet-name>servletForOrganization</servlet-name>
<servlet-class>com.project.report.OrganizationServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servletForOrganization</servlet-name>
<url-pattern>/printOrganization</url-pattern>
</servlet-mapping>
我的 css 文件位于此路径中:
MyProject > resource > styles > myStyle.css
当我在浏览器中 运行 我的应用程序时,OrganizationServlet servlet 的 printAllOrganization() 方法中的 css 样式没有显示任何效果。有人可以帮我解决这个问题吗?提前谢谢你。
添加到您的主页JSP文件中:
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<link rel="stylesheet" href="<c:url value="/resource/styles/myStyle.css" />">
在调度器中添加-servlet.xml:
<mvc:resources mapping="/resource/**" location="/resource/" />
修改你的class:
public class OrganizationServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String organizatio = request.getParameter("orgName");
if (organizatio.equals("ABC")) {
printAllOrganization(request);
}
}
public void printAllOrganization(HttpServletRequest request) {
PrintWriter pw = response.getWriter();
response.setContentType("text/html");
pw.println("<!DOCTYPE html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n");
pw.println("<html>\n");
pw.println("<head>\n");
pw.println("<title> Print Organization </title>\n");
pw.println("<link rel=\"stylesheet\" type=\"text/css\" HREF=\"" + request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/resource/styles/myStyle.css\">\n");
pw.println("</head>\n");
pw.println("<body>\n");
//printing all organization code is here!
pw.println("</body>\n");
pw.println("</html>\n");
pw.close();
}
我有一个 report.jsp 页面,如下所示(注意:我刚刚添加了必要的代码)。
<form name="report" action="../printOrganization" method="post">
<table>
<tr>
<td>
Organization name: <input type="text" name="orgName" />
</td>
</tr>
<tr>
<td>
<input type="submit" value="Submit" name="action" />
</td>
</tr>
</table>
当用户单击 report.jsp 页面的 "Submit" 按钮时。请求被发送到名为 OrganizationServlet 的 servlet。请求由 doPost 方法处理。 OrganizationServlet 中的代码如下所示:
public class OrganizationServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String organizatio = request.getParameter("orgName");
if (organizatio.equals("ABC")) {
printAllOrganization();
}
}
public void printAllOrganization()
{
PrintWriter pw = response.getWriter();
response.setContentType("text/html");
pw.println("<!DOCTYPE html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n");
pw.println("<html>\n");
pw.println("<head>\n");
pw.println("<title> Print Organization </title>\n");
pw.println("<link rel=\"stylesheet\" type=\"text/css\" HREF=\"../styles/myStyle.css\">\n"); // This style sheet doesn't show effect when program run in browser
pw.println("</head>\n");
pw.println("<body>\n");
//printing all organization code is here!
pw.println("</body>\n");
pw.println("</html>\n");
pw.close();
}
web.xml处理请求的部分如下所示:
<servlet>
<servlet-name>servletForOrganization</servlet-name>
<servlet-class>com.project.report.OrganizationServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servletForOrganization</servlet-name>
<url-pattern>/printOrganization</url-pattern>
</servlet-mapping>
我的 css 文件位于此路径中:
MyProject > resource > styles > myStyle.css
当我在浏览器中 运行 我的应用程序时,OrganizationServlet servlet 的 printAllOrganization() 方法中的 css 样式没有显示任何效果。有人可以帮我解决这个问题吗?提前谢谢你。
添加到您的主页JSP文件中:
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<link rel="stylesheet" href="<c:url value="/resource/styles/myStyle.css" />">
在调度器中添加-servlet.xml:
<mvc:resources mapping="/resource/**" location="/resource/" />
修改你的class:
public class OrganizationServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String organizatio = request.getParameter("orgName");
if (organizatio.equals("ABC")) {
printAllOrganization(request);
}
}
public void printAllOrganization(HttpServletRequest request) {
PrintWriter pw = response.getWriter();
response.setContentType("text/html");
pw.println("<!DOCTYPE html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n");
pw.println("<html>\n");
pw.println("<head>\n");
pw.println("<title> Print Organization </title>\n");
pw.println("<link rel=\"stylesheet\" type=\"text/css\" HREF=\"" + request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/resource/styles/myStyle.css\">\n");
pw.println("</head>\n");
pw.println("<body>\n");
//printing all organization code is here!
pw.println("</body>\n");
pw.println("</html>\n");
pw.close();
}