前兩篇已經簡單為大家介紹了一下,有關Filter接口的知識,本篇就讓我們以一個登錄小功能,來具體實現一下過濾器的作用,便于大家掌握。具體為大家介紹一下如何使用Filter對訪問進行過濾,及如何防止中文亂碼的問題,內容不多,大家只要簡單一練習便可以掌握。
1、登錄表單:
和一般的表單沒有任何區別,大家可以新建一個Login.jsp作為登錄界面,在其中添加一個表單即可。
<body> <center> <form method="post" action="<%=request.getContextPath() %>/servlet/login" enctype="application/x-www-form-urlencoded"> 姓名:<input type="text" name="name"> 密碼:<input type="passWord" name="pwd"> <input type="submit" value="登錄"> <input type="reset" value="重置"> </form> </center> </body>
2、select:
既然是登錄,就一定少不了我們select類,因為我們僅僅討論過濾器的作用,在這里就不再連接數據庫了,大家如有需要可以參看我的前幾篇博客,對于數據庫連接,有詳細的講解。我們的select代碼:
public class login extends HttpServlet { /** * Constructor of the object. */ public login() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log System.out.session session = request.getSession(); session.setAttribute("username", username); response.sendRedirect(request.getContextPath()+"/success.jsp"); }else{ response.sendRedirect(request.getContextPath()+"/error.jsp"); } } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { System.out.println("初始化select"); }}
2、success.jsp與error.jsp:
為了達到極佳的用戶體驗,我們這里新建兩個jsp界面,用于給用戶反饋登錄成功與否提醒。
3、創建Filter類:
public class firstFilter implements Filter { FilterConfig config ; public void destroy() { } public void doFilter(ServletRequest Request, ServletResponse Response, FilterChain arg2) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest)Request; HttpServletResponse res = (HttpServletResponse)Response; //防止中文亂碼 req.setCharacterEncoding("utf-8"); res.setCharacterEncoding("utf-8"); //Filter設置字符集 //req.setCharacterEncoding(config.getInitParameter("lanager")); //res.setCharacterEncoding(config.getInitParameter("lanager")); HttpSession session = req.getSession(); String username = (String) session.getAttribute("username"); //System.out.println(new String(username.getBytes("ISO-8859-1"),"utf-8"));//防止中文亂碼 System.out.println(username); // if(req.getRequestURI().indexOf("index.jsp")!=-1 || req.getRequestURI().indexOf("servlet/login")!=-1){// arg2.doFilter(Request, Response); // return ;// } String noLoginPath = config.getInitParameter("noLoginPath");//獲得我們設置的默認值 if(noLoginPath!=null){ String [] NoPath = noLoginPath.split(";"); for(int i=0; i<NoPath.length; i++ ){ if(req.getRequestURI().indexOf(NoPath[i])!=-1){ arg2.doFilter(Request, Response); return ; } } } if(username!=null){ arg2.doFilter(Request, Response); //res.sendRedirect(req.getContextPath()+"/success.jsp"); }else{ res.sendRedirect(req.getContextPath()+"/index.jsp"); } } public void init(FilterConfig arg0) throws ServletException { config = arg0; }}
這里我們第一次使用了init()方法中的FilterConfig對象,該方法具體的作用是什么呢?我們在web.xml中配置我們的Filter時,設置的默認字段,可以通過其來獲得。
4、web.xml配置:
這里我們第一次設置默認字段。
5、運行測試:
啟動我們的項目,在地址欄輸入工程名:http://localhost:8080/Test/index.jsp,進入登錄界面,當我們登錄成功后,復制一下地址欄的地址,然后打開一個新的瀏覽器,在地址欄直接訪問剛剛拷貝的地址,我就會發現我們又回到了登錄界面,這就表示我們的過濾器起作用了。當然我們的過濾器的功能遠不止這些,剩下的就要靠到家自己摸索了。
6、中文問題:
在上面的代碼中已經為大家介紹了一下有關防止中文亂碼的問題,大家可以自行了解。
到今天我們關于JSP基礎知識的總結就真正的告一段落了,如果以上十二篇博客你均已學會,我保證做一個簡單的動態網站不是什么問題,當然想做好一個網站,只有這些是遠遠不夠的,大家還需要學習div+CSS以及javaScript的知識,所謂師傅領進門,修行靠個人,祝大家在程序猿這條路上愈來愈好。
新聞熱點
疑難解答