首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

struts2.0中个get请求乱码,使用过滤器仍然乱码,帮帮了

2012-03-12 
struts2.0中个get请求乱码,使用过滤器仍然乱码,各位高手帮帮了监听类EncodeFilter.javaJava codepackage c

struts2.0中个get请求乱码,使用过滤器仍然乱码,各位高手帮帮了
监听类EncodeFilter.java

Java code
package com.qian.tools;import java.io.IOException;import java.io.UnsupportedEncodingException;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import org.apache.struts2.dispatcher.FilterDispatcher;import org.omg.CORBA.Request;import com.qian.mian.MainListener;public class EncodeFilter implements Filter{        private String edcoding=null;        private boolean ignore=true;           public void destroy() {             edcoding=null;        }           public void doFilter(ServletRequest request, ServletResponse response,                FilterChain filterChain) throws IOException, ServletException {            if(ignore==true||request.getCharacterEncoding()==null){                String encoding=setCharacterEncoding(request);               System.out.println("请求"+request.getParameter("sms.username"));                          if(encoding!=null){                    //request.setCharacterEncoding(encoding);                    response.setCharacterEncoding(encoding);                System.out.println("en:"+encoding);                }            }           System.out.println( request.getAttribute("username"));          filterChain.doFilter(request, response);        }           public void init(FilterConfig filterConfig) throws ServletException {            this.edcoding=filterConfig.getInitParameter("encoding");            String value=filterConfig.getInitParameter("ignore");           System.out.println(value);               if(value==null){                this.ignore=true;            }else if(value.equalsIgnoreCase("true")){                this.ignore=true;                            }else{                this.ignore=false;            }                       }        public String setCharacterEncoding(ServletRequest request){            return this.edcoding;        }}    

web.xml配置文件
XML code
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <filter>  <filter-name>encode</filter-name>  <filter-class>com.qian.tools.EncodeFilter</filter-class>  <init-param>   <param-name>encoding</param-name>   <param-value>UTF-8</param-value>  </init-param> </filter> <filter>  <filter-name>struts2</filter-name>  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  <init-param>   <param-name>encoding</param-name>   <param-value>UTF-8</param-value>  </init-param> </filter> <filter-mapping>  <filter-name>struts2</filter-name>  <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping>  <filter-name>encode</filter-name>  <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list>  <welcome-file>index.jsp</welcome-file> </welcome-file-list> <login-config>  <auth-method>BASIC</auth-method> </login-config></web-app>

struts.xml配置文件
XML code
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    "http://struts.apache.org/dtds/struts-2.0.dtd"><struts>    <constant name="struts.i18n.encoding" value="UTF-8"></constant>    <package name="struts2" namespace="/" extends="struts-default">        <action name="ltdx" class="com.qian.actions.LtDxTSAction">            <result name="success">/result.jsp</result>        </action>    </package></struts> 


index.jsp页面
Java code
<%@ page language="java" contentType="text/html; charset=UTF-8" %><%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title>My JSP 'index.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache">    <meta http-equiv="cache-control" content="no-cache">    <meta http-equiv="expires" content="0">        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    <meta http-equiv="description" content="This is my page">    <!--    <link rel="stylesheet" type="text/css" href="styles.css">    -->  </head>    <body>    <form action="ltdx.action" method="get">&nbsp;         username:<input style="text" name="sms.username"/><br/>        password:<input style="text" name="sms.password"/><br/>        <input type="submit"/>    </form>  </body></html>

result.jsp页面
Java code
<%@ page language="java" contentType="text/html; charset=UTF-8" %><%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>          <title>My JSP 'index.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache">    <meta http-equiv="cache-control" content="no-cache">    <meta http-equiv="expires" content="0">        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    <meta http-equiv="description" content="This is my page">    <!--    <link rel="stylesheet" type="text/css" href="styles.css">    -->  </head>    <body>    username:${sms.username }    password:${sms.password }  </body></html>


在页面显示乱码,在Action里面输出也是乱码啊,各位帮帮忙谢谢

[解决办法]
<form action="ltdx.action" method="post">
[解决办法]
通过get请求时过滤器是无用的,如果你使用的是tomcat请配置
%tomcat_home%/conf/server.xml
<Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
追加
URIEncoding="your encoder"

热点排行