主頁(yè) > 知識(shí)庫(kù) > jsp實(shí)現(xiàn)登錄驗(yàn)證的過(guò)濾器

jsp實(shí)現(xiàn)登錄驗(yàn)證的過(guò)濾器

熱門(mén)標(biāo)簽:自繪地圖標(biāo)注數(shù)據(jù) 電銷(xiāo)機(jī)器人免培訓(xùn) 潤(rùn)滑油銷(xiāo)售電銷(xiāo)機(jī)器人 如何看懂地圖標(biāo)注點(diǎn) 外呼系統(tǒng)使用方法 給地圖標(biāo)注得傭金 南通通訊外呼系統(tǒng)產(chǎn)品介紹 電話機(jī)器人需要使用網(wǎng)絡(luò)嗎 海外圖書(shū)館地圖標(biāo)注點(diǎn)

本文實(shí)例為大家分享了jsp實(shí)現(xiàn)登錄驗(yàn)證的過(guò)濾器,供大家參考,具體內(nèi)容如下

1.新建一個(gè)Dynamic Web Project項(xiàng)目,里面新建1個(gè)filter文件、1個(gè)servlet文件和2個(gè)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("您沒(méi)有登錄,請(qǐng)先登錄!3秒后回到登錄頁(yè)面。");
 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>用戶(hù)名:/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運(yùn)行之后得到的結(jié)果如下所示:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • jsp實(shí)現(xiàn)簡(jiǎn)單用戶(hù)7天內(nèi)免登錄
  • servlet+jsp實(shí)現(xiàn)過(guò)濾器 防止用戶(hù)未登錄訪問(wèn)
  • 利用JSP session對(duì)象保持住登錄狀態(tài)
  • JSP+MySQL實(shí)現(xiàn)網(wǎng)站的登錄與注冊(cè)小案例
  • JSP實(shí)現(xiàn)用戶(hù)登錄、注冊(cè)和退出功能
  • jsp基于XML實(shí)現(xiàn)用戶(hù)登錄與注冊(cè)的實(shí)例解析(附源碼)
  • JavaWeb實(shí)現(xiàn)用戶(hù)登錄注冊(cè)功能實(shí)例代碼(基于Servlet+JSP+JavaBean模式)
  • JSP制作簡(jiǎn)單登錄界面實(shí)例
  • JSP+Servlet制作Java Web登錄功能的全流程解析
  • jsp實(shí)現(xiàn)用戶(hù)自動(dòng)登錄功能

標(biāo)簽:南京 黃石 銅川 大連 廣州 內(nèi)江 樂(lè)山 貸款邀約

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《jsp實(shí)現(xiàn)登錄驗(yàn)證的過(guò)濾器》,本文關(guān)鍵詞  jsp,實(shí)現(xiàn),登錄,驗(yàn)證,的,過(guò)濾器,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《jsp實(shí)現(xiàn)登錄驗(yàn)證的過(guò)濾器》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于jsp實(shí)現(xiàn)登錄驗(yàn)證的過(guò)濾器的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章