简体   繁体   English

无法从 java EE 应用程序加载任何资源

[英]Fail to load any resources from java EE application

I have a simple servlet and it works (it forward me to about.jsp file).我有一个简单的 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);
    }
}

This about.jsp file needs a navbar.css to be displayed properly这个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>

But navber.css loading failed.但是navber.css加载失败。 I opened Network tab in browser and see我在浏览器中打开网络选项卡并查看

General:一般的:
Request URL: http://localhost:8080/css/navbar.css请求URL:http://localhost:8080/css/navbar.css
Request Method: GET请求方式:GET
Status Code: 302状态码:302
Remote Address: [::1]:8080远程地址:[::1]:8080
Referrer Policy: strict-origin-when-cross-origin推荐人政策:严格起源时交叉起源

Response Headers:响应标头:
Connection: keep-alive连接:保持活动状态
Content-Length: 0内容长度:0
Date: Wed, 20 Apr 2022 07:20:06 GMT日期:2022 年 4 月 20 日星期三 07:20:06 GMT
Keep-Alive: timeout=20保持活动状态:超时=20
Location: /about位置:/关于

So I have 302 redirect (I don't where this redirect) and I have content-length: 0 for my.css file.所以我有 302 重定向(我没有这个重定向)并且我有 content-length: 0 my.css 文件。 navbar.css located in the webapp/css/navbar.css navbar.css位于 webapp/css/navbar.css
在此处输入图像描述

And the same problem also with images that places in the webapp folder.放置在 webapp 文件夹中的图像也存在同样的问题。 So why I filed to load resources from server and how to make ot work?那么为什么我申请从服务器加载资源以及如何使它工作?

I've found an issue.我发现了一个问题。

@WebServlet("/")
public class MainServlet extends HttpServlet
{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
    {
        response.sendRedirect("/about");
    }
}

This servlet redirects all requests from non servlet URL's to the /about , so when I commented it everything works OK.这个 servlet 将来自非 servlet URL 的所有请求重定向到/about ,所以当我评论它时一切正常。 But I don't understand why it causes this issue and how to redirect user from / to /about但我不明白为什么会导致这个问题以及如何将用户从/重定向到/about

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

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