当前位置: 代码迷 >> Java Web开发 >> 关于字符编码的有关问题
  详细解决方案

关于字符编码的有关问题

热度:82   发布时间:2016-04-17 15:34:51.0
关于字符编码的问题
现象:表单提交的内容含有中文的时候,接受表单的JSP无法正确显示提交的中文

表单代码如下:
<html>
    <head>
        <meta   http-equiv= "Content-Type "   content= "text/html;   charset=utf-8 "   />
    <head>
<body>
<form   method=get   action= "sessiontest2.jsp ">
姓名:
<input   type=text   name= "username "/>
<input   type=submit   value= "submit "/>
</form>
</body>
</html>

接受表单的JSP代码:
<%@   page   contentType= "text/html;charset=utf-8 "   language= "java "   %>
<html>
<head>
</head>
<body>
您好
<%
String   username   =   request.getParameter( "username ");
session.setAttribute( "username ",   username);
%>
<%=   username   %>

</form>
</body>
</html>



------解决方案--------------------
试试用method=POST
------解决方案--------------------
request.sertCharactorEncoding( "utf-8 ");
------解决方案--------------------
影子说的方法试过,在servlet里面可行,但是在JSP中乱码依旧……
------解决方案--------------------
<%@ page contentType= "text/html; charset=GBK "%>
<meta http-equiv= "Content-Type " content= "text/html; charset=GBK ">
request.sertCharactorEncoding( "GBK ");

三个地方
------解决方案--------------------
你要看看你的IE的默认编码是哪种,必须一样才可以
------解决方案--------------------
应该是可以的

------解决方案--------------------
> 水户
同一个JSP文件里设置三次吗?

> IT Farmer
我用的FireFox 2,IE被我剥离了……


题外:
如果使用MVC结构的话是不是就没有这类问题了?
------解决方案--------------------
<%@ page language= "java " import= "java.util.* " pageEncoding= "GBK "%>

在jsp开头加上上面一段代码!


pageEncoding= "GBK " 是有关编码的
------解决方案--------------------
> sjjga
引入util包有什么作用?
------解决方案--------------------
我发现的是在form标记中加入accept-charset= "iso-8859-1 "能够让JSP回显中文,但是觉得这样比较麻烦,有没有其他解决办法
------解决方案--------------------
<%
String username = request.getParameter( "username ");
%>
<%= username %>
<%
session.setAttribute( "username ", username);
%>
你这样试试,告诉我结果...

------解决方案--------------------
<form method=get action= "sessiontest2.jsp "> 如果没有特殊需要method改为post.
再用 <%request.sertCharactorEncoding( "GB2312 ");%> 试试
------解决方案--------------------
我输入的是“到底”
结果……

???×
------解决方案--------------------
  相关解决方案