過濾器filter處理中文亂碼
阿新 • • 發佈:2019-02-17
我部署了一個Java web應用,專案全部採用utf-8編碼,在web.xml中配置了兩個頁面需要經過過濾器的登入和許可權判定處理,其他頁面不做處理,如下:
<init-param>
<param-name>filterPages</param-name>
<param-value>/main.html,/admin/adminMain.html</param-value>
</init-param>
</filter>
啟動tomcat後,開始都能正常訪問,也沒有亂碼。
於是乎,我某天在ie下將訪問main.html時,郵件將編碼從utf-8改為了gbk,出現了亂碼,然後我又改回了utf-8,此時頁面的title和頁面上的兩個超連結上的文字都變成了亂碼:
伺服器上的檔案編碼都是正常的,我在未手動修改瀏覽器中頁面編碼前都正常,就是改了後才出現該問題,而且自從我改了以後在其他人的電腦上訪問也有這個問題,實在不明白我在瀏覽器上做了修改怎麼就影響到伺服器上了呢?
重啟tomcat,重新部署專案都沒用,今天將main.html檔案重新命名為a.html後再訪問又一切正常,才發現應該是Filter搞的鬼,可是我在過濾器中也沒做什麼特殊處理,為何如此呢?Filter的程式碼如下:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException
{
HttpServletRequest httpRequest = (HttpServletRequest)request;
String rootPath=httpRequest.getContextPath();
String path = httpRequest.getServletPath();
System.out.println(path);
if(StringUtil.splitAllToArrayList(filterPages).contains(path)){
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
HttpSession session = httpRequest.getSession();
String isLogin = (String) session.getAttribute("isLogin");
if(isLogin == null || !isLogin.equals("yes")){
out.print("<script type=\"text/javascript\">");
out.print("alert('您還沒有登入,請先登入!');window.location='"+rootPath+"/login.html';");
out.print("</script>");
}
else{
String userAccount = (String) session.getAttribute("userAccount");
Dao dao = DaoFactory.getDao();
List<Role> roles =dao.query(Role.class, Cnd.wrap("userAccount='"+userAccount+"'"));
List<String> roleNames = new ArrayList<>();
for (Role role : roles) {
roleNames.add(role.getRole());
}
//登入後直接訪問該地址時驗證
if("/admin/adminMain.html".equals(path) && ! roleNames.contains("administrator")){
out.print("<script type=\"text/javascript\">");
out.print("alert('您沒有訪問許可權!');window.location='"+rootPath+"/login.html';");
out.print("</script>");
}else
chain.doFilter(request, response);
}
out.close();
}else {
chain.doFilter(request, response);
}
}
<init-param>
<param-name>filterPages</param-name>
<param-value>/main.html,/admin/adminMain.html</param-value>
</init-param>
</filter>
啟動tomcat後,開始都能正常訪問,也沒有亂碼。
於是乎,我某天在ie下將訪問main.html時,郵件將編碼從utf-8改為了gbk,出現了亂碼,然後我又改回了utf-8,此時頁面的title和頁面上的兩個超連結上的文字都變成了亂碼:
伺服器上的檔案編碼都是正常的,我在未手動修改瀏覽器中頁面編碼前都正常,就是改了後才出現該問題,而且自從我改了以後在其他人的電腦上訪問也有這個問題,實在不明白我在瀏覽器上做了修改怎麼就影響到伺服器上了呢?
重啟tomcat,重新部署專案都沒用,今天將main.html檔案重新命名為a.html後再訪問又一切正常,才發現應該是Filter搞的鬼,可是我在過濾器中也沒做什麼特殊處理,為何如此呢?Filter的程式碼如下:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException
{
HttpServletRequest httpRequest = (HttpServletRequest)request;
String rootPath=httpRequest.getContextPath();
String path = httpRequest.getServletPath();
System.out.println(path);
if(StringUtil.splitAllToArrayList(filterPages).contains(path)){
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
HttpSession session = httpRequest.getSession();
String isLogin = (String) session.getAttribute("isLogin");
if(isLogin == null || !isLogin.equals("yes")){
out.print("<script type=\"text/javascript\">");
out.print("alert('您還沒有登入,請先登入!');window.location='"+rootPath+"/login.html';");
out.print("</script>");
}
else{
String userAccount = (String) session.getAttribute("userAccount");
Dao dao = DaoFactory.getDao();
List<Role> roles =dao.query(Role.class, Cnd.wrap("userAccount='"+userAccount+"'"));
List<String> roleNames = new ArrayList<>();
for (Role role : roles) {
roleNames.add(role.getRole());
}
//登入後直接訪問該地址時驗證
if("/admin/adminMain.html".equals(path) && ! roleNames.contains("administrator")){
out.print("<script type=\"text/javascript\">");
out.print("alert('您沒有訪問許可權!');window.location='"+rootPath+"/login.html';");
out.print("</script>");
}else
chain.doFilter(request, response);
}
out.close();
}else {
chain.doFilter(request, response);
}
}