国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

JSP使用Servlet過濾器進行身份驗證的方法

2024-09-05 00:22:26
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了JSP使用Servlet過濾器進行身份驗證的方法,結合實例形式分析了Servlet過濾器的實現方法及jsp身份驗證的具體使用技巧,需要的朋友可以參考下

本文實例講述了JSP使用Servlet過濾器進行身份驗證的方法。分享給大家供大家參考,具體如下:

1、Servlet過濾器的作用描述

(1)在HttpServletRequest到達Servlet 之前,攔截客戶的HttpServletRequest。

根據需要檢查HttpServletRequest,也可以修改HttpServletRequest頭和數據。

(2)在HttpServletResponse 到達客戶端之前,攔截HttpServletResponse。

根據需要檢查HttpServletResponse,可以修改HttpServletResponse頭和數據。

2、應用Servlet過濾器進行身份驗證

假設網站根目錄下的login1.htm、longin1.jsp用于用戶登錄,而chap08目錄下的文件需要用戶登錄后才能訪問。

(1)編寫Servlet過濾器

 

 
  1. @WebFilter("/FilterStation"
  2. public class FilterStation extends HttpServlet implements Filter { 
  3. private FilterConfig filterConfig; 
  4. public FilterStation() { 
  5. super(); 
  6. public void destroy() { 
  7. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 
  8. HttpSession session=((HttpServletRequest)request).getSession(); 
  9. response.setCharacterEncoding("gb2312"); 
  10. if(session.getAttribute("me")==null){ 
  11. PrintWriter out=response.getWriter(); 
  12. out.print("<script>alert('請登錄!');location.href='../login1.htm'</script>"); 
  13. else
  14. // pass the request along the filter chain 
  15. chain.doFilter(request, response); 
  16. public void init(FilterConfig fConfig) throws ServletException { 
  17. // TODO Auto-generated method stub 
  18. this.filterConfig=fConfig; 

(2)配置web.xml

 

 
  1. <filter> 
  2. <filter-name>filterstation</filter-name> 
  3. <filter-class>zhou.FilterStation</filter-class
  4. </filter> 
  5. <filter-mapping> 
  6. <filter-name>filterstation</filter-name> 
  7. <url-pattern>/chap08/*</url-pattern> 
  8. </filter-mapping> 

(3)login1.htm代碼

 

 
  1. <html> 
  2. <head> 
  3. <title>用戶登錄</title> 
  4. </head> 
  5. <body> 
  6. <form method="POST" action="login1.jsp"
  7. <p>用戶名:<input type="text" name="user" size="18"></p> 
  8. <p>密碼:<input type="text" name="pass" size="20"></p> 
  9. <p><input type="submit" value="提交" name="ok"
  10. <input type="reset" value="重置" name="cancel"></p> 
  11. </form> 
  12. </body> 
  13. </html> 

(4)login1.jsp代碼

 

 
  1. <%@ page contentType="text/html;charset=GB2312" %> 
  2. <html> 
  3. <head><title>Session 應用演示</title></head> 
  4. <%  
  5. if (request.getParameter("user")!=null && request.getParameter("pass")!=null
  6. String strName=request.getParameter("user"); 
  7. String strPass=request.getParameter("pass"); 
  8. if (strName.equals("admin") && strPass.equals("admin")) 
  9. session.setAttribute("login","OK"); 
  10. session.setAttribute("me",strName); 
  11. response.sendRedirect("chap08/welcome.jsp"); 
  12. else 
  13. out.print("<script>alert('用戶名或密碼錯誤');location.href='login1.htm'</script>"); 
  14. %> 
  15. </html> 

希望本文所述對大家JSP程序設計有所幫助。


注:相關教程知識閱讀請移步到JSP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 静乐县| 栾城县| 荆门市| 满洲里市| 孝昌县| 日土县| 阿荣旗| 东乡族自治县| 奉化市| 普宁市| 长垣县| 大荔县| 轮台县| 唐山市| 成都市| 随州市| 新丰县| 台山市| 读书| 丹巴县| 青阳县| 灵川县| 安达市| 绥江县| 项城市| 师宗县| 汪清县| 化州市| 盐边县| 海丰县| 黎平县| 拉孜县| 大英县| 平南县| 桐乡市| 班戈县| 沽源县| 安国市| 武邑县| 嘉荫县| 宜阳县|