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

首頁 > 編程 > JSP > 正文

JSP使用Servlet過濾器進(jìn)行身份驗(yàn)證的方法

2020-07-27 21:28:41
字體:
供稿:網(wǎng)友

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

1、Servlet過濾器的作用描述

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

2、應(yīng)用Servlet過濾器進(jìn)行身份驗(yàn)證

假設(shè)網(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('請(qǐng)登錄!');location.href='../login1.htm'</script>");}else{// pass the request along the filter chainchain.doFilter(request, response);}}public void init(FilterConfig fConfig) throws ServletException {// TODO Auto-generated method stubthis.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 應(yīng)用演示</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('用戶名或密碼錯(cuò)誤');location.href='login1.htm'</script>");}}%></html>

希望本文所述對(duì)大家JSP程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 玉山县| 庐江县| 枞阳县| 金沙县| 浙江省| 鄂伦春自治旗| 甘洛县| 昌黎县| 洪洞县| 延吉市| 江安县| 临洮县| 大厂| 达州市| 林口县| 永宁县| 红河县| 阳江市| 宁都县| 长子县| 汝阳县| 盐山县| 上思县| 南投市| 徐州市| 黔西县| 四川省| 尼木县| 靖安县| 凉城县| 五家渠市| 房产| 工布江达县| 樟树市| 普兰县| 兴安县| 靖远县| 江西省| 长沙市| 合川市| 彭州市|