JSP数据提交乱码问题

 原理不说了,网上有很多,都可以自己查到,至于为什么会出现乱码,是因为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");//最关键的便是此句,设置其字符方式 %>,在代码中我已经进行了注释,当然,这只是其中一种方式,如果有兴趣还可以使用其它方法实现,此例只作为参考而已!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值