• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Redirecting inside a filter

 
M Burke
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64618
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to go back and edit your post to add UBB code tags to preserve the formatting of the code.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic