jsp表单提交中文字符,request对象获取时乱码解决方法
第一种:
在request对象获取页面Charset中的“C”大写,且页面无中文字符,最好用英文,否则MyEclipse无法保存此页面编码(表单页面能够不大写“C”)
<%@ page contentType="text/html;Charset=GB2312" %>
另外一种:
tomcat4.x支持中文传码。但5.x不支持。假设用5.0以上的版本号就得转码了。
就是这样的格式
<%@ page contentType="text/html;Charset=GB2312" %>
另外一种:
tomcat4.x支持中文传码。但5.x不支持。假设用5.0以上的版本号就得转码了。
就是这样的格式
<%@ page contentType="text/html;charset=GB2312" %>
String strKeyWords=new String(request.getParameter("key_words").getBytes("iso8859_1"),"gb2312");
这个是我们的项目解决sqlserver中文乱码的方案。能够试试看
--------------------------------------------------------------------------
第三种:
在 jsp、servlet中 申明这种方法转换一下: (表单JSP页面的编码一定要GB2312,且 charset中的“c”小写。此方法JSP页面就能够写中文字符
在 jsp、servlet中 申明这种方法转换一下: (表单JSP页面的编码一定要GB2312,且 charset中的“c”小写。此方法JSP页面就能够写中文字符
即:<%@
page contentType="text/html;charset=GB2312" %>)
<%!public String handleString(String str){ try{ byte bb[]=str.getBytes("ISO-8859-1"); str=new String(bb); }catch(Exception e){ System.out.println("字符转换失败"+e); } return str; } %> <% String logname=request.getParameter("參数ID"); logname=handleString(logname); %>
第四种:
在request对象获取提交的页面开头写(注意:此方法表单提交方式仅仅能是post,并且表单页码的编码方式和request.setCharacterEncoding("utf-8");
必须保持一致)
<%@
page contentType="text/html; charset=utf-8"%>
<% request.setCharacterEncoding("utf-8"); %>
<% request.setCharacterEncoding("utf-8"); %>
第一行是页面编码方式
第二行是传值编码方式
表单页面
<%@ page contentType="text/html; charset=utf-8"%> <html> <body> <form method="post" action="2.jsp"> <div align="center"> <input type="text" name="name"> <input type="submit" name="Submit" value="Submit"> </div> </form> </body> </html> </span>
request对象获取页面
<%@ page contentType="text/html; charset=utf-8"%> <% request.setCharacterEncoding("utf-8"); %> <html> <body> <%=request.getParameter("name")%> </body> </html></span>
版权声明:本文博主原创文章,博客,未经同意不得转载。