验证用户身份Filter过滤器
生活随笔
收集整理的這篇文章主要介紹了
验证用户身份Filter过滤器
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
通過過濾器對一批頁面或Servlet統(tǒng)一進(jìn)行身份驗(yàn)證
運(yùn)行本例,直接進(jìn)入loginsuccess.jsp頁面,會彈出提示信息
過濾器實(shí)現(xiàn)類FilterLogin.java
JavaBean類User
public class User { private String username; private String password; public String getUsername() {return username; } public void setUsername(String username) {this.username = username; } public String getPassword() {return password; } public void setPassword(String password) {this.password = password; } }用戶登錄頁面index.jsp頁面
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <script type="text/javascript"> function checkEmpty(){if(document.form.name.value==""){alert("用戶名不能為空");document.form.name.focus();return false;}if(document.form.password.value==""){alert("密碼不能為空");document.form.password.focus();return false;} } </script> </head> <body><h3> </h3><p align="center">使用過濾器身份驗(yàn)證</p><form name="form" method="post" action="loginresult.jsp" onSubmit="return checkEmpty()"><table width="220" border="1" align="center" cellpadding="0" cellspacing="0" cgcolor="808080"><tr><td align="center">用戶名</td><td><input type="text" name="name"></td></tr><tr><td align="center">密碼</td><td><input type="password" name="password"></td></tr><tr><td align="center" colspan="2"><input type="submit" name="Submit" value="登錄"><input type="submit" value="退出"></td></tr></table> </body> </html>創(chuàng)建loginresult.jsp頁面,在user對象的session中執(zhí)行跳轉(zhuǎn)到下一頁面
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@page import="com.cn.zj.Filter.User" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String name=request.getParameter("name"); String password=request.getParameter("password"); User user=new User(); user.setUsername(name); user.setPassword(password); session.setAttribute("user",user); response.sendRedirect("filter/loginsuccess.jsp"); %> </body> </html>創(chuàng)建loginsuccess.jsp頁面
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %> <%@ page import="com.cn.zj.Filter.User"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>使用過濾器身份驗(yàn)證</title> </head> <body><div align="center"><table width="333" height="285" cellpadding="0" cellspacing="0"><tr><td align="center"><p>您己成功登錄</p><p><br><a href="backtrack.jsp">返回</a></p></td></tr> </table> </div></body> </html>backtrack.jsp頁面
<% session.invalidate(); out.print("<script language='javascript'>window.location.href='../index.jsp';</script>"); %>web.xml文件配置
<welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list> <filter><filter-name>filterUSer</filter-name><filter-class>com.cn.zj.Filter.FilterLogin</filter-class> </filter> <filter-mapping><filter-name>filterUser</filter-name><url-pattern>/filter/*</url-pattern> </filter-mapping>總結(jié)
以上是生活随笔為你收集整理的验证用户身份Filter过滤器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 字符替换过滤器
- 下一篇: 使用过滤器监控网站流量