詳解Spring中的CharacterEncodingFilter過濾器,過濾url請求
在專案中有很多讓人頭疼的問題,其中,編碼問題位列其一,那麼在Spring框架中是如何解決從頁面傳來的字串的編碼問題的呢?下面我們來看看Spring框架給我們提供過濾器CharacterEncodingFilter
1.看清結構:
可以看到其繼承GenericFilterBean和OncePerRequestFilter,也就是說,這個過濾器就是針對於每次瀏覽器請求進行過濾的,然後再其之上添加了父類沒有的功能即處理字元編碼。
2.官方解釋:
Servlet 2.3/2.4 Filter that allows one to specify a character encoding for requests. This is useful because current browsers typically do not set a character encoding even if specified in the HTML page or form.
This filter can either apply its encoding if the request does not already specify an encoding, or enforce this filter's encoding in any case ("forceEncoding"="true").(只要你設定了foreEncoding=true,則在程式碼中設定編碼格式沒用,)In the latter case, the encoding will also be applied as default response encoding on Servlet 2.4+ containers (although this will usually be overridden by a full content type set in the view).
3.如何使用
下面來看看如何在web.xml中配置:
Xml程式碼- <filter>
- <filter-name>encodingFilter</filter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter
- </filter-class>
- <init-param>
- <param-name>encoding</param-name>
-
<param-value
- </init-param>
- <init-param>
- <param-name>forceEncoding</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
其中encoding用來設定編碼格式,forceEncoding用來設定是否理會 request.getCharacterEncoding()方法,設定為true則強制覆蓋之前的編碼格式。
4.原始碼賞析
當Servlet容器啟動的時候,會讀取web.xml中對於過濾器的配置資訊, 讀取到<init-param>中的子標籤<param-name>encoding和forceEncoding所對應的<param-value>的值,再通過呼叫該類setEncoding(String encoding)和setForceEncoding(boolean
forceEncoding) 將值注入到這連個欄位中。
在這裡就能看到為什麼設定foreEncoding為true會覆蓋掉request.getCharacterEncoding()中的方法了吧,呵呵,原始碼之前了無祕密,只有深入到原始碼之中才能看清本質。
專案過程中用到的過濾器:
- <?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">
-
<filter>
<filter-name>Spring character encoding filter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Spring character encoding filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>SystemFilter</filter-name>
<filter-class>
com.cpinfo.his.filter.SystemFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>SystemFilter</filter-name>
<url-pattern>*.htm</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> - </web-app>
2)處理
-
import
com.cpinfo.his.tables.Bas_user;
import java.io.IOException;
import java.io.PrintStream;
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;
public class SystemFilter
implements Filter
{
static String[] EXCEPTURL = { "login.htm", "checkUser.htm", "getDepts.htm", "getShaynes", "number/skin_testshow.htm", "getAllSkintest", "checkSkin", "number/getPost" };
public void init(FilterConfig arg0)
throws ServletException
{
long daySpan = 86400000L;
}
public void destroy()
{
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException
{
HttpServletRequest req = (HttpServletRequest)request;
HttpServletResponse resp = (HttpServletResponse)response;
HttpSession session = req.getSession(true);
String menuid = request.getParameter("menuid");
if ((menuid != null) && (!menuid.equals("")))
{
session.setAttribute("systemid", menuid.substring(0, 2));
session.setAttribute("menuid", menuid);
}
if (checkUrl(req)) {
Bas_user user = (Bas_user)session.getAttribute("login_user");
String url = req.getRequestURI();
if ((user == null) && (url.indexOf("/his/scan/") == -1)) {
String basePath = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + req.getContextPath() + "/login.htm";
resp.sendRedirect(basePath);
}
else {
chain.doFilter(request, response);
}
} else {
chain.doFilter(request, response);
}
System.out.println("------->url" + req.getRequestURI());
}
boolean checkUrl(HttpServletRequest req)
{
for (String obj : EXCEPTURL)
{
if (req.getRequestURI().indexOf(obj) > 0)
{
return false;
}
}
return true;
}
}