求javaEE开发中乱码解决方案(jsp,ssh)!! 求struts乱码解决方案! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用一下filter啦public class SetCharacterEncodingFilter implements Filter { protected String encoding = null; protected String igno = null; protected FilterConfig filterConfig = null; protected boolean ignore = true; public void destroy() { this.encoding = null; this.filterConfig = null; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest rq=(HttpServletRequest) request; RequestDispatcher rd = rq.getRequestDispatcher("/"); if(!rq.getRequestURI().equals("/")&&!rq.getRequestURI().equals("/perso.do")&&rq.getSession().getAttribute("diquid")==null){ rq.setAttribute("msg", "超时,请重新登录"); rd.forward(request, response); } if (ignore || (request.getCharacterEncoding() == null)) { String encoding = selectEncoding(request); if (encoding != null) { request.setCharacterEncoding(encoding); } } chain.doFilter(request, response); }web.xml <filter> <filter-name>EncodingFilter</filter-name> <filter-class>util.SetCharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>ignore</param-name> <param-value>true</param-value> </init-param> </filter>20分太少了 我自己总结的表单传值post方法传值: request.setCharacterEncoding("gbk")get方法传值: new String(request.getParameter("参数名").getBytes("iso-8859-1"))地址传值跟表单get方法传值一样 new String(request.getParameter("参数名").getBytes("iso-8859-1"))get方法传值和地址传值都是通过在项目中编码解决乱码问题,可以在配置文件中设置编码,这样就可以不用在项目中编码来解决了。在Tomcat 6.0\conf\server.xml文件中改,如下是改好的节点,主要添加URIEncoding="gbk"属性<Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="GBK"/>注意:修改配置文件解决不了post方法传值的乱码问题。<jsp:param>动作传值 传值的时候先编码: <jsp:param name="name" value="<%=java.net.URLEncoder.encode("中文信息","gbk") %>"/> 得值的时候再解码: <%=java.net.URLDecoder.decode(request.getParameter("name")) %>上面这个解决乱码例子后来发现可以用request.setCharacterEncoding("gbk")这个来解决。总结:乱码解决的时候要注意乱码之前是什么码转什么码,然后根据了解到的再去转例如:GBK - ISO-8859-1 - UTF-8 现在的码是UTF-8,然后转回GBK的话,要注意一步一步转回去。 这些都不得行呢??jsp可以filter 过滤器搞 可是struts他好像例外了,不得行。我也想到了从页面中接收到参数,然后修改该参数的字符集,我一修改,struts就不能运行了,系统报出了一个异常 java转义字符双引号 我发现自己被骗了好多次啊 基于SSH架构的 电子商务网站 需求分析书 HIBERNATE级连出了个问题。。。 谁给翻译一下,或者解释一下下面的内容 STRUTS国际化有意义吗? 如何将html转换成rtf格式?有没有现成的api??? tomcat启动不起来,启动后 自动退出,怎么样能查看到错误信息??? 换了家新公司,开始学EJB,高兴,散分... js里面eval这方法怎么用的呀 同一台机器布署多个ActiveMQ的问题 怎样用推技术实现即时信息?
protected String igno = null; protected FilterConfig filterConfig = null; protected boolean ignore = true;
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest rq=(HttpServletRequest) request;
RequestDispatcher rd = rq.getRequestDispatcher("/");
if(!rq.getRequestURI().equals("/")&&!rq.getRequestURI().equals("/perso.do")&&rq.getSession().getAttribute("diquid")==null){
rq.setAttribute("msg", "超时,请重新登录");
rd.forward(request, response);
}
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null) {
request.setCharacterEncoding(encoding);
}
}
chain.doFilter(request, response);
}web.xml <filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>util.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>20分太少了
post方法传值:
request.setCharacterEncoding("gbk")
get方法传值:
new String(request.getParameter("参数名").getBytes("iso-8859-1"))地址传值跟表单get方法传值一样
new String(request.getParameter("参数名").getBytes("iso-8859-1"))get方法传值和地址传值都是通过在项目中编码解决乱码问题,可以在配置文件中设置编码,这样就可以不用在项目中编码来解决了。
在Tomcat 6.0\conf\server.xml文件中改,如下是改好的节点,主要添加URIEncoding="gbk"属性
<Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="GBK"/>
注意:修改配置文件解决不了post方法传值的乱码问题。
<jsp:param>动作传值
传值的时候先编码:
<jsp:param name="name" value="<%=java.net.URLEncoder.encode("中文信息","gbk") %>"/>
得值的时候再解码:
<%=java.net.URLDecoder.decode(request.getParameter("name")) %>
上面这个解决乱码例子后来发现可以用request.setCharacterEncoding("gbk")这个来解决。总结:乱码解决的时候要注意乱码之前是什么码转什么码,然后根据了解到的再去转
例如:GBK - ISO-8859-1 - UTF-8 现在的码是UTF-8,然后转回GBK的话,要注意一步一步转回去。