日期:2014-05-20  浏览次数:20732 次

JSP乱码
请问一下怎么是乱码呢 源文件
HTML code

<head>
<meta http-equiv="Content-Type" content="text/html;charset=gbk">
</head>
<body>
请输入三遍长度,输入数字用逗号分隔
<form action="triangle.jsp" method="post" name="form">
    <input type="text" name="booy" value="示例:1,3,5">
    <input type="submit" value="t提交" name="submit">
</form>
<%!    String[] str = new String[3]; 
    
%>

<%    

    
    String s = request.getParameter("booy");
    if(s!=null){
        str = s.split(",");
        for(int i=0;i<str.length;i++){
            out.write(str[i]+";");
        }
    }
    
%>
</body>
</html>


访问页面:

提交后页面:

原来没有<meta http-equiv="Content-Type" content="text/html;charset=gbk">
是乱码 加上这句还是乱
改成gb2312还是挺乱

------解决方案--------------------
在 String s = request.getParameter("booy");
Java code

request.setCharacterEncoding("GBK");

------解决方案--------------------
首先要保证文件和编码要一致
静态文件要注意编码是ANSI还是Unicode还是utf-8
一般GBK类用ANSI、utf-8就用utf-8
jsp可以用注解<%@ page pageEncoding="GBK" %>规定输出的html文件编码格式
接收post参数要设置编码 request.setCharacterEncoding("GBK");