[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 605
  • Last Modified:

sendRedirect problem in Servlet Filters

Hi,

   I keep on hitting java.lang.IllegalStateException when i go a sendRedirect after when i do a respond.sendRedirect("....");

   Any sollution to this?


Thanks
sunmaster

0
sunmaster
Asked:
sunmaster
  • 9
  • 7
1 Solution
 
objectsCommented:
you cannot do anything after a sendRedirect
0
 
sunmasterAuthor Commented:
thanks

   Tried already but still cant work. I got a few filters... If i do a sendredirect will it go through all the filters like a normal request from a JSP page?


regards,
sunmaster
0
 
objectsCommented:
you should be returning from your filter immediately after you redirect.
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
sunmasterAuthor Commented:
I keep getting this error....

java.lang.IllegalStateException

what else could be the problem...? ;)
0
 
objectsCommented:
what are you doing to get that error?
0
 
sunmasterAuthor Commented:
here is my do filter method...
anything wrong with it?
there is only one sendRedirect.....

thanks in advance...


   public void doFilter(ServletRequest request, ServletResponse response,
    FilterChain chain)
    throws IOException, ServletException {

        if (DEBUG) log("TaskFilter:doFilter()");
        Throwable problem = null;
        RequestWrapper  wrappedRequest  = new RequestWrapper((HttpServletRequest)request);
        ResponseWrapper wrappedResponse = new ResponseWrapper((HttpServletResponse)response);

        HttpSession session = wrappedRequest.getSession();
        String userStatus = (String)session.getAttribute("my.com.eprotea.emcms.userStatus");
   
        boolean sendRedirect = false;
        if( isAuthorized(wrappedRequest, response)){
            try {
                chain.doFilter(wrappedRequest, response);
                String passExp = (String)session.getAttribute("my.com.eprotea.emcms.passwordExpiry");
                passExp = passExp.substring(0,2)+"/"+passExp.substring(2,4)+"/"+passExp.substring(4,8);

                SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
                Calendar c = Calendar.getInstance();
                String curTms = formatter.format(c.getTime());

                Date expDt = formatter.parse(passExp);
                Date curDt = formatter.parse(curTms);
                double diff = expDt.getTime() - curDt.getTime();
                int tempdiff = (int)(diff/86400000);


                if(("N".equals(userStatus) && wrappedRequest.getParameter("sent") == null) || (tempdiff <= 0 && wrappedRequest.getParameter("sent") == null)){//new user or password expires

                    //if __stat == N then redirect to login Page
                    sendRedirect = true;
                    //((HttpServletResponse)response).sendRedirect("gen/RSE501MN.jsp?__stat=N");
                }
            }catch(Throwable t) {
                    // If an exception is thrown somewhere down the filter chain,
                    // we still want to execute our after processing, and then
                    // rethrow the problem after that.
                problem = t;
                t.printStackTrace();
            }

        }else{
            ((HttpServletResponse)response).sendError(HttpServletResponse.SC_FORBIDDEN,"Access denied. No permission to access this resource.  Have you set the task id for this jsp in JSP_TASK table?");
        }


        //
        // If there was a problem, we want to rethrow it if it is
        // a known type, otherwise log it.
        //
        if (problem != null) {
            if (problem instanceof ServletException) throw (ServletException)problem;
            if (problem instanceof IOException) throw (IOException)problem;
            sendProcessingError(problem, response);
        }
        if(sendRedirect){
                        ((HttpServletResponse)response).sendRedirect("gen/RSE501MN.jsp?__stat=N");
        }

    }
0
 
objectsCommented:
when is the exception getting thrown, what is the stack trace?
0
 
objectsCommented:
you are continuing to process the request after you have passed it to the next filter in the chain.
0
 
sunmasterAuthor Commented:
How do i 'break' the chain?
0
 
objectsCommented:
just return as I mentioned earlier
0
 
sunmasterAuthor Commented:
U mean just put a return statement?
something like,

...
       if(sendRedirect){
              ((HttpServletResponse)response).sendRedirect("gen/RSE501MN.jsp?__stat=N");
              return;
        }

coz i have tried it and still the same.Here is the stacktrace....


java.lang.IllegalStateException
      at org.apache.coyote.tomcat5.CoyoteResponseFacade.sendRedirect(CoyoteResponseFacade.java:352)
      at javax.servlet.http.HttpServletResponseWrapper.sendRedirect(HttpServletResponseWrapper.java:125)
      at javax.servlet.http.HttpServletResponseWrapper.sendRedirect(HttpServletResponseWrapper.java:125)
      at javax.servlet.http.HttpServletResponseWrapper.sendRedirect(HttpServletResponseWrapper.java:125)
      at my.com.eprotea.emcms.filters.TaskCheckPointFilter.doFilter(TaskCheckPointFilter.java:168)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:186)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
      at compressionFilters.CompressionFilter.doFilter(CompressionFilter.java:190)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:186)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
      at my.com.eprotea.emcms.filters.ChangePassword.doFilter(ChangePassword.java:137)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:186)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
      at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
      at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
      at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
      at java.lang.Thread.run(Thread.java:534)

0
 
objectsCommented:
yes but your filter has already passed the request up the chain. You cannot do *any* processing once you have done that.
0
 
sunmasterAuthor Commented:
I am kind of beginer in this...
This whole thing not even written by me...I just have to modify it....
what you say is that there is no sollution izzit...? ;)
0
 
sunmasterAuthor Commented:
Thanks...

I manage to solve the original problem...
I have change my code to...

filterConfig.getServletContext().getRequestDispatcher("/gen/RSE501MN.jsp?__stat=N").forward(request,response);

instead of using sendRedirect.

but another problem come out...
I wan user to stay on this jsp page> RSE501MN.jsp which is a change password page until the user really change the password...
But when i key in another url in the browser this exception come out...

java.lang.IllegalStateException: Cannot forward after response has been committed
      at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:324)
      at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:312)
      at my.com.eprotea.emcms.filters.TaskCheckPointFilter.doFilter(TaskCheckPointFilter.java:143)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:186)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
      at compressionFilters.CompressionFilter.doFilter(CompressionFilter.java:190)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:186)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:540)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
      at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
      at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
      at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
      at java.lang.Thread.run(Thread.java:534)




Any ideas....?


thanks in advance...
0
 
vijaydograCommented:
In this method:
isAuthorized(wrappedRequest, response)
are you performing any operation wherein you send the reposne to the browser or similar. This is when you are using reponse.sendREdirect();
0
 
objectsCommented:
> java.lang.IllegalStateException: Cannot forward after response has been committed

Similiar problem as earlier, once the response has been committed you cannot do anything else with it.

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 9
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now