I'm studying java for web (EE) and would like to know if you could help solve a java problem!
I have in my web.xml a call to the file erro404.jsp that is in the root of the site to filter pages that do not exist!
It turns out that if the nonexistent page called is in the root also, for example /site/pageInexiste.jsp, it will be at the same level as error 404.jsp and when error404.jsp is called, all your internal links (css, includes [code]) will work.
However, when the Non-existent Page is inside any folder of the root, ie outside the error level 404.jsp, example /site/pasta/paginaInexistente.jsp
, then error404.jsp is usually called but its internal links do not work.
I tried to send 404 errors to a Servlet and to make RequestDispatcher
to call error404.jsp because both Servlet and erro404.jsp are on the same level, but it did not work!
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>imwsaogotardo</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>AdminServlet</servlet-name>
<servlet-class>imwsaogotardo.src.controller.AdminServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AdminServlet</servlet-name>
<url-pattern>/imwsaogotardo/AdminServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ProfessorServlet</servlet-name>
<servlet-class>imwsaogotardo.src.controller.ProfessorServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ProfessorServlet</servlet-name>
<url-pattern>/imwsaogotardo/ProfessorServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>SalaServlet</servlet-name>
<servlet-class>imwsaogotardo.src.controller.SalaServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SalaServlet</servlet-name>
<url-pattern>/imwsaogotardo/SalaServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>UsuarioServlet</servlet-name>
<servlet-class>imwsaogotardo.src.controller.UsuarioServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UsuarioServlet</servlet-name>
<url-pattern>/imwsaogotardo/UsuarioServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>PerguntaServlet</servlet-name>
<servlet-class>imwsaogotardo.src.dao.pergunta.PerguntaServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PerguntaServlet</servlet-name>
<url-pattern>/imwsaogotardo/PerguntaServlet</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>/ErrosServlet</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/ErrosServlet</location>
</error-page>
</web-app>
And in Servlet
package erros;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(
name="ErrosServlet",
urlPatterns={"/ErrosServlet"}
)
public class ErrosServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
public ErrosServlet()
{
super();
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
Throwable throwable = (Throwable) req.getAttribute("javax.servlet.error.exception");
Integer statusCode = (Integer) req.getAttribute("javax.servlet.error.status_code");
req.setAttribute("errorType", throwable);
req.setAttribute("statusCode", statusCode);
//colocando o setStatus OK
res.setStatus(HttpServletResponse.SC_OK);
// We have to remove the exception from the request. If we do not remove the IE9 will not display out error page.
// IE9 will think that some error created a crash inside our application if we do not remove the exception from the request.
req.setAttribute("javax.servlet.error.exception", null);
req.setAttribute("javax.servlet.error.status_code", null);
RequestDispatcher requestDispatcher = null;
if(statusCode==404)
{
requestDispatcher = req.getRequestDispatcher("/erro404.jsp");
}
else if(statusCode==500)
{
requestDispatcher = req.getRequestDispatcher("/erro500.jsp");
}
requestDispatcher.forward(req, res);
}
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
doPost(req, res);
}
}
Note: I did not launch the Servet in web.xml
Is there any other resource?
Adding the error page:
<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>
Following error404Conteudo.jsp
<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>
Well, remembering that the dispatcher is done naturally. The error is only for the links of style and images. Hrefs and includes normally occur!