我有一個簡單的 servlet,它可以作業(它轉發給我about.jsp檔案)。
@WebServlet("/about")
public class AboutServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
RequestDispatcher dispatcher = request.getRequestDispatcher("/about.jsp");
dispatcher.forward(request, response);
}
}
此about.jsp檔案需要navbar.css正確顯示
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta charset="UTF-8">
<title>Main</title>
<link href="${pageContext.request.contextPath}css/navbar.css" rel="stylesheet" type="text/css">
</head>
<body>
<header>
About
</header>
<nav>
<ul>
<li><a href="/about" class="headerLink current-category">About</a></li>
<li><a href="/catalog" class="headerLink">Catalog</a></li>
</ul>
</nav>
</body>
</html>
但是navber.css加載失敗。我在瀏覽器中打開網路選項卡并查看
常規:
請求 URL:http://localhost:8080/css/navbar.css
請求方法:GET
狀態代碼:302
遠程地址:[::1]:8080
推薦人策略:strict-origin -when-cross-origin
回應標頭:
連接:keep-alive
內容長度:0
日期:2022 年 4 月 20 日星期三 07:20:06 GMT
Keep-Alive:timeout=20
位置:/about
所以我有 302 重定向(我不要重定向到哪里)并且我的 .css 檔案有 content-length: 0 。navbar.css位于 webapp/css/navbar.css

放置在 webapp 檔案夾中的影像也存在同樣的問題。那么為什么我申請從服務器加載資源以及如何使 ot 作業?
uj5u.com熱心網友回復:
我發現了一個問題。
@WebServlet("/")
public class MainServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
{
response.sendRedirect("/about");
}
}
這個 servlet 將所有請求從非 servlet URL 重定向到/about,所以當我評論它時,一切正常。但我不明白為什么會導致這個問題以及如何將用戶重定向/到/about
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/464728.html
