javaweb Filter在工作中的簡單應用[從學習到工作(三)]
阿新 • • 發佈:2018-11-28
一.Filter對亂碼問題的處理
web中的配置
java程式<filter> <filter-name>EncodingFilter</filter-name> <filter-class>com.test.filter.EncodeFilter</filter-class> <init-param> <param-name>Encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>EncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
package com.test.filter; 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 javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; public class EncodeFilter implements Filter{ String encode = null; public void init(FilterConfig filterConfig) throws ServletException { encode = filterConfig.getInitParameter("Encoding"); System.out.println("Filter init....."); } public void destroy() { System.out.println("Filter destroy....."); } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; if(req.getMethod().equals("GET")) { //Get方式處理亂碼 request = new MyRequest(req); } else { //post方式處理亂碼 request.setCharacterEncoding(encode); } response.setContentType("text/html;charset="+encode); //放行 chain.doFilter(request, response); } /** * 內部類裝飾request物件 * @author Administrator * */ class MyRequest extends HttpServletRequestWrapper { public MyRequest(HttpServletRequest request) { super(request); } @Override public String getParameter(String name) { String value = super.getParameter(name); if (value != null) { try { value = new String(value.getBytes("ISO8859-1"), encode); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } return value; } @Override public String[] getParameterValues(String name) { String[] values = super.getParameterValues(name); if (values != null && values.length > 0) { for (int i = 0; i < values.length; i++) { String value = values[i]; try { values[i] = new String(value.getBytes("ISO8859-1"), encode); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } } return values; } } }
二.Filter在許可權控制中的應用(登入許可權)
web中filter的註冊
<filter> <filter-name>LoginFilter</filter-name> <filter-class>com.test.filter.LoginFilter</filter-class> </filter> <filter-mapping> <filter-name>LoginFilter</filter-name> <url-pattern>/jsps/*</url-pattern> </filter-mapping>
java程式碼
package com.test.filter;
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;
import javax.servlet.http.HttpSession;
import com.test.model.User;
public class LoginFilter implements Filter{
public void init(FilterConfig arg0) throws ServletException {
}
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse rep = (HttpServletResponse) response;
HttpSession session = req.getSession();
//session中取出User物件
User user = (User) session.getAttribute("user");
if(user != null) {
//放行
chain.doFilter(request, response);
} else {
//客戶端跳轉
rep.sendRedirect("/index.jsp");
//服務端跳轉
//req.getRequestDispatcher("/index.jsp").forward(request, response);
}
}
}