Search
Contribute
Navigation
Go elsewhere
Web Content Management: Custom error pages
Article information
![]() |
WCM FAQs
, WCM samples and example
, WCM Tutorials
6.0 , 6.0.1 , 6.1 , development , example , WCM |
Chet Tuttle 12/02/2008 |
Hernan Cunico 12/08/2008 |
Summary:
This document provides an example of how WCM administrators can create a servlet filter to intercept an HTTP request / response and redirect users to custom pages based on the status. A filter will allow you to intercept requests and perform some processing prior to the request reaching the servlet. Filters also provide the ability to override and even return a custom response object. This example will show you how to implement a filter that overrides the setStatus method on the HttpServletResponse and use it to redirect the user to a custom error404.html page when the servlet returns a 404 not found.
Sample attached below
The code for a filter that can be used to return a customer error page when a status of 404 is returned:
package com.ibm.sample.filters;
import javax.servlet.*;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.Filter;
import java.io.*;
public class SampleResponseFilter implements Filter {
private FilterConfig filterConfig = null;
public void init(FilterConfig filerConfig) {
this.filterConfig = filterConfig;
}
public void destroy()
// Called by the web container to indicate to a filter that it is being taken out of service.
{
filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// Create a wrappedResponse object done before chain.doFilter as this is what is passed to the servlet
HttpServletResponse httpResp = (HttpServletResponse) response;
ResponseStatusWrapper wrappedResponse = new ResponseStatusWrapper(
httpResp);
// Called by the container each time a request/response pair is passed through the chain due to a client request.
chain.doFilter(request, wrappedResponse);
// use getStatus to check for 404 after the fact
Integer error404 = new Integer(404);
if (wrappedResponse.getStatus().contains(error404.toString())) {
System.out.println("**** 404 REDIRECTED TO CUSTOM PAGE ****");
}
}
}
package com.ibm.sample.filters;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.http.*;
public class ResponseStatusWrapper extends HttpServletResponseWrapp er {
private ArrayList status = null;
public ResponseStatusWrapper(HttpServletResponse response)
{
super(response);
status = new ArrayList(1);
}
public ArrayList getStatus()
{
return status;
}
//override the setStatus method
public void setStatus( int currentStatus) {
System.out.println(currentStatus);
Integer intStatus = new Integer(currentStatus);
status.add(intStatus.toString());
HttpServletResponse httpResponse;
//Update the original response depending on status
if ( currentStatus != 404)
{
// If the status is not 404 be transparent just set the status
httpResponse = (HttpServletResponse) this.getResponse();
httpResponse.setStatus(intStatus.intValue());
} else
// If the status is 404 then send a redirect URL
httpResponse = (HttpServletResponse) this.getResponse();
try {
httpResponse.sendRedirect("http://tuttlec.raleigh.ibm.com:10038/wps/wcm/jsp/html/error1.html");
} catch (IOException e) {System.out.println("ResponseStatusWrapper: IOEXCEPTION");}
}
}
Put the class files in the ../shared/app directory:
Once you have compiled the filter code take the package jar file in my case filters.jar and place it in the/shared app directory
Update the web.xml:
Locate the web.xml and add the following
/wp_profile/config/cells/tut/applications/wcm.ear/deployments/wcm/ilwwcm.war/WEB-INF/web.xml
/wp_profile/installedApps/tut/wcm.ear/ilwwcm.war/WEB-INF/web.xml

Finally, restart then server and request some page that will result in an error like:
http://localhost:10038/wps/wcm/myconnect/web+content/Corning/404Error
This document provides an example of how WCM administrators can create a servlet filter to intercept an HTTP request / response and redirect users to custom pages based on the status. A filter will allow you to intercept requests and perform some processing prior to the request reaching the servlet. Filters also provide the ability to override and even return a custom response object. This example will show you how to implement a filter that overrides the setStatus method on the HttpServletResponse and use it to redirect the user to a custom error404.html page when the servlet returns a 404 not found.
Sample attached below
The code for a filter that can be used to return a customer error page when a status of 404 is returned:
package com.ibm.sample.filters;
import javax.servlet.*;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.Filter;
import java.io.*;
public class SampleResponseFilter implements Filter {
private FilterConfig filterConfig = null;
public void init(FilterConfig filerConfig) {
this.filterConfig = filterConfig;
}
public void destroy()
// Called by the web container to indicate to a filter that it is being taken out of service.
{
filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// Create a wrappedResponse object done before chain.doFilter as this is what is passed to the servlet
HttpServletResponse httpResp = (HttpServletResponse) response;
ResponseStatusWrapper wrappedResponse = new ResponseStatusWrapper(
httpResp);
// Called by the container each time a request/response pair is passed through the chain due to a client request.
chain.doFilter(request, wrappedResponse);
// use getStatus to check for 404 after the fact
Integer error404 = new Integer(404);
if (wrappedResponse.getStatus().contains(error404.toString())) {
System.out.println("**** 404 REDIRECTED TO CUSTOM PAGE ****");
}
}
}
package com.ibm.sample.filters;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.http.*;
public class ResponseStatusWrapper extends HttpServletResponseWrapp er {
private ArrayList status = null;
public ResponseStatusWrapper(HttpServletResponse response)
{
super(response);
status = new ArrayList(1);
}
public ArrayList getStatus()
{
return status;
}
//override the setStatus method
public void setStatus( int currentStatus) {
System.out.println(currentStatus);
Integer intStatus = new Integer(currentStatus);
status.add(intStatus.toString());
HttpServletResponse httpResponse;
//Update the original response depending on status
if ( currentStatus != 404)
{
// If the status is not 404 be transparent just set the status
httpResponse = (HttpServletResponse) this.getResponse();
httpResponse.setStatus(intStatus.intValue());
} else
// If the status is 404 then send a redirect URL
httpResponse = (HttpServletResponse) this.getResponse();
try {
httpResponse.sendRedirect("http://tuttlec.raleigh.ibm.com:10038/wps/wcm/jsp/html/error1.html");
} catch (IOException e) {System.out.println("ResponseStatusWrapper: IOEXCEPTION");}
}
}
Put the class files in the ../shared/app directory:
Once you have compiled the filter code take the package jar file in my case filters.jar and place it in the
Update the web.xml:
Locate the web.xml and add the following
Finally, restart then server and request some page that will result in an error like:
http://
Comments



