servlet接收get方式的中文参数,显示乱码,试了很多方式都不行呢!!!源码如下:
jsp主要代码页面<%@ page contentType="text/html; charset=UTF-8" language="java"%>
<script type="text/javascript">
function getResult(){
var url = "teac_servlet/valid?name=张三";
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
}
else if (window.ActiveXObject){
req = new ActiveXObject("Microsoft.XMLHTTP");
} if(req){
req.open("GET",url, true);
req.send(null);
}
}
</script>servlet源码:
public class UserValidate extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String strName = request.getParameter("name"); System.out.println("strName:"+strName);
//打印出的strName为????????????
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}}
打印出的strName为????????????
jsp主要代码页面<%@ page contentType="text/html; charset=UTF-8" language="java"%>
<script type="text/javascript">
function getResult(){
var url = "teac_servlet/valid?name=张三";
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
}
else if (window.ActiveXObject){
req = new ActiveXObject("Microsoft.XMLHTTP");
} if(req){
req.open("GET",url, true);
req.send(null);
}
}
</script>servlet源码:
public class UserValidate extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String strName = request.getParameter("name"); System.out.println("strName:"+strName);
//打印出的strName为????????????
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}}
打印出的strName为????????????
解决方案 »
- struts2的获取页面参数的问题
- struts2问题(急)
- 一个比较难的问题
- Jsp显示大数据量的问题
- 那位有jsp 用户注册 程序,(mysql数据库)?
- 问个简单又复杂的问题!!
- WEB开发群(2767336)、Java/JSP、JS、XML、VRML、SQL、Oracle、UML…………
- 如何更新整形变量?
- JSP+JAVABEAN就是无法查处结果???
- 【最新】JAVA Spring mvc +mybatis(oracle 和 mysql) HTML5
- Servlet生命周期,在检测destroy()方法是否发生时,无法得到我想要的结果,应该如何修改?
- struts2 禁止url直接提交action
action:URLDecoder.decode(str,"UTF-8")
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
strName = new String("strName".getByte("iso-8859-1"),"uft-8")
<Connector executor="tomcatThreadPool"
port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8"/>
试试
获取 String value = EscapeUnicode.unescape(value);
1、首先进入servlet中时检查request传入的编码
request.getCharacterEncoding();
2、web.xml中有没有EncodingFilter过滤器,其中的编码设置检查一下
3、如果没有,也可以自己写一个过滤器,设定request的编码
request.setCharacterEncoding("UTF-8")
4、jsp页面上pageEncoding设置要检查一下
5、tomcat/conf/server.xml中,connector这里的编码设置:URIEncoding="UTF-8"
以上逐条检查调试,原则是:编码要保持一致。