1

I'm trying to put CSS, JS and IMGs on my JSP, but doesn't work, it's strange because I use c:url and JSTL core.

I'm using JSP, JSTL and Servlet, no frameworks.

structure

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html>
<html>    
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>clientes atuantes</title>
        <link rel="stylesheet" href="<c:url value="/bootstrap/css/bootstrap.min.css"/>">
        <link rel="stylesheet" href="<c:url value="/css/layout.css"/>">
        <script type="text/javascript" src="<c:url value="/bootstrap/js/jquery-1.12.0.js" />"></script>
        <script type="text/javascript" src="<c:url value="/bootstrap/js/bootstrap.min.js" />"></script>
    </head>
    <body>

        <main>
            <div class="col-md-12">
                <div class="panel panel-default espacamentoExterno5">

                    <div class="panel-body">
                        <p><img src="<c:url value="/img/ativo.gif"/>" /> ativo</p>
                        <p><img src="<c:url value="/img/congelado.gif"/>" /> congelado</p>
                        <p><img src="<c:url value="/img/cancelado.gif"/>" /> cancelado</p>

                    </div>
                </div>
            </div>
        </main>
    </body>
</html>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns:web="http://java.sun.com/xml/ns/javaee" 
         xmlns="http://java.sun.com/xml/ns/javaee" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
    
  <display-name>PainelProduct</display-name>
  

</web-app>
19
  • are you phasing any error ? if yes then please post here. Commented Mar 1, 2016 at 15:33
  • Show the output of your code. Commented Mar 1, 2016 at 15:34
  • any error, only doesn't find images, css, js Commented Mar 1, 2016 at 15:36
  • check properly, whether you have make available proper jar(jstl) on classpath or not ? Commented Mar 1, 2016 at 15:37
  • may i know you img directory location into project-structure...? Commented Mar 1, 2016 at 16:00

2 Answers 2

1

I have encountered the same problem and have not resolved it for a long time. Then I realized that it was a problem from the path of the images.

Wrong path is like that;

src>main>resources>images

The order of files(path) should definitely be as follows.

src>main>webapp>resources>images

and you should to call the function like that;

<img src="<c:url value="/resources/img/logo.png"/>" alt=""/>
Sign up to request clarification or add additional context in comments.

Comments

0

The url pattern "/" has a special meaning to the web server. It's used for default servlet mapping which is capable of serving the static content. The static content is JS, CSS, images, html resources. Directing these files to your servlet is wrong, because it's not capable to serve the static content.

Change the servlet mapping to something @WebServlet(urlPatterns="/incidente")

and use url localhost:8080/PainelProduct/incidente to forward to jsp page.

Check this question to learn more about servlets and servlet mapping Mapping a specific servlet to be the default servlet in Tomcat.

2 Comments

could I do more one question? how can I put "/incidente" like first page?
make this page to forward to this "/incedente", when you forward the url remains the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.