Home > OS >  Fail to load any resources from java EE application
Fail to load any resources from java EE application

Time:04-25

I have a simple servlet and it works (it forward me to about.jsp file).

@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

<%@ 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" >About</a></li>
                <li><a href="/catalog" >Catalog</a></li>
            </ul>
        </nav>   
 
    </body>
</html>

But navber.css loading failed. I opened Network tab in browser and see

General:
Request URL: http://localhost:8080/css/navbar.css
Request Method: GET
Status Code: 302
Remote Address: [::1]:8080
Referrer Policy: strict-origin-when-cross-origin

Response Headers:
Connection: keep-alive
Content-Length: 0
Date: Wed, 20 Apr 2022 07:20:06 GMT
Keep-Alive: timeout=20
Location: /about

So I have 302 redirect (I don't where this redirect) and I have content-length: 0 for my .css file. navbar.css located in the webapp/css/navbar.css
enter image description here

And the same problem also with images that places in the webapp folder. So why I filed to load resources from server and how to make ot work?

CodePudding user response:

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. But I don't understand why it causes this issue and how to redirect user from / to /about

  • Related