jsp页面:
servlet:
这种写法可以解决2个问题:
1.中文乱码
2.%这个字符使用request.getParameter方法获取时报异常
<%@ page language="java" import="java.net.*" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="Sun, 6 Mar 2005 01:00:00 GMT" />
<title>测试</title>
<script type="text/javascript">
function encode(Str){
alert(Str);
//alert("http://127.0.0.1:9080/magicMMS/servlet/QmessageServlet?content="+encodeURIComponent("中文*&^$#@!汉%字"));
window.open("http://127.0.0.1:9080/magicMMS/servlet/QmessageServlet?content="+encodeURIComponent("中文*&^$#@!汉%字"));
}
</script>
</head>
<body>
<p/>
本地测试:
<p/>
<a href=http://127.0.0.1:8080/magicMMS/servlet/QmessageServlet?content=<%=java.net.URLEncoder.encode("中文*&^$#@!)(汉%字","UTF-8") %> target="_blank">中文本地测试</a>
<p/>
本地js编码测试:
<p/>
<button onclick="javascript:encode('中文*&^$#@!)(汉%字');">中文测试</button>
<p/>
</body>
</html>
servlet:
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String message = "";
message = new String(request.getParameter("content").getBytes("ISO8859-1"),"UTF-8");
}
这种写法可以解决2个问题:
1.中文乱码
2.%这个字符使用request.getParameter方法获取时报异常