ACTUALIZACIÓN:Escribir un servlet que comprueba para ver si existen y los delanteros del JSP a otra JSP si no están
Para aclarar un receptor de error genérico que las capturas de 404 no tiene suficiente granularidad para mí. Necesito hacer esto solo si el jsp está en un directorio particular y solo si el nombre de archivo contiene una cierta cadena.
/ACTUALIZACIÓN
He recibido el encargo de escribir un servlet que intercepta una llamada a y JSP en un directorio específico, compruebe que el archivo existe y si se limita a remitir a ese archivo, si entonces no voy a reenviar a un JSP predeterminado. he configurar el web.xml de la siguiente manera:
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>CustomJSPListener</servlet-name>
<servlet-class> ... CustomJSPListener</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
...
<servlet-mapping>
<servlet-name>CustomJSPListener</servlet-name>
<url-pattern>/custom/*</url-pattern>
</servlet-mapping>
Y el método doGet del servlet es el siguiente:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
logger.debug(String.format("Intercepted a request for an item in the custom directory [%s]",request.getRequestURL().toString()));
String requestUri = request.getRequestURI();
// Check that the file name contains a text string
if (requestUri.toLowerCase(Locale.UK).contains("someText")){
logger.debug(String.format("We are interested in this file [%s]",requestUri));
File file = new File(requestUri);
boolean fileExists = file.exists();
logger.debug(String.format("Checking to see if file [%s] exists [%s].",requestUri,fileExists));
// if the file exists just forward it to the file
if (fileExists){
getServletConfig().getServletContext().getRequestDispatcher(
requestUri).forward(request,response);
} else {
// Otherwise redirect to default.jsp
getServletConfig().getServletContext().getRequestDispatcher(
"/custom/default.jsp").forward(request,response);
}
} else {
// We aren't responsible for checking this file exists just pass it on to the requeseted jsp
getServletConfig().getServletContext().getRequestDispatcher(
requestUri).forward(request,response);
}
}
Este parece ser el resultado de un error 500 de Tomcat, creo esto se debe a que el servlet está redirigiendo a la misma carpeta que el servlet vuelve a interceptar, lo que da como resultado un ciclo infinito. ¿Hay una mejor manera de hacer esto? Me hacen creer que podría usar filtros para hacer esto, pero no sé mucho sobre ellos.
Gracias, intentaré esto. –
Eso todavía no detiene el problema al ingresar un bucle infinito. –
Agregue ' SOLICITUD ' a ''. Por cierto: un filtro es un mejor lugar para esto, puedes poner el mismo código en el 'doFilter()', solo tienes que enviar la solicitud y la respuesta hacia abajo. –
BalusC