Error in include JSP

Asked

Viewed 456 times

8

I have the following situation:

Servlet instantiated at the root

/Minhaservlet

Jsp instantiated in a folder inside the root:

/folder/file.jsp

Inside that file I have the following

<img src="_img/editar.png">
<a href="_global/topoErro.jsp">Editar</a>
<jsp:include page="_global/topoErro.jsp" /> </div>

Note that both the image and the href link are pointing to folders that are at the root of the application but should be with .. / before this file is in /folder as well as /_img and /_global.

My question is why jsp include gives error when using the same way as href if the files that are in href and the file that is in jsp include are the same?

_global,
_img

These are folders that are at the root! And my Servlet is also noted at the root

@WebServlet(
            name="MinhaServlet", 
            urlPatterns={"/MinhaServlet"}
           )

No mention on the web.xml

In my project it looks like this: Here I am having problems to paste. erro404.jsp

<jsp:useBean id="constantes" class="util.Constantes" />     
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>    
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<!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=UTF-8">
<link type="text/css" rel="stylesheet" href="_global/_css/estiloSite.css" />
<title>${constantes.tituloSite}</title>
</head>
<body class="fadeIn">

 <div id="topo"> <jsp:include page="../_global/topoErro.jsp" /> </div>
 <div id="meio"> <jsp:include page="erro404Conteudo.jsp" /> </div>
 <div id="mapa"> <jsp:include page="../_global/mapaErro.jsp" /> </div>
 <div id="creditos"> <jsp:include page="../_global/creditosErro.jsp" /> </div>

 </body>
</html>

erro404Conteud

<div id="central">

        Esta página não existe! <br/>
        Código do Erro : ${statusCode} <br/>
        <img src="_img/editar.png">
        <a href="_global/topoErro.jsp">Editar</a>
        <jsp:include page="_global/topoErro.jsp" />

</div>

2 answers

1

I believe it is best to use absolute paths...

<img src="/SuaAplicação/_img/editar.png">

<a href="/SuaAplicação/_global/topoErro.jsp">Editar</a>

<jsp:include page="/_global/topoErro.jsp" /> </div>

If you do not want to use absolute path to the HTML links of the page just use:

<img src="../_img/editar.png">

<a href="../_global/topoErro.jsp">Editar</a>

But in include:jsp use the path starting only with '/'.

Note: If you do not want to explicitly declare your application name for absolute paths, it can be used ${pageContext.request.contextPath} in your JSP. =)

1

Use the jstl:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<a href="<c:url value="/jsp/index.htm"/>">TEST</a>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.