日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

验证用户身份Filter过滤器

發布時間:2025/3/20 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 验证用户身份Filter过滤器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

通過過濾器對一批頁面或Servlet統一進行身份驗證
運行本例,直接進入loginsuccess.jsp頁面,會彈出提示信息

過濾器實現類FilterLogin.java

public class FilterLogin extends HttpServlet implements Filter {private FilterConfig filterConfig;@Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)throws IOException, ServletException {HttpSession session=((HttpServletRequest)request).getSession();response.setCharacterEncoding("utf-8");if(session.getAttribute("user")==null){ //判斷session是否由user這個對象PrintWriter out=response.getWriter(); //創建一個輸出流//如果為空則通過javascript腳本輸出提示并跳轉到index頁面out.println("<script language=javascript>alert('您還沒有登錄');window.location.href='../index.jsp';</script>");}else{filterChain.doFilter(request, response);}}@Overridepublic void init(FilterConfig arg0) throws ServletException {this.filterConfig=filterConfig;}}

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>&nbsp;</h3><p align="center">使用過濾器身份驗證</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>

創建loginresult.jsp頁面,在user對象的session中執行跳轉到下一頁面

<%@ 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>

創建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>使用過濾器身份驗證</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>

總結

以上是生活随笔為你收集整理的验证用户身份Filter过滤器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。