当前位置: 代码迷 >> Java Web开发 >> jsp中读取 text中的汉语言
  详细解决方案

jsp中读取 text中的汉语言

热度:8587   发布时间:2013-02-25 21:11:05.0
jsp中读取 text中的中文
<input   type= "text "   name= "address ">
然后调用   String   address   =   request.getParameter( "address ");

text文本域中输入   中文   则是乱码   该怎么解决呢?

------解决方案--------------------------------------------------------
在java/sevlet代码中设置
request.setcharacterencoding("你的编码集");
------解决方案--------------------------------------------------------
一般情况下 ,不管默认的编码方式是什么,都在java的Servlet代码中添加
Java code
request.setCharacterEncoding("UTF-8");
------解决方案--------------------------------------------------------
一般来说,用post方式提交,在页面中设置设置编码方式就可以了,如果是get方式提交,则需要做一些处理

--jsp中--
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

var opinion = document.getElementById("opinion").value;
encodeURIComponent(encodeURIComponent(opinion)) //这是对中文进行编码处理

--java中--
String opinion = "";
try {
opinion = java.net.URLDecoder.decode(request.getParameter("opinion "), "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}

这样才能保证中文不出现乱码。
------解决方案--------------------------------------------------------
新建一个JSP 把最上面的改成<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 这就是和hrml不同的地方 少了这行不行。

在servelt中
response.setContentType("text/html;charset=utf-8");
request.setcharacterencoding("utf8");
这两句要在out对象之前声明换了位置等于没有用
  相关解决方案