Meaningless Drivel is fun!*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Redirecting inside a filter" Watch "Redirecting inside a filter" New topic
Author

Redirecting inside a filter

M Burke
Ranch Hand

Joined: Jun 25, 2004
Posts: 388
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 java.io.IOException;
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;

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

JSP:

<%@ page contentType="text/html;charset=windows-1252"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>untitled</title>
</head>
<body>
<%
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>");
out.write(err);
out.write("</li></ul><hr>");

}
%>

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

</body>
</html>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60783
    
  65

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] [Bear's FrontMan] [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: 388
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
 
 
subject: Redirecting inside a filter
 
Similar Threads
Problem with UTF-8 Character encoding
Internationlization problem
Tomcat servlet-mapping bypass if real file requested
Q: Filter on Page 668 and page 695
Confused by session management.