校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識庫 > JSP使用Servlet過濾器進行身份驗證的方法

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

熱門標簽:電銷機器人好賣么 高德地圖標注樣式 商洛電銷 電銷機器人是有一些什么技術 杭州語音電銷機器人軟件 杭州ai語音電銷機器人功能 地圖標注線上教程 四川保險智能外呼系統商家 北票市地圖標注

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

1、Servlet過濾器的作用描述

(1)在HttpServletRequest到達Servlet 之前,攔截客戶的HttpServletRequest。
根據需要檢查HttpServletRequest,也可以修改HttpServletRequest頭和數據。
(2)在HttpServletResponse 到達客戶端之前,攔截HttpServletResponse。
根據需要檢查HttpServletResponse,可以修改HttpServletResponse頭和數據。

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

假設網站根目錄下的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程序設計有所幫助。

您可能感興趣的文章:
  • 通過過濾器(Filter)解決JSP的Post和Request中文亂碼問題
  • JSP過濾器Filter配置過濾類型全部匯總
  • JSP 開發中過濾器filter設置編碼格式的實現方法
  • JSP過濾器防止Xss漏洞的實現方法(分享)
  • JSP使用過濾器防止SQL注入的簡單實現
  • jsp實現登錄驗證的過濾器
  • jsp中過濾器選擇過濾器的寫法詳解
  • 詳解JSP中使用過濾器進行內容編碼的解決辦法
  • servlet+jsp實現過濾器 防止用戶未登錄訪問
  • JSP使用過濾器防止Xss漏洞
  • jsp filter 過濾器功能與簡單用法示例

標簽:云浮 紅河 江西 丹東 宿州 青島 西藏 貴州

巨人網絡通訊聲明:本文標題《JSP使用Servlet過濾器進行身份驗證的方法》,本文關鍵詞  JSP,使用,Servlet,過濾器,進行,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《JSP使用Servlet過濾器進行身份驗證的方法》相關的同類信息!
  • 本頁收集關于JSP使用Servlet過濾器進行身份驗證的方法的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 绍兴市| 永康市| 乳山市| 青神县| 晋江市| 墨江| 怀远县| 紫阳县| 乌恰县| 蛟河市| 朝阳区| 灵武市| 襄城县| 涡阳县| 客服| 天柱县| 巴彦淖尔市| 高台县| 尉氏县| 银川市| 涪陵区| 教育| 霸州市| 宜黄县| 鄂托克前旗| 石景山区| 怀仁县| 雷波县| 文昌市| 上思县| 黎川县| 桦甸市| 廉江市| 桑植县| 磴口县| 万山特区| 江山市| 苗栗市| 咸宁市| 九龙坡区| 木兰县|