您的位置:首页 > Web前端 > JavaScript

JSP数据提交乱码问题

2009-10-31 10:31 218 查看
原理不说了,网上有很多,都可以自己查到,至于为什么会出现乱码,是因为JAVA有使用国际化,遇到中文时.....

下面给出一些简单的实用代码,只供参考:

public class ChangeUtf8 {
public static String getS(String Str){

try{

byte b[]=Str.getBytes("ISO-8859-1");

Str=new String(b,"UTF-8");

}

catch(Exception ee){

ee.printStackTrace();

}

return Str;

}
//反向转换
public static String setS(String Str){

try{

byte b[]=Str.getBytes("UTF-8");

Str=new String(b,"ISO-8859-1");

}

catch(Exception ee){

ee.printStackTrace();

}

return Str;

}
}

此方法一般的都可解决,下面再给出一种在JSP页面使用<jsp:include>标签时的一种乱码解决方式:

示例代码如下:

/*****************************

*titleview.jsp

*****************************/

<%@page contentType="text/html;charset=utf-8" %>
<%@page pageEncoding="utf-8" %>

<html>
<head>

</head>
<body>
<center>
<table>
<tr>
<td width='70%' align='center'>
<font color='red' size='15'>

<% String name=request.getParameter("title");%>

<%=name%>
</font>
</td>

</tr>
</table>
<hr width='90%' >
</center>
</body>
</html>

/*****************************

*loginview.jsp

*****************************/

<%@page contentType="text/html;charset=utf-8" %>
<html>
<head></head>
<body>
<% request.setCharacterEncoding("utf-8");//最关键的便是此句,设置其字符方式 %>
<jsp:include page="/titleview.jsp" >
<jsp:param name="title" value="登录方式" />
</jsp:include>
<center>
<table >
<tr>
<td>
<a href='#'>管理员登录 </a>
</td>
</tr>
</table>
</center>
</body>
</html>

请看此句<% request.setCharacterEncoding("utf-8");//最关键的便是此句,设置其字符方式 %>,在代码中我已经进行了注释,当然,这只是其中一种方式,如果有兴趣还可以使用其它方法实现,此例只作为参考而已!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: