過濾器的使用

過濾器:html

package app.java.filter.demo1;
 
import java.io.IOException;
 
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
 * 當前Filter用於解決全站中文亂碼問題.
 * @author JYL
 */
public class EncodingFilter implements Filter {
 
    public void destroy() {
        
    }
 
    public void doFilter(ServletRequest req, ServletResponse resp,
            FilterChain chain) throws IOException, ServletException {
        
        //1 進行Request與Response對象的類型強轉
        HttpServletRequest request = (HttpServletRequest)req;
        HttpServletResponse response = (HttpServletResponse)resp;
        
        //2 解決中文亂碼問題:1)請求的中文亂碼(GET\POST);2)響應的中文亂碼.
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        
        //3 建立自定義的Request對象
        MyRequest myRequest = new MyRequest(request);
        
        //3 放行
        chain.doFilter(myRequest, response);
    }
 
    public void init(FilterConfig arg0) throws ServletException {
    }
 
}
View Code

servlet:java

package app.java.filter.demo1;
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class TestServlet extends HttpServlet {
 
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
 
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        
        System.out.println(username+" : "+password);
        
        response.getWriter().println("中文解決了,真好!!!!");
    }
 
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
 
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        
        System.out.println(username+" : "+password);
        
        response.getWriter().println("中文解決了,真好!!!!");
    }
 
}
View Code

Request:app

package app.java.filter.demo1;
 
import java.io.UnsupportedEncodingException;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
/**
 * 自定義的Request對象
 * @author JYL
 */
public class MyRequest extends HttpServletRequestWrapper {
 
    public MyRequest(HttpServletRequest request) {
        super(request);
    }
    
    @Override
    public String getParameter(String name) {
        String value = super.getParameter(name);
        if(super.getMethod().equalsIgnoreCase("GET")){
            try {
                value = new String(value.getBytes("ISO-8859-1"),"utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return value;
    }
 
}
View Code

xml:jsp

<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>demo1.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
View Code

jsp:ide

<form action="test" method="post">
用戶名:<input type="text" name="username"><br>
密碼:<input type="password" name="password"><br>
<input type="submit" value="提交">
</form>
<a href="test?username=張無忌&password=123">GET方式發送的請求</a>
View Code
相關文章
相關標籤/搜索