【解决HTML标签问题】
<%@ page import="java.net.*, org.apache.commons.lang3.*" pageEncoding="gb2312" %><%
int num;
try {
num = Integer.valueOf(request.getParameter("num"));
} catch (NumberFormatException e) {
num = 0;
}
%><!doctype html>
<html>
<head>
<meta charset="gb2312">
<title>简体中文网页</title>
</head>
<body>
<p>Tomcat8中server.xml的<Connector>标签下必须加上URIEncoding="ISO-8859-1", 否则将无法解析GB2312字符串!</p>
<p><%
String str = request.getParameter("value");
str = new String(str.getBytes("ISO-8859-1"), "gb2312");
str = StringEscapeUtils.escapeHtml4(str); // HTML编码
out.print(str);
%></p>
<form id="form1" name="form1" method="get">
<input name="value" type="text" id="value" value="<%= str %>">
<input type="submit" value="确 定">
<input name="num" type="hidden" id="num" value="<%= num + 1 %>">
</form>
<p><%
str = request.getQueryString();
str = URLDecoder.decode(str, "gb2312");
str = StringEscapeUtils.escapeHtml4(str); // HTML编码
out.print(str);
%></p>
</body>
</html>
需要在WEB-INF/lib/文件夹里放入commons-lang3-3.5.jar,下载地址是:
http://mirrors.hust.edu.cn/apache//commons/lang/binaries/commons-lang3-3.5-bin.tar.gz该jar文件位于压缩包内。
这样当URL参数中含有HTML标签时网页也能正常显示:
http://localhost:8080/test/get_encoding.jsp?value=%BC%F2%CC%E5%D6%D0%CE%C4%3Cscript%3E&num=10