亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

jsp實現登錄驗證的過濾器

瀏覽:304日期:2022-06-07 14:55:44

本文實例為大家分享了jsp實現登錄驗證的過濾器,供大家參考,具體內容如下

1.新建一個Dynamic Web Project項目,里面新建1個filter文件、1個servlet文件和2個jsp文件,整體的框架如圖所示:

2.LoginFilter.java里面的代碼如下所示:

package filter; import java.io.IOException;import java.io.PrintWriter; import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.annotation.WebFilter;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession; @WebFilter(filterName = "loginFilter", urlPatterns = { "/*" })public class LoginFilter implements Filter {  public void destroy() { }  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse resp = (HttpServletResponse) response; HttpSession session = req.getSession(true); resp.setContentType("text/html;"); resp.setCharacterEncoding("utf-8"); PrintWriter out = resp.getWriter(); String request_uri = req.getRequestURI(); String ctxPath = req.getContextPath(); String uri = request_uri.substring(ctxPath.length()); if (uri.contains("login.jsp") || uri.contains("loginServlet")) { chain.doFilter(request, response); } else { if (session.getAttribute("user") != null) { chain.doFilter(request, response); } else { out.println("您沒有登錄,請先登錄!3秒后回到登錄頁面。"); resp.setHeader("refresh", "3;url=" + ctxPath + "/login.jsp"); return; } } }  public void init(FilterConfig fConfig) throws ServletException { } }

3.LoginServlet.java里面的代碼如下所示:

package servlet; import java.io.IOException; import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession; @WebServlet(name = "loginServlet", urlPatterns = { "/loginServlet" })public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L;  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); }  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("name"); String password = request.getParameter("pwd"); if ("filter".equals(username) && "filter".equals(password)) { HttpSession session = request.getSession(true); session.setAttribute("user", username); response.sendRedirect("loginSuccess.jsp"); } else { response.sendRedirect("login.jsp"); } } }

4.login.jsp里面的代碼如下所示:

<%@page import="java.util.Date"%><%@ 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>example</title></head><body bgcolor="lightPink"> <form action="loginServlet" method="post"> <table> <tr> <td>用戶名:</td> <td><input type="text" name="name" /></td> </tr> <tr> <td>密碼:</td> <td><input type="password" name="pwd" /> </tr> <tr> <td><input type="submit" value="提交" /></td> <td><input type="reset" value="重置" /></td> </tr> </table> </form></body></html>

5.loginSuccess.jsp里面的代碼如下所示:

<%@page import="java.util.Date"%><%@ 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>example</title></head><body> <% String username = (String) session.getAttribute("user"); %> 恭喜<%=username%>登錄成功!</body></html>

6.在瀏覽器里面輸入http://localhost:8080/Servlet/login.jsp運行之后得到的結果如下所示:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持。

標簽: JSP
相關文章:
主站蜘蛛池模板: 看欧美的一级毛片 | 青青青免费视频精品99 | 成人男女啪啪免费观看网站 | 成人午夜激情 | 亚洲国产欧美久久香综合 | 黄色网址发给我 | 国产美女一区二区 | 亚洲无限乱码一二三四区 | 成人午夜免费剧场 | 九九九久久久 | 久久综合综合久久 | 黑人超大 | 中国一级特黄特爽刺激大片 | 日本高清毛片视频在线看 | 91青青视频| 免费一级特黄3大片视频 | 伊人蕉久中文字幕无码专区 | www.免费黄色 | 自拍愉拍 | 免费特黄一区二区三区视频一 | 国产乳摇福利视频在线观看 | 亚洲国产一区二区三区四区 | 亚洲一级毛片欧美一级说乱 | 国产精品久久久亚洲 | 日韩在线第二页 | 国产一区二区不卡免费观在线 | 亚洲视频综合网 | 黄色视屏免费观看 | 小明永久2015xxx免费看视频 | 亚洲色图套图超市 | 久草在线视频免费资源观看 | 国产一区曰韩二区欧美三区 | mimiai网址 | 国产高清在线a视频大全 | 婷婷情| 午夜国产精品理论片久久影院 | 国产亚洲精品美女一区二区 | 欧美限制级在线观看 | 正在播真实出轨炮对白 | 之后3在线观看完整免费酷客 | 国产精品亚洲一区二区三区久久 |