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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T14:47:35+00:00 2026-05-29T14:47:35+00:00

It feels like I’m stuck. I’m trying to write the simplest servlet Filter (and

  • 0

It feels like I’m stuck.
I’m trying to write the simplest servlet Filter (and deploy it to tomcat). It’s a groovy code, but actually I’m heavily using java approaches here, so it is almost copy-paste, that’s the reason I’ve added java tag as well.

My question is – how can I insert UTF-8 string to filter?
Here is the code:

public class SimpleFilter implements javax.servlet.Filter

{
    ...
    public void doFilter(ServletRequest request, ServletResponse response,
           FilterChain chain) 
           throws java.io.IOException, javax.servlet.ServletException
    {   
            PrintWriter out = response.getWriter()
            chain.doFilter(request, wrapResponse((HttpServletResponse) response))

            response.setCharacterEncoding('UTF-8')
            response.setContentType('text/plain')

            def saw = 'АБВГДЕЙКА ЭТО НЕПРОСТАЯ ПЕРЕДАЧА ABCDEFGHIJKLMNOP!!!'
            def bytes = saw.getBytes('UTF-8')
            def content = new String(bytes, 'UTF-8')

            response.setContentLength(content.length())
            out.write(content);
            out.close();
    }   

    private static HttpServletResponse  wrapResponse(HttpServletResponse response) {
        return new HttpServletResponseWrapper(response) {
            @Override
            public PrintWriter getWriter() {
                 def writer  = new OutputStreamWriter(new ByteArrayOutputStream(), 'UTF-8')
                 return new PrintWriter(writer)
            }   
        }   
    }   
}

Content-Type of the filtered page is text/plain;charset=ISO-8859-1.
So, content type have changed, but charset is ignored.

As you can see, I’ve take some measures (I guess quite naive) to make sure content is UTF-8, but none of these steps actually was helpful.

I’ve also tried to add URIEncoding="UTF-8" or useBodyEncodingForUri="true" attributes to
Connector in tomcat conf/server.xml

It would be nice if somebody explained me what I’m doing wrong.

UPD: just a bit of explanation – I’m writing XSLT-applying filter, that is the real reason I’m trying to discard whole request.

  • 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-29T14:47:38+00:00Added an answer on May 29, 2026 at 2:47 pm
    def saw = 'АБВГДЕЙКА ЭТО НЕПРОСТАЯ ПЕРЕДАЧА ABCDEFGHIJKLMNOP!!!'
    def bytes = saw.getBytes('UTF-8')
    def content = new String(bytes, 'UTF-8')
    

    Does not change a thing between saw and content. What you want is to do (using the outputstream and not the writer, this is why the charset is reset to ISO-8859-1 See tomcat doc):

    out.write(saw.getBytes("UTF-8);
    

    Your code looks okay to set the charset as UTF-8.

    I don’t understand what you are doing with HttpResponseWrapper.

    To make it clear, this will work:

    public void doFilter(ServletRequest request, ServletResponse response,
       FilterChain chain) 
       throws java.io.IOException, javax.servlet.ServletException
    {   
        OutputStream out = response.getOutputStream()
    
        response.setCharacterEncoding('UTF-8')
        response.setContentType('text/plain')
    
        def saw = 'АБВГДЕЙКА ЭТО НЕПРОСТАЯ ПЕРЕДАЧА ABCDEFGHIJKLMNOP!!!'
    
        response.setContentLength(saw.length())
        out.write(content.getBytes("UTF-8"));
    
    }   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This feels like the kind of code that only fails in-situ, but I will
OK, this feels like an idiot question, but I'm stuck - I don't know
I'm completely new to AIR but what I'm trying to do feels like it
I don't see any problems with this code, but it feels like I'm missing
I've been trying to get this code to work for what feels like an
It feels like there must be some semi-simple solution to this, but I just
This feels like a really basic question, but I can't figure it out anyway..
This feels like it should be pretty simple, but not much seems to be
I've notice an issue - it feels like a bug but I suspect a
This feels like a silly question but I'm stumped. I working on an iOS

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.