?
Solved

sendRedirect problem in Servlet Filters

Posted on 2005-03-23
19
Medium Priority
?
602 Views
Last Modified: 2008-01-09
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
Comment
Question by:sunmaster
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 7
19 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13618476
you cannot do anything after a sendRedirect
0
 
LVL 1

Author Comment

by:sunmaster
ID: 13618547
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
 
LVL 92

Expert Comment

by:objects
ID: 13618587
you should be returning from your filter immediately after you redirect.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:sunmaster
ID: 13618637
I keep getting this error....

java.lang.IllegalStateException

what else could be the problem...? ;)
0
 
LVL 92

Expert Comment

by:objects
ID: 13618647
what are you doing to get that error?
0
 
LVL 1

Author Comment

by:sunmaster
ID: 13618700
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
 
LVL 92

Expert Comment

by:objects
ID: 13618726
when is the exception getting thrown, what is the stack trace?
0
 
LVL 92

Expert Comment

by:objects
ID: 13618737
you are continuing to process the request after you have passed it to the next filter in the chain.
0
 
LVL 1

Author Comment

by:sunmaster
ID: 13619017
How do i 'break' the chain?
0
 
LVL 92

Expert Comment

by:objects
ID: 13619033
just return as I mentioned earlier
0
 
LVL 1

Author Comment

by:sunmaster
ID: 13619112
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
 
LVL 92

Expert Comment

by:objects
ID: 13619122
yes but your filter has already passed the request up the chain. You cannot do *any* processing once you have done that.
0
 
LVL 1

Author Comment

by:sunmaster
ID: 13619177
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
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 13619216
0
 
LVL 1

Author Comment

by:sunmaster
ID: 13619392
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
 
LVL 1

Expert Comment

by:vijaydogra
ID: 13667834
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
 
LVL 92

Expert Comment

by:objects
ID: 13667849
> 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
In this modest contribution, I want to share with the IT community (especially system administrators, IT Support Engineers and IT Help Desks) about Windows crashes/hangs and how to deal with these particular problems.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question