Jsp and CSS File Not Loading in Browser Java Webapp

JSP - CSS file and JS files aren't loading at all

Fixed it.

I moved the css and js folder to src/main/resoucres/static/ and also I had to remove @EnableMVC from the Application.java and then it started working.

Also I had to add {pageContext.request.contextPath} to index.jsp otherwise it wouldn't work on the server since the URL would be http://localhost/myapp/.

<script src="{pageContext.request.contextPath}/js/jquery.min.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="{pageContext.request.contextPath}/css/bootstrap.min.css">

JSP doesn't see CSS

There are two problems in your app:

  1. In JSP, you should use ${pageContext.request.contextPath} to append the base path for your URLs. With this, you can be sure you will use absolute path instead of relative path for your URLs. So, this:

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

    Will be

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

    This can also be accomplished by using <c:url> from JSTL:

    <link rel="stylesheet" type="text/css" href="<c:url value='/css/style.css' />"/>
  2. Your servlet is mapping to every request made in your app. Note that this includes simple requests for resources like this CSS file. If you're not handling these requests successfully, you may get an error response or an empty response or whatever, depends on how you handle the request for CSS files in the servlet. I recommend you to change the URL pattern for your servlet to map to specific paths instead.


i can't link my css to my jsp in eclipse working on dynamic web project jee

Guys i just reinstalled Eclipse using jdk 8 then i placed my css in a folder named css inside WebContent .
and finally linked my jsp with :

<link rel="stylesheet" href="<c:url value ="/css/loginStyle.css"/>" /> 

Can't access CSS and JavaScript files in JSP

When you are running application then controller calls the .jsp and jsp is get converted to servlet during translation phase so try to add relative path using ${pageContext.request.contextPath}
and once check the url pattern in web.xml and once left it blank and then try it may work.

JSP doesn't see CSS and JS files from resources folder

For JSP project:

Create cssLoader.jsp page inside folder webapp as below:



<link rel="stylesheet" href="resources/stylesheet/common.css"/>
<link rel="stylesheet" href="resources/stylesheet/index.css"/>
<script type="text/javascript" src="resources/js/validators/loginValidator.js"></script>

Then load this cssLoader page where you want to import css/js file in page using <jsp:include page="../../cssLoader.jsp"></jsp:include>.
For example: in my case



<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>WEB-INF INSIDE PAGE</title>

<jsp:include page="../../cssLoader.jsp"></jsp:include>


../../ depends upon folder level structure.

Related Topics

Leave a reply