Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8436675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:14:43+00:00 2026-06-10T07:14:43+00:00

In servlet Filters, filterChain.doFilter(request, response); should pass the request to next one in chain.

  • 0

In servlet Filters, filterChain.doFilter(request, response); should pass the request to next one in chain.
But consider the following two codes:

Code 1:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException 
{
filterChain.doFilter(request, response);
try
{
Thread.sleep(20000);
}
catch(Exception e)
{
}            
}

Code 2:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException 
{
try
{
Thread.sleep(20000);
}
catch(Exception e)
{
}      
filterChain.doFilter(request, response);      
}

Both the filters executes as same. i.e. Both takes 20 seconds before serving the request.

But actually what should happen is Code1 should serve inmmediately and Code2 should serve after 20 seconds.
Why this ambiguity in Filter?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:14:45+00:00Added an answer on June 10, 2026 at 7:14 am

    Sleeping in servlet or in filter is always a bad idea because HTTP worker threads are scarce resources, thus you shouldn’t block them. But in your particular example there is hope.

    Basically whatever you print in your servlet or filter to the output is implicitly buffered to improve performance. If you print enough data inside servlet/filter, servlet container will flush the buffer and parts of your response will reach the client. But you can also flush manually!

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        filterChain.doFilter(request, response);
        response.flushBuffer();
        Thread.sleep(20000);
    }
    

    flushBuffer() instruction forces the container to flush the output buffer. All response headers and whatever you sent from the servlet is sent to the client. But here’s the catch: the client will receive the data but the HTTP connection remains open for the next 20 seconds. I tested this with curl and it works as expected. But when the same URL is used in the browser (tested on Opera, Firefox and Google Chrome), the browser waits 20 seconds before displaying anything (this may be dependent on what you actually send).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws
How can I write a filter class to pass the response from one servlet
I have a Filter Servlet that filters request from a Servlet. I do not
That's my filter: public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws java.io.IOException, javax.servlet.ServletException
The following servlet filter is getting called, but not able to give the correct
Is there a .NET equivalent to using Servlet Filters for request pre-processing in .NET?
Is it possible to write a servlet filter to take inspect HTTP response codes?
In the servlet 2.4 spec you can define dispatchers in the filter config [REQUEST
I am debugging a really tricky problem with servlet filters in a web application
In the Tomcat container, do all the filters associated to a servlet and the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.