1.字元編碼過濾器 實現功能,在a.jsp中填寫用戶名提交到b.jsp,在b.jsp中讀取參數名。 a.jsp b.jsp 若需要讀取參數的頁面太多,需要在每一個頁面都添加<% request.setCharacterEncoding("UTF-8");%>,該方法行不通。字元編碼過濾器通過配置參數 ...
1.字元編碼過濾器
實現功能,在a.jsp中填寫用戶名提交到b.jsp,在b.jsp中讀取參數名。
a.jsp
<body> <form action="encoding/b.jsp" method="post"> name:<input type="text" name="username"/> <input type="submit" value="Submit"/> </form> </body>
b.jsp
<body> <% request.setCharacterEncoding("UTF-8");%> hello:${param.username} </body>
若需要讀取參數的頁面太多,需要在每一個頁面都添加<% request.setCharacterEncoding("UTF-8");%>,該方法行不通。字元編碼過濾器通過配置參數encoding指明使用何種字元編碼,以處理Html Form請求參數的中文問題。
HttpFilter.java
package com.javaweb.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; public abstract class HttpFilter implements Filter { @Override public void destroy() { } /* * 原生的doFilter方法,在方法內部把ServletRequest和ServletResponse轉為了HttpServletRequest和HttpServletResponse, * 並調用了doFilter(HttpServletRequest request,HttpServletResponse response,FilterChain filterchain)方法。 * 若編寫Filter的過濾方法不建議直接繼承該方法,而建議繼承doFilter(HttpServletRequest request,HttpServletResponse response, * FilterChain filterchain)方法。 * */ @Override public void doFilter(ServletRequest req, ServletResponse resp, FilterChain filterchain) throws IOException, ServletException { HttpServletRequest request=(HttpServletRequest) req; HttpServletResponse response=(HttpServletResponse) resp; doFilter(request,response,filterchain); } //抽象方法,為Http請求定製必須實現的方法。 public abstract void doFilter(HttpServletRequest request,HttpServletResponse response,FilterChain filterchain) throws IOException, ServletException; private FilterConfig filterconfig; //不建議子類直接覆蓋,若直接覆蓋,將可能會導致filterConfig成員變數初始化失敗。 @Override public void init(FilterConfig filterconfig) throws ServletException { this.filterconfig =filterconfig; init(); } //供子類繼承的初始化方法,可以通過getFilterConfig()獲取FilterConfig對象。 public void init(){} //直接返回init(ServletConfig)的ServletConfig對象。 public FilterConfig getFilterConfig(){ return filterconfig; } }
encodingFilter.java
package com.javaweb.Filter; import java.io.IOException; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.javaweb.Filter.HttpFilter; public class encodingFilter extends HttpFilter { @Override public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain filterchain) throws IOException, ServletException { //2.指定請求的字元編碼為1中讀取的字元編碼 request.setCharacterEncoding(encoding); //3.調用chain.doFile()方法放行請求 filterchain.doFilter(request,response); } private String encoding; public void init(){ //1.讀取配置文件web.xml中的字元編碼方式 encoding=getFilterConfig().getServletContext().getInitParameter("encoding"); } }
在web.xml中進行配置,首先指定當前頁的字元編碼:
<context-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </context-param>
配置encodingFilter,
<filter> <filter-name>encodingFilter</filter-name> <filter-class>com.javaweb.Filter.encodingFilter</filter-class> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/encoding/*</url-pattern> </filter-mapping>
這樣,不用在b.jsp頁面指定<% request.setCharacterEncoding("UTF-8");%>也不會出現輸入中文的亂碼問題。
2.檢測用戶是否登錄過的過濾器
系統中的某些頁面只有在正常登錄後才可以使用,用戶請求這些頁面時要檢查session中有無該用戶的信息,但在所有的頁面加上session的判斷相當麻煩。因此需要重新編寫一個用於檢測用戶是否登錄的過濾器,如果用戶未登錄,則重定向到指定的登錄頁面。
示例代碼:
a.jsp
<body> <h4>AAA page</h4> <a href="login/list.jsp">return list...</a> </body>
b.jsp
<body> <h4>BBB page</h4> <a href="login/list.jsp">return list...</a> </body>
c.jsp
<body> <h4>CCC page</h4> <a href="login/list.jsp">return list...</a> </body>
d.jsp
<body> <h4>DDD page</h4> <a href="login/list.jsp">return list...</a> </body>
e.jsp
<body> <h4>EEE page</h4> <a href="login/list.jsp">return list...</a> </body>
list.jsp
<body> <a href="login/a.jsp">AAA</a> <br><br> <a href="login/b.jsp">BBB</a> <br><br> <a href="login/c.jsp">CCC</a> <br><br> <a href="login/d.jsp">DDD</a> <br><br> <a href="login/e.jsp">EEE</a> <br><br> </body>
login.jsp
<body> <form action="login/doLogin.jsp" method="post"> username:<input type="text" name="username"/> <input type="submit" value="Submit"/> </form> </body>
doLogin.jsp
<body> <% String username=request.getParameter("username"); if(username!=null){ session.setAttribute(application.getInitParameter("sessionKey"),username); response.sendRedirect("list.jsp"); }else{ response.sendRedirect("login.jsp"); } %> </body>
loginFilter.java
package com.javaweb.Filter; import java.io.IOException; import java.util.Arrays; import java.util.List; import javax.servlet.FilterChain; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.javaweb.Filter.HttpFilter; public class loginFilter extends HttpFilter { String sessionKey; String redirectUrl; String uncheckedUrls; @Override public void init() { ServletContext servletcontext=getFilterConfig().getServletContext(); sessionKey=servletcontext.getInitParameter("sessionKey"); redirectUrl=servletcontext.getInitParameter("redirectPage"); uncheckedUrls=servletcontext.getInitParameter("uncheckedPage"); } @Override public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain filterchain) throws IOException, ServletException { //b.jsp c.jsp d.jsp ... String servletPath=request.getServletPath(); //檢查uncheckedUrls是否包含請求的URL,若包含,放行 List<String> Urls=Arrays.asList(uncheckedUrls.split(",")); if (Urls.contains(servletPath)){ filterchain.doFilter(request, response); return; } //從sessionKey中檢查是否包含sessionKey對應的值,若值不存在,則重定向到redirectUrl Object user=request.getSession().getAttribute(sessionKey); if (user==null){ response.sendRedirect(request.getContextPath()+redirectUrl); return; } //若存在,則放行 filterchain.doFilter(request, response); } }
在web.xml中進行配置:
<context-param> <param-name>redirectPage</param-name> <param-value>/login/login.jsp</param-value> </context-param> <context-param> <param-name>uncheckedPage</param-name> <param-value>/login/a.jsp,/login/doLogin.jsp,/login/list.jsp,/login/login.jsp</param-value> </context-param> <context-param> <param-name>sessionKey</param-name> <param-value>sessionValue</param-value> </context-param>
<filter>
<filter-name>loginFilter</filter-name>
<filter-class>com.javaweb.Filter.loginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>loginFilter</filter-name>
<url-pattern>/login/*</url-pattern>
</filter-mapping>
AAA頁面不需要登錄即可訪問,
點擊BBB(及之後的)超鏈接時,跳轉到登錄頁面:
填寫登錄名之後再訪問BBB,可以跳轉到BBB頁面: