關於Java過濾器處理表單中文亂碼

作者:希望我的代碼能解決你的問題

jsp頁面代碼

index.jsp頁面

<form action="Login" method="get">
        <input type="text" name="name"  placeholder="用戶名" />
        <br />
        <input type="password" name="pwd" placeholder="密碼"/>
        <input type="submit" value="登陸">
</form>

show.jsp代碼

<body>
    登陸成功!
</body>

servlet代碼

public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

            String oldName = request.getParameter("name");
            String oldPwd = request.getParameter("pwd");
            //檢查是否轉碼成功
            System.out.println(oldName+"==================");
            if("週週".equals(oldName)&&"123456".equals(oldPwd)){
                response.sendRedirect("show.jsp");
            }
    }

過濾器代碼塊

public class EncodeFilter implements Filter{
    //編碼
    private String encode ;
    public void destroy() {
        // TODO Auto-generated method stub
        this.encode=null;
    }

    public void doFilter(ServletRequest req, ServletResponse res,
            FilterChain filter) throws IOException, ServletException {
        // TODO Auto-generated method stub
        // 轉爲HttpServletRequest對象
        HttpServletRequest request = (HttpServletRequest)req;
        // 重新獲得編碼後的請求:
        HttpServletRequest newReq =null;
        // 提交方式
        String method = request.getMethod();
        // 提交方式判斷
        if("POST".equals(method)){
            // post方式
            request.setCharacterEncoding(this.encode);
        }else{
            // GET方式
            newReq = new HttpServletRequestWrapper(request){
                // 重寫方法getParameter方法
                @Override
                public String getParameter(String name){
                    String value = super.getParameter(name);
                    String newValue =null;
                    try {
                        if(value!=null){
                        newValue = new     String(value.getBytes("iso-8859-1"),encode);
                        }
                    } catch (Exception e) {
                        // TODO: handle exception
                        System.out.println("轉碼失敗");
                    }
                    return newValue;
                }
            };
        }
        // 這裏直接用HttpservletRequest的父類ServletRequest 
        filter.doFilter(req, res);
    }

    public void init(FilterConfig config) throws ServletException {
        // TODO Auto-generated method stub
        // 獲得編碼格式
        this.encode=config.getInitParameter("encoding");
    }

}

web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <servlet-name>Login</servlet-name>
    <servlet-class>com.###.servlet.Login</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Login</servlet-name>
    <url-pattern>/Login</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <!-- 編碼 -->
  <filter>
        <filter-name>encode</filter-name>
        <!--包名加上類名-->
        <filter-class>com.###.filter.EncodeFilter</filter-class>
        <init-param>
                <!-- 參數名 -->
                <param-name>encoding</param-name>
                <!-- 編碼參數 -->
                <param-value>UTF-8</param-value>
        </init-param>
  </filter>
  <filter-mapping>  
            <filter-name>encode</filter-name>
            <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章