JSP使用Servlet過濾器進行身份驗證的方法
本文實例講述了JSP使用Servlet過濾器進行身份驗證的方法。分享給大家供大家參考,具體如下:
1、Servlet過濾器的作用描述
(1)在HttpServletRequest到達Servlet 之前,攔截客戶的HttpServletRequest。
根據(jù)需要檢查HttpServletRequest,也可以修改HttpServletRequest頭和數(shù)據(jù)。
(2)在HttpServletResponse 到達客戶端之前,攔截HttpServletResponse。
根據(jù)需要檢查HttpServletResponse,可以修改HttpServletResponse頭和數(shù)據(jù)。
2、應用Servlet過濾器進行身份驗證
假設網(wǎng)站根目錄下的login1.htm、longin1.jsp用于用戶登錄,而chap08目錄下的文件需要用戶登錄后才能訪問。
(1)編寫Servlet過濾器
@WebFilter("/FilterStation") public class FilterStation extends HttpServlet implements Filter { private FilterConfig filterConfig; public FilterStation() { super(); } public void destroy() { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpSession session=((HttpServletRequest)request).getSession(); response.setCharacterEncoding("gb2312"); if(session.getAttribute("me")==null){ PrintWriter out=response.getWriter(); out.print("<script>alert('請登錄!');location.href='../login1.htm'</script>"); } else{ // pass the request along the filter chain chain.doFilter(request, response); } } public void init(FilterConfig fConfig) throws ServletException { // TODO Auto-generated method stub this.filterConfig=fConfig; } }
(2)配置web.xml
<filter> <filter-name>filterstation</filter-name> <filter-class>zhou.FilterStation</filter-class> </filter> <filter-mapping> <filter-name>filterstation</filter-name> <url-pattern>/chap08/*</url-pattern> </filter-mapping>
(3)login1.htm代碼
<html> <head> <title>用戶登錄</title> </head> <body> <form method="POST" action="login1.jsp"> <p>用戶名:<input type="text" name="user" size="18"></p> <p>密碼:<input type="text" name="pass" size="20"></p> <p><input type="submit" value="提交" name="ok"> <input type="reset" value="重置" name="cancel"></p> </form> </body> </html>
(4)login1.jsp代碼
<%@ page contentType="text/html;charset=GB2312" %> <html> <head><title>Session 應用演示</title></head> <% if (request.getParameter("user")!=null && request.getParameter("pass")!=null) { String strName=request.getParameter("user"); String strPass=request.getParameter("pass"); if (strName.equals("admin") && strPass.equals("admin")) { session.setAttribute("login","OK"); session.setAttribute("me",strName); response.sendRedirect("chap08/welcome.jsp"); } else { out.print("<script>alert('用戶名或密碼錯誤');location.href='login1.htm'</script>"); } } %> </html>
希望本文所述對大家JSP程序設計有所幫助。
相關文章
在JSTL EL中處理java.util.Map,及嵌套List的情況
在EL中,方括號運算符用來檢索數(shù)組和集合的元素。對于實現(xiàn) java.util.Map 接口的集合,方括號運算符使用關聯(lián)的鍵查找存儲在映射中的值。2009-03-03JSP實現(xiàn)遠程文件下載保存到服務器指定目錄中的方法
這篇文章主要介紹了JSP實現(xiàn)遠程文件下載保存到服務器指定目錄中的方法,涉及JSP文件傳輸及目錄操作的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10Javaweb工程運行報錯HTTP Status 404解決辦法
這篇文章主要介紹了Javaweb工程運行報錯HTTP Status 404解決辦法,文中通過示例介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07JSP 中Spring的Resource類讀寫中文Properties實例代碼
這篇文章主要介紹了JSP 中Spring的Resource類讀寫中文Properties實例代碼的相關資料,需要的朋友可以參考下2017-03-03深入淺析Jsp中 out.print 和 out.write 的區(qū)別
本文簡明扼要的給大家介紹了jsp中 out.print 和 out.write 的區(qū)別,雖然本文簡短但是主要內容給大家介紹清楚了,需要的朋友參考下吧2017-02-02jsp基于XML實現(xiàn)用戶登錄與注冊的實例解析(附源碼)
這篇文章主要介紹了jsp基于XML實現(xiàn)用戶登錄與注冊的實例解析,xml做數(shù)據(jù)庫實現(xiàn)用戶登錄與注冊,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2015-11-11jsp提交到Servlet報404錯誤問題解決(webroot下子目錄)
第一次用jsp寫東西,在webroot子文件夾下寫jsp,當提交到Servlet時報404錯誤,下面是具體的解決方法,有類似問題的朋友可以參考下哈2013-06-06