File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Redirecting inside a filter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Redirecting inside a filter" Watch "Redirecting inside a filter" New topic

Redirecting inside a filter

M Burke
Ranch Hand

Joined: Jun 25, 2004
Posts: 397
I am making a filter to intercept calls between a jsp and servlet. If there is a business rules error, I want to redirect the call back to the jsp.

The problem is, when I forward back to the jsp, the browser thinks it was forwarded to the servlet. I can see this in the address bar of the browser. Then when I resubmit the jsp request, the filter thinks the call is coming from the servlet, not the jsp. This breaks the navigation.

This filter is still in the beginning stages, so the business rules have not been added.

package servlets;
import java.util.Enumeration;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletResponse;
import javax.servlet.Filter;
import javax.servlet.ServletException;
import javax.servlet.FilterConfig;
import javax.servlet.ServletRequest;
import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;

public class Filter1 implements Filter
private FilterConfig _filterConfig = null;

public void init(FilterConfig filterConfig) throws ServletException
_filterConfig = filterConfig;

public void destroy()
_filterConfig = null;

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
HttpServletRequest sr = (HttpServletRequest) request;
String qs = sr.getQueryString();
String relPath = sr.getContextPath();
String uri = sr.getHeader("REFERER");
if(uri == null)
throw new ServletException("Cannot determain origin");

int ix = uri.lastIndexOf('/');
uri = uri.substring(ix);

boolean bizRules = false;

request.setAttribute("errMsg", "You cannot promote this bug report to 'Ready for Testing'");
request.getRequestDispatcher(uri).forward(request, response);;
chain.doFilter(request, response);


<%@ page contentType="text/html;charset=windows-1252"%>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
String err = (String) request.getAttribute("errMsg");
if(err != null && !err.equals("")){

out.write("<h4><font color='red'>Validation Error</font></h4>You must correct the following error before proceeding:<ul><li>");


<form action="transferservlet" method="post" name="form1">
<input type="TEXT" name="trans" />
<input type="SUBMIT" />

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63852

You might want to go back and edit your post to add UBB code tags to preserve the formatting of the code.

[Asking smart questions] [About Bear] [Books by Bear]
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
This is just to inform you, incase you dont know. We can use filter without using servlet. Actually Filter is the complement to servlet. Moreover, you can say Filter is something more than a servlet or a special servlet.

We can't always guess what's going on just by seeing the address in the address bar. Sometimes the whole application runs under the same URL, and the URL never changes during navigation.
M Burke
Ranch Hand

Joined: Jun 25, 2004
Posts: 397
well, in this case I do need the filter to be assosiated with a servlet. I am doing a mode to an existing app from another company. And they do not provide the source code. So I need to fit componants between theirs
I agree. Here's the link:
subject: Redirecting inside a filter
It's not a secret anymore!