Yishu Fang
Yishu Fang

Reputation: 9958

In JSP, how can I get GET parameters using utf8 encoding?

I am using GlassFish 4, and my JSP file is simple:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <%= request.getParameter("a") %>
    </body>
</html>

This is my request GET index.jsp?a=历史, and the output is:

åå² 

What's wrong with it?

Upvotes: 0

Views: 4292

Answers (1)

NuuoeiZ
NuuoeiZ

Reputation: 94

You can use this code "a_receive = new String(a_receive.getBytes("ISO8859_1"), "UTF-8");"

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <% a_receive = request.getParameter("a") 
           a_receive = new String(a_receive.getBytes("ISO8859_1"), "UTF-8");
%>
        <%=a_receive %>
    </body>
</html>

Upvotes: 5

Related Questions