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");//最关键的便是此句,设置其字符方式 %>,在代码中我已经进行了注释,当然,这只是其中一种方式,如果有兴趣还可以使用其它方法实现,此例只作为参考而已!
下面给出一些简单的实用代码,只供参考:
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");//最关键的便是此句,设置其字符方式 %>,在代码中我已经进行了注释,当然,这只是其中一种方式,如果有兴趣还可以使用其它方法实现,此例只作为参考而已!
相关文章推荐
- Jsp表单提交数据乱码问题
- SSM框架Jsp页面POST提交的中文数据保存到数据库变成乱码问题的分析
- JSP数据提交中文乱码问题
- jsp中解决中文乱码问题--表单提交数据
- 解决前台jsp页面提交中文数据以后,数据库中乱码问题??
- 获取 客户端(浏览器)提交的数据乱码问题
- Android传统HTTP请求get----post方式提交数据(包括乱码问题)
- 我遇到的jsp表单提交乱码问题
- java中form以post、get方式提交数据中文乱码问题总结
- JSP+ MySQL中文乱码问题post提交乱码解决方案
- jsp连接MySQL数据库显示GIS地理数据乱码问题的解决(select AsText(the_geom))
- 网页java servlet JSP 提交表单乱码问题
- jsp连接MySQL数据库显示GIS地理数据乱码问题的解决(select AsText(the_geom))
- 在JSP中,解决get提交方式出现乱码的问题
- 解决Java Web开发中Jsp存储读取MySQL数据中文乱码的问题
- jenkins + mysql提交数据中的乱码问题解决办法
- JSP页面传值Action数据乱码问题
- jsp提交表单的乱码问题
- jsp数据提交至servlet中文乱码
- JSP+Servlet学习经验1:解决form表单提交后相应页面中文乱码的问题