<%String username = "张某某" ;
username = URLEncoder.encode(username,"utf-8");
%>
<a href="to.jsp?param=<%=username %>">转入</a>
to.jsp页面
复制代码 代码如下:
<%=URLDecoder.decode(request.getParameter("param"),"utf-8")%>
总之 ,乱码的解决方案如下:
post传值乱码时,在接收端设置request.setCharacterEncoding("UTF-8")
get传值或者url乱码时,手动设置接收的参数String str = new String(request.getParameter("something").getBytes("ISO-8859-1"),"utf-8") ;
由上可见get,post传值在tomcat5中是不一样的.
看完了上面红字部分的内容,我决定在提交数据的页面设置以UTF-8的格式提交数据,而同时在接收数据的页面设置也以UTF-8接收数据,于是我在两个页面的首部都添加了如下语句:
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=utf-8");
%>
然后测试,OK了!没有乱码了!
现在,index.jsp页面的代码如下:
复制代码 代码如下:
<%@ page language="java" pageEncoding="utf-8"%>
<%@ page contentType="text/html;charset=utf-8"%>
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=utf-8");
%>
<html>
<head>
</head>
<body>
<form action="mysql_insert.jsp" method="post">
ID :<input type = "text" value="0"/>
姓名 :<input type = "text" value="aaa"/>
性别 :<input type = "text" value="female"/>
年龄:<input type = "text" value="20"/>
</br>
<input type = "submit" value="提交"/>
</form>
</body>
</html>
您可能感兴趣的文章: