简体   繁体   中英

Pass data of non english character to web server

From html , i am using data of non english characters to controller. eg) passing multi章byte in url as post request.

But in controller data is receiving as

multiç« byte

Can any one help me on how to fix this issue?

First, you need to tell the browser that you're serving UTF-8 content and expecting UTF-8 submit. You can do that by either placing the line in the very top of your JSP,

<%@page pageEncoding="UTF-8" %>

or by adding the following entry to web.xml so that you don't need to repeat @page over all JSPs:

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <page-encoding>UTF-8</page-encoding>
    </jsp-property-group>
</jsp-config>

Then, you need to tell the servlet API to use UTF-8 to parse POST request parameters. You can do that by creating a servlet filter which does basically the following:

@WebFilter("/*")
public class CharacterEncodingFilter implements Filter {

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        req.setCharacterEncoding("UTF-8");
        chain.doFilter(req, res);
    }

    // ...
}

That's all. You do not need <form accept-charset> (that would make things worse when using MSIE browser) and you do not necessarily need <meta http-equiv="content-type"> (that's only interpreted when the page is served from local disk file system instead of over HTTP).

Beware that when you're using System.out.println() or a logger in order to verify the submitted value, that it should in turn also be configured to use UTF-8 to present the characters, otherwise you will be mislead by still being presented with Mojibake .

See also:

you can use this in your action method

request.setCharacterEncoding("UTF-8");

this must be called prior to any request.getParameter() call

and just change the encoding depending on your needs

you can also refer to this

HttpServletRequest - setCharacterEncoding seems to do nothing

request.getQueryString() seems to need some encoding

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