jsp传递过来参数为中文,程序中接收过来是乱码,如何处理呢? 同时也要转到另一个页面,传递过去中文。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 写个过滤器类 ,在 Web.xml 中配置 就OK了 不是大问题 像LS说的说如果从大局看最后写个过滤器为后面的操作减少很多不必要的麻烦http://www.pmjava.com/blogview.asp?id=303http://www.pmjava.com/blogview.asp?id=187http://www.pmjava.com/blogview.asp?id=133 我把用户发送请求方式不同引起的中文问题划分了四种类型(这里未提及数据库):1、表单的get提交2、表单的post提交3、页面链接传递中文参数4、地址栏中参数直接输入中文提交lz可以看下http://www.javaeye.com/topic/300656 或者用post提交。如果非得用get方式提交,先转码,接收的时候再往回转码。 麻烦给我一份吧。email:[email protected] 中文外面加上java.net.URLEncoder("你的中文","utf-8"),编码根据你用的情况改 处理乱码的方法有很多最优的解决可以看下面文章http://blog.csdn.net/leves1989/archive/2008/11/08/3251253.aspx 写个过滤器类是最好的解决办法。还可以通过设置tomcat中的<connector>在这个里面添加URLEncoding="utf-8" 1、写个过滤器,可以解决大部分问题。2、javascript传参数前先URLEncoding,再传。3、最好所有页面都用utf-8 如果服务器是tomcat的话,可以修改配置文件。我博客上有! package com.elec.tool;import java.io.IOException;import javax.servlet.*;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class EncodingChange implements Filter{ protected String encoding=null; protected FilterConfig filterconfig=null; protected boolean ignore=true; public void destroy() { this.encoding=null; this.filterconfig=null; } public void doFilter(ServletRequest requests, ServletResponse responses, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub HttpServletRequest request=(HttpServletRequest)requests; HttpServletResponse response=(HttpServletResponse)responses; if(ignore||request.getCharacterEncoding()==null){ String encoding=selectEncoding(request); if(encoding!=null){ request.setCharacterEncoding(encoding); request.getSession().getAttribute("Login_Student"); request.getSession().getAttribute("Login_Teacher"); request.getSession().getAttribute("Login_Admin"); } } chain.doFilter(request, response); } public void init(FilterConfig filterconfig) throws ServletException { // TODO Auto-generated method stub this.filterconfig = filterconfig; this.encoding = filterconfig.getInitParameter("encoding"); String value = filterconfig.getInitParameter("ignore"); if (value == null) { this.ignore = true; } else if (value.equalsIgnoreCase("true")) { this.ignore = true; } else if (value.equalsIgnoreCase("yes")) { this.ignore = true; } else { this.ignore = false; } } public String selectEncoding(ServletRequest request){ return this.encoding; }}...........................web.xml<filter> <filter-name>setCharacterEncoding</filter-name> <filter-class> com.elec.tool.EncodingChange</filter-class> <init-param> <param-name>ignore</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>setCharacterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> public static final String toUnicode(String str){ try{ str= new String(str.getBytes("gb2312"),"iso-8859-1"); } catch(Exception e){ } return str}这个是一个公共的方法。把这个方法放在一个类中,在任何地方引用这个方法就可以了。这个方法是来处理中文里面的乱码。注意:统一字符串不能两次处理,不然又出现乱码了。 SSH中,采用SPRING配置式事务管理的一个疑惑??? JSP问题,请大家帮帮忙,谢谢了 同样的程序在windows2003和windowsXP下运行的效果不一样,郁闷中... 有关Struts2 [散分了] 根据客服端代码写服务器代码(多线程) 高分求解关于MYSQL和HIBERNATE连接发生错误问题,求解决方案 求救,JSP高手!! 怎样在客户端读写文本文件? Jsp调用Oracle存储过程返回参数的问题! 请教有关SQLServer2000的jdbc连接问题!!!!!!! 继续弱问:框架刷新的问题! WebService --- 急等
如果非得用get方式提交,先转码,接收的时候再往回转码。
麻烦给我一份吧。
email:[email protected]
http://blog.csdn.net/leves1989/archive/2008/11/08/3251253.aspx
2、javascript传参数前先URLEncoding,再传。
3、最好所有页面都用utf-8
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class EncodingChange implements Filter{
protected String encoding=null;
protected FilterConfig filterconfig=null;
protected boolean ignore=true;
public void destroy() {
this.encoding=null;
this.filterconfig=null;
} public void doFilter(ServletRequest requests, ServletResponse responses,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletRequest request=(HttpServletRequest)requests;
HttpServletResponse response=(HttpServletResponse)responses;
if(ignore||request.getCharacterEncoding()==null){
String encoding=selectEncoding(request);
if(encoding!=null){
request.setCharacterEncoding(encoding);
request.getSession().getAttribute("Login_Student");
request.getSession().getAttribute("Login_Teacher");
request.getSession().getAttribute("Login_Admin");
}
}
chain.doFilter(request, response);
}
public void init(FilterConfig filterconfig) throws ServletException {
// TODO Auto-generated method stub
this.filterconfig = filterconfig;
this.encoding = filterconfig.getInitParameter("encoding");
String value = filterconfig.getInitParameter("ignore");
if (value == null) {
this.ignore = true;
} else if (value.equalsIgnoreCase("true")) {
this.ignore = true;
} else if (value.equalsIgnoreCase("yes")) {
this.ignore = true;
} else {
this.ignore = false;
}
}
public String selectEncoding(ServletRequest request){
return this.encoding;
}
}
...........................web.xml<filter>
<filter-name>setCharacterEncoding</filter-name>
<filter-class>
com.elec.tool.EncodingChange
</filter-class>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param> <init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>setCharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
str= new String(str.getBytes("gb2312"),"iso-8859-1"); } catch(Exception e){
} return str
}
这个是一个公共的方法。把这个方法放在一个类中,在任何地方引用这个方法就可以了。这个方法是来处理中文里面的乱码。注意:统一字符串不能两次处理,不然又出现乱码了。