I've an ErrorFilter
which extends the spring GenericFilterBean
. I want to show an error page decorated with tiles if some error happens.
Is there any way to set a view name from the filter?
<filter>
<filter-name>errorFilter</filter-name>
<filter-class>com.abc.filter.ErrorFilter</filter-class>
<init-param>
<param-name>errorPage</param-name>
<param-value>/jsp/errorpage.jsp</param-value>
</init-param>
</filter>
This is the configuration in web.xml
and the doFilter
method in errorfilter
is the following:
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpReq = (HttpServletRequest) req;
StringBuffer reqUrl = httpReq.getRequestURL();
try {
chain.doFilter(req, resp);
} catch (Exception ex) {
String requestRepresentation = createRequestRepresentation(req);
errorService.handleException(reqUrl.toString(), ex, requestRepresentation);
req.getRequestDispatcher(
getFilterConfig().getInitParameter("errorPage")).forward(req, resp);
} catch (Error er) {
errorService.handleError(reqUrl.toString(), er);
req.getRequestDispatcher(
getFilterConfig().getInitParameter("errorPage")).forward(req, resp);
}
}
The current errorpage is not decorated with tiles, so I want to decorate it with normal header and footer and call that view name from the filter.
Is it possible ?
Edit: Basically we want to be able to do something similar to Controller-method i.e. return "view name";
Already tried:
There are three ways to add your filter, Annotate your filter with one of the Spring stereotypes such as @Component. Register a @Bean with Filter type in Spring @Configuration. Register a @Bean with FilterRegistrationBean type in Spring @Configuration.
The view is a component of MVC architecture that is used to return a user interface output to the user in response to the user request. A View page can be any HTML or JSP file. Spring MVC internally uses a view resolver to fetch the requested view to the user.
The FilterRegistrationBean is, as the name implies, a bean used to provide configuration to register Filter instances. It can be used to provide things like URL mappings etc.
i think this is not possible, because this is a servlet filter, which would be applied after the spring request mapper servlet was applied. so basically, the request mapper servlet thinks it is finished, and passes the request back to the servlet container.
view names only work INSIDE of spring - outside of spring, in the servlet container, you will have to talk about urls, not view names.
a redirect to specific url would work. for this, you have to be aware of the differences between a redirect and a forward.
a redirect sends the following header to the client:
Location: http://server/new/location
along with the status code 301 (permanent redirect, to let the client know that it can cache this information) or 307 (temporary redirect, to tell the client it should ask again next time, because the redirect may change)
the forward on the request dispatcher basically mimics a new request, and you can send the request to ANY servlet in the same container. this means, you have to ALSO take the context path into consideration, meaning the answer @iimuhin gave:
response.sendRedirect(
request.getContextPath() +
getFilterConfig().getInitParameter("errorPage"));
is actually the right way to go. you can (should) add logging to see what actually happens and what paths are actually used.
also - you have to be aware of buffering. a servlet response is usually buffered, and nothing is sent to the client until either the buffer is full or the entire processing has finished.
when it is flushed, the headers are written first. this means that changing the headers AFTER the response has been flushed is not possible, because they're already sent out.
this is the reason why servlets can force flushing, but usually shouldn't.
in your case, you may want to increase the buffer size on the response:
response.setBufferSize(int size)
before calling chain.doFilter() to avoid premature flushing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With