<
meta
http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
<%@
page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
|
request.setCharacterEncoding(
"UTF-8"
);
|
清单附
C-5
SetCharacterEncoding.java
|
package
chapc;
import
java.io.IOException;
import
javax.servlet.Filter;
import
javax.servlet.FilterChain;
import
javax.servlet.FilterConfig;
import
javax.servlet.ServletException;
import
javax.servlet.ServletRequest;
import
javax.servlet.ServletResponse;
public
class
SetCharacterEncoding
implements
Filter {
@Override
public
void
destroy() {
}
@Override
public
void
doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws
IOException, ServletException {
//
设置字符编码
request.setCharacterEncoding(
"UTF-8"
);
//
将控制传到下一个过滤器,如果没有过滤器则传到被调用者
chain.doFilter(request, response);
}
@Override
public
void
init(FilterConfig arg0)
throws
ServletException {
}
}
|
清单附
C-6 web.xml
片段
|
<
filter
>
<
filter-name
>
SetCharacterEncoding
</
filter-name
>
<
filter-class
>
chapc.SetCharacterEncoding
</
filter-class
>
</
filter
>
<
filter-mapping
>
<
filter-name
>
SetCharacterEncoding
</
filter-name
>
<
url-pattern
>
/*
</
url-pattern
>
</
filter-mapping
>
|
清单附
C-7 server.xml
片段
|
<
Connector
connectionTimeout="20000"
port="8080"
protocol="HTTP/1.1"
redirectPort="8443"
URIEncoding="UTF-8"/>
|
<
form
action="registerProcess.jsp" method="get">
|
<
a
href="a.jsp?p=
中文参数
"
>
超链接传递中文参数测试
</
a
>
|
response.sendRedirect(
"a.jsp?p=
中文参数
"
);
|
<%
//
引入
URLEncoder
%>
<%@
page
import="java.net.URLEncoder"%>
<%
String p =
"
中文参数
"
;
//
对中文参数编码
p = URLEncoder.encode(p,
"UTF-8"
);
response.sendRedirect(
"a.jsp?p="
+ p);
%>
|
<%@
page
import="java.net.URLEncoder"%>
<%
String
p =
"
中文参数
"
;
//
对中文参数编码
p = URLEncoder.encode(p,
"UTF-8"
);
%>
<
a
href="a.jsp?p=<%=p %>">
超链接传递中文参数测试
</
a
>
|
open(
"a.jsp?p=
中文参数
"
);
|
<
script
type="text/javascript">
function
openWindow()
{
var
url =
"a.jsp?p=
中文参数
"
;
url = encodeURI(url);
open(url);
}
</
script
>
<
input
type="button" value="JavaScript
中文参数
"
onclick="openWindow();">
|
jdbc:mysql://localhost:3306/exam?useUnicode=true&characterEncoding=utf8;
|
String p = new String(p.getBytes(“
源编码
”),”
目标编码
”)
|
String p = new String(p.getBytes(“
源编码
”),”
目标编码
”)
|