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

  • SEARCH
  • Home
  • 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 7408887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:59:21+00:00 2026-05-29T05:59:21+00:00

I coded my own Spring filter in order to encode in UTF-8 all the

  • 0

I coded my own Spring filter in order to encode in UTF-8 all the responses except for images:

package my.local.package.filter;

public class CharacterEncodingFilter extends org.springframework.web.filter.CharacterEncodingFilter
{

    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException
    {
        if(!request.getRequestURI().endsWith("jpg") &&
                !request.getRequestURI().endsWith("png") &&
                !request.getRequestURI().endsWith("gif") &&
                !request.getRequestURI().endsWith("ico"))
        {
            super.doFilterInternal(request, response, filterChain);
        }

        filterChain.doFilter(request, response);
    }
}

I’m referencing it in the web.xml:

<filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>my.local.package.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Everything works as expected, jpg/png/gif/ico files are not encoded in UTF-8 while all the other files are.

I’m now trying to code a simple controller which has to return a 404 error under certain conditions:

@Controller
public class Avatar
{
    @RequestMapping("/images/{width}x{height}/{subject}.jpg")
    public void avatar(HttpServletResponse response,
                       @PathVariable("width") String width,
                       @PathVariable("height") String height,
                       @PathVariable("subject") String subject) throws IOException
    {
        ...

        // if(error)
        // {
            response.sendError(HttpServletResponse.SC_NOT_FOUND, "Not found");
            return;
        // }

        ...
    }
}

But when requesting, for example, /images/52×52/1.jpg i get a page containing this error:

java.lang.IllegalStateException: Cannot call sendError() after the response has been committed

I think I coded the filter in a wrong way (I’m not experienced with Spring), because if I specify org.springframework.web.filter.CharacterEncodingFilter instead of my.local.package.filter.CharacterEncodingFilter in the web.xml file, it works perfectly.

Can someone help me?

Thank you.

  • 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-29T05:59:22+00:00Added an answer on May 29, 2026 at 5:59 am

    You are calling filterChain.doFilter(request, response); twice. Once in your code and once in super.doFilterInternal(request, response, filterChain);

    To fix this, simply put your doFilter in the else clause to your if.

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

Sidebar

Related Questions

I have coded my own tiny static DAL class for .NET Compact Framework and
Let's assume we've got the following Java code: public class Maintainer { private Map<Enum,
I have this code (like in Spring's reference): <bean id=multipartResolver class=org.springframework.web.multipart.commons.CommonsMultipartResolver> <!-- one of
I am following this excellent article in order to create my own attribute in
I am currently trying to code my own JS drag and drop script (out
I have an application that allows users to write their own code in a
I've seen a lot of people try to code their own image conversion techniques.
What do you call a programming language that can execute its own code (passed
I'm starting to code up my own window manager, and was wondering how to
I am trying to reuse Apple's Speak Here sample code in my own iPhone

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.