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 836081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:55:27+00:00 2026-05-15T04:55:27+00:00

What is the difference between a Servlet and Filter ? What do you recommend

  • 0

What is the difference between a Servlet and Filter? What do you recommend to use for authorization to pages?

  • 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-05-15T04:55:28+00:00Added an answer on May 15, 2026 at 4:55 am

    Use a Filter when you want to filter and/or modify requests based on specific conditions. Use a Servlet when you want to control, preprocess and/or postprocess requests.

    The Java EE tutorial mentions the following about filters:

    A filter is an object that can transform the header and content (or both) of a request or response. Filters differ from web components in that filters usually do not themselves create a response. Instead, a filter provides functionality that can be “attached” to any kind of web resource. Consequently, a filter should not have any dependencies on a web resource for which it is acting as a filter; this way it can be composed with more than one type of web resource.

    The main tasks that a filter can perform are as follows:

    • Query the request and act accordingly.
    • Block the request-and-response pair from passing any further.
    • Modify the request headers and data. You do this by providing a customized version of the request.
    • Modify the response headers and data. You do this by providing a customized version of the response.
    • Interact with external resources.

    For authorization, a Filter is the best suited. Here’s a basic kickoff example of how a filter checks requests for the logged-in user:

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        if (((HttpServletRequest) request).getSession().getAttribute("user") == null) {
            // User is not logged in. Redirect to login page.
            ((HttpServletResponse) response).sendRedirect("login");
        } else {
            // User is logged in. Just continue with request.
            chain.doFilter(request, response);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone explain the difference between a servlet filter and tomcat valve. What is
is there any difference between servlet and filter lifecycle?.
Asked in the interview: What is the difference between HTTP servlet and SOAP over
What is the difference between A Web Server and A Servlet Container? In general
The javadoc for javax.servlet.http.HttpServletResponse is a little vague on the difference between what rules
What's the difference between Java Servlet and core Java? Is that difficult to learn
what is the difference between socket programming, rmi and Servlets. When to use what?
As the title says what is the difference between dispatcher-servlet.xml and spring-servlet.xml They seem
So I read this great thread: Difference between each instance of servlet and each
What is the difference between relative and absolute url in servlet container. for example

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.