Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9210055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:59:02+00:00 2026-06-18T00:59:02+00:00

I added an external CSS stylesheet to my project and placed in the WEB-CONTENTS

  • 0

I added an external CSS stylesheet to my project and placed in the WEB-CONTENTS folder of my project in Eclipse. When I deployed it on the Tomcat, the stylesheet was not applied. When I debugged it in Chrome and opened it, it gave me 404 file not found error. Why is that and how to fix it?

Here is the code:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>joined now </title>

<link href="globalCSS.css" rel="stylesheet" type="text/css"/>

</head>
<body>
<div>this is at the top</div>
<c:import url="header.jsp" />
<c:import url="navigationBar.jsp" />  
<c:import url="leftpane.jsp" /> 
<c:import url="mainContent.jsp" /> 
<c:import url="rightpane.jsp" />
<c:import url="footer.jsp" />  
</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:59:03+00:00Added an answer on June 18, 2026 at 12:59 am

    The reason that you get the 404 File Not Found error, is that your path to CSS given as a value to the href attribute is missing context path.

    An HTTP request URL contains the following parts:

    http://[host]:[port][request-path]?[query-string]
    

    The request path is further composed of the following elements:

    • Context path: A concatenation of a forward slash (/) with the context
      root
      of the servlet’s web application. Example: http://host[:port]/context-root[/url-pattern]

    • Servlet path: The path section that corresponds to the component
      alias that activated this request. This path starts with a forward
      slash (/).

    • Path info: The part of the request path that is not part of the
      context path or the servlet path.

    Read more here.


    Solutions

    There are several solutions to your problem, here are some of them:

    1) Using <c:url> tag from JSTL

    In my Java web applications I usually used <c:url> tag from JSTL when defining the path to CSS/JavaScript/image and other static resources. By doing so you can be sure that those resources are referenced always relative to the application context (context path).

    If you say, that your CSS is located inside WebContent folder, then this should work:

    <link type="text/css" rel="stylesheet" href="<c:url value="/globalCSS.css" />" />
    

    The reason why it works is explained in the “JavaServer Pages™ Standard Tag Library” version 1.2 specification chapter 7.5 (emphasis mine):

    7.5 <c:url>
    Builds a URL with the proper rewriting rules applied.
    …
    The URL must be either an absolute URL
    starting with a scheme (e.g. “http:// server/context/page.jsp”) or a
    relative URL as defined by JSP 1.2 in JSP.2.2.1 “Relative URL
    Specification”. As a consequence, an implementation must prepend the
    context path to a URL that starts with a slash
    (e.g. “/page2.jsp”) so
    that such URLs can be properly interpreted by a client browser.

    NOTE
    Don’t forget to use Taglib directive in your JSP to be able to reference JSTL tags. Also see an example JSP page here.

    2) Using JSP Expression Language and implicit objects

    An alternative solution is using Expression Language (EL) to add application context:

    <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/globalCSS.css" />
    

    Here we have retrieved the context path from the request object. And to access the request object we have used the pageContext implicit object.

    3) Using <c:set> tag from JSTL

    DISCLAIMER
    The idea of this solution was taken from here.

    To make accessing the context path more compact than in the solution №2, you can first use the JSTL <c:set> tag, that sets the value of an EL variable or the property of an EL variable in any of the JSP scopes (page, request, session, or application) for later access.

    <c:set var="root" value="${pageContext.request.contextPath}"/>
    ...
    <link type="text/css" rel="stylesheet" href="${root}/globalCSS.css" />
    

    IMPORTANT NOTE
    By default, in order to set the variable in such manner, the JSP that contains this set tag must be accessed at least once (including in case of setting the value in the application scope using scope attribute, like <c:set var="foo" value="bar" scope="application" />), before using this new variable. For instance, you can have several JSP files where you need this variable. So you must ether a) both set the new variable holding context path in the application scope AND access this JSP first, before using this variable in other JSP files, or b) set this context path holding variable in EVERY JSP file, where you need to access to it.

    4) Using ServletContextListener

    The more effective way to make accessing the context path more compact is to set a variable that will hold the context path and store it in the application scope using a Listener. This solution is similar to solution №3, but the benefit is that now the variable holding context path is set right at the start of the web application and is available application wide, no need for additional steps.

    We need a class that implements ServletContextListener interface. Here is an example of such class:

    package com.example.listener;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    import javax.servlet.annotation.WebListener;
    
    @WebListener
    public class AppContextListener implements ServletContextListener {
    
        @Override
        public void contextInitialized(ServletContextEvent event) {
            ServletContext sc = event.getServletContext();
            sc.setAttribute("ctx", sc.getContextPath());
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent event) {}
    
    }
    

    Now in a JSP we can access this global variable using EL:

    <link type="text/css" rel="stylesheet" href="${ctx}/globalCSS.css" />
    

    NOTE
    @WebListener annotation is available since Servlet version 3.0. If you use a servlet container or application server that supports older Servlet specifications, remove the @WebServlet annotation and instead configure the listener in the deployment descriptor (web.xml). Here is an example of web.xml file for the container that supports maximum Servlet version 2.5 (other configurations are omitted for the sake of brevity):

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                            http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        version="2.5">
        ...  
        <listener>
            <listener-class>com.example.listener.AppContextListener</listener-class>
        </listener>
        ...
    </webapp>
    

    5) Using scriptlets

    As suggested by user @gavenkoa you can also use scriptlets like this:

    <%= request.getContextPath() %>
    

    For such a small thing it is probably OK, just note that generally the use of scriptlets in JSP is discouraged.

    Conclusion

    I personally prefer either the first solution (used it in my previous projects most of the time) or the second, as they are most clear, intuitive and unambiguous (IMHO). But you choose whatever suits you most.


    Other thoughts

    You can deploy your web app as the default application (i.e. in the default root context), so it can be accessed without specifying context path. For more info read the “Update” section here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I added an external stylesheet file (css) to my flex project. Is there a
I'm have added external jar file to the libs folder of mt project and
I have added an external .exe file into my VS2010 setup project and I'd
i'm using filemtime for fingerprinting external resources in html, like: <link rel=stylesheet href=screen-<?=md5(filemtime('screen.css'));?>.css> I
In my java project, i have added some external jars. I want to update
I have added some external libraries to my java project (in netbeans). Is it
I added an external Data Source to my C# app (I've placed it on
In VS2010 they added to all projects a virtual directory called External Dependencies: alt
I work on developing an external API. I added a method to my public
I added custom named styles to the app.xaml. I created an external resource dictionary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.