简体   繁体   English

使用Servlet在Java Web应用程序中渲染CSS和JS

[英]Rendering css and js in a Java web application using servlet

project structure 项目结构

In intellij IDEA, I started a new Java Web application and I am using servlets to handle GET and POST requests, now the problem comes in where I introduce CSS and JS in my JSP pages, they seem not to be rendered/ pulled from the sources and when I inspect the code in browser and click on the href or src links, I am redirected to the servlet error page, with this code 在intellij IDEA中,我启动了一个新的Java Web应用程序,并使用servlet处理GET和POST请求,现在问题出在我在JSP页面中引入CSS和JS时,它们似乎没有从源代码中呈现/提取。当我在浏览器中检查代码并单击href或src链接时,使用此代码将我重定向到servlet错误页面

<!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">h1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} h2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} h3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} body {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} b {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} p {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;} a {color:black;} a.name {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Message</b> &#47;post&#47;MIS&#47;assets&#47;css&#47;contracts-list.jsp</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/8.5.43</h3></body></html>

from my research I changed my links from 从我的研究中,我改变了我的链接

<link href="assets/css/style.css" rel="stylesheet">

to

<link href="${pageContext.request.contextPath}/assets/css/style.css" rel="stylesheet">

But the same problem (redirect to servlet errror page) still persists 但是,同样的问题(重定向到servlet错误页面)仍然存在

<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css">
    <!-- Bootstrap core CSS -->
    <link rel="stylesheet" href="${pageContext.request.contextPath}/assets/css/bootstrap.min.css">
    <!-- Material Design Bootstrap -->
    <link href="${pageContext.request.contextPath}/assets/css/mdb.min.css" rel="stylesheet">
    <!-- Your custom styles (optional) -->
    <link href="${pageContext.request.contextPath}/assets/css/style.css" rel="stylesheet">

    <!-- MDBootstrap Datatables  -->
    <link href="${pageContext.request.contextPath}/assets/css/addons/datatables.min.css" rel="stylesheet">
<script type="text/javascript" src="${pageContext.request.contextPath}/assets/js/jquery-3.4.1.min.js"></script>
<!-- Bootstrap tooltips -->
<script type="text/javascript" src="${pageContext.request.contextPath}/assets/js/popper.min.js"></script>
<!-- Bootstrap core JavaScript -->
<script type="text/javascript" src="${pageContext.request.contextPath}/assets/js/bootstrap.min.js"></script>
<!-- MDB core JavaScript -->
<script type="text/javascript" src="${pageContext.request.contextPath}/assets/js/mdb.min.js"></script>
<!-- MDBootstrap Datatables  -->
<script type="text/javascript" src="${pageContext.request.contextPath}/assets/js/addons/datatables.min.js"></script>

I expect the page to render linked css and JS files but this redirects to servlet error page 我希望页面能够呈现链接的CSS和JS文件,但这会重定向到servlet错误页面

By Analysing Your error, 通过分析您的错误,

HTTP Status 404 – Not Found ****** /post/MIS/assets/css/contracts-list.jsp HTTP状态404 –未找到****** /post/MIS/assets/css/contracts-list.jsp

I got your problem, 我有你的问题
1. You are not reading error messages sent by your intellij server. 1.您没有在阅读由intellij服务器发送的错误消息。
2. Two mistakes in URL. 2. URL中的两个错误。 a. 一种。 context path /post/MIS but instead it should be /MIS b. 上下文路径/ post / MIS,但是应该是/ MIS b。 assets/css/contract-list.jsp, I guess you are doing mistake while including your JSP(forwarding to JSP or redirecting to JSP). 资产/css/contract-list.jsp,我想您在包含JSP(转发到JSP或重定向到JSP)时出错。

There is nothing relation to css path to your 404 error. 与您的404错误的CSS路径没有关系。 As css and js files loads after your jsp loads. 在载入jsp之后,载入css和js文件。 Main entry only 404. Fix including contract-list.jsp, Rest of the things will work definitely. 主条目只有404。修复了包括contract-list.jsp在内的问题,其余的事情肯定会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM