简体   繁体   中英

UTF-8 encoding in jsp

I am currently returning a utf-8 encoded string from a method in a class within my jsp page however although most characters of the string are ok problems arise with letters that have an infrontation such as ό as all of them are encoded as ?

How can this problem be solved?

I have the correct directives needed by the jsp page in order to have its content encoded as utf-8

The contents of my string are in greek

I had the same problem and for me adding the CharacterEncodingFilter from spring in web.xml fixed that:

<filter>
    <filter-name>SetCharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>SetCharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

You can use it even if you do not use spring in your project (just download spring-web-xxx.jar).

Or you can create your own filter that does this:

request.setCharacterEncoding("UTF8");
response.setCharacterEncoding("UTF8");

You can find the source code of org.springframework.web.filter.CharacterEncodingFilter on grepcode.com, for example, to see exactly what it does.

Did you use this directive <%@page contentType="text/html;charset=UTF-8"%> ?

This sets your content-type to utf-8 and your pageEncoding

Some how I did the following :

I am using the Eclipse IDE and set Window > Preferences > General > Workspace > Text File Encoding to UTF-8.

After that, all my encoding problems, magically vanished !!!!!

Can somebody explain this ??

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM