欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java Web過濾器詳解

 更新時間:2017年08月28日 14:34:26   作者:cityhuntshou  
這篇文章主要為大家詳細介紹了Java WEB過濾器的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下

過濾器是什么玩意?

所謂過濾器,其實就是一個服務(wù)端組件,用來截取用戶端的請求與響應(yīng)信息。

過濾器的應(yīng)用場景:
1.對用戶請求進行統(tǒng)一認證,保證不會出現(xiàn)用戶賬戶安全性問題

2.編碼轉(zhuǎn)換,可在服務(wù)端的過濾器中設(shè)置統(tǒng)一的編碼格式,避免出現(xiàn)亂碼

3.對用戶發(fā)送的數(shù)據(jù)進行過濾替換

4.轉(zhuǎn)換圖像格式

5.對響應(yīng)的內(nèi)容進行壓縮

其中,第1,2場景經(jīng)常涉及。

login.jsp

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <base href="<%=basePath%>">
  
  <title>My JSP 'login.jsp' starting page</title>
  
  <meta http-equiv="pragma" content="no-cache">
  <meta http-equiv="cache-control" content="no-cache">
  <meta http-equiv="expires" content="0">  
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="This is my page">
  <!--
  <link rel="stylesheet" type="text/css" href="styles.css">
  -->

 </head>
 
 <body>
 <form action="<%=path %>/servlet/LoginServlet" method="post" >
  用戶名:<input type="text" name="username" />
 密碼:<input type="password" name="password" />
 <input type="submit" value="登錄" />
 </form>
 </body>
</html>

success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <base href="<%=basePath%>">
  
  <title>My JSP 'index.jsp' starting page</title>
  <meta http-equiv="pragma" content="no-cache">
  <meta http-equiv="cache-control" content="no-cache">
  <meta http-equiv="expires" content="0">  
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="This is my page">
  <!--
  <link rel="stylesheet" type="text/css" href="styles.css">
  -->
 </head>
 
 <body>
  
 </body>
</html>

failure.jsp

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <base href="<%=basePath%>">
  
  <title>My JSP 'login.jsp' starting page</title>
  
  <meta http-equiv="pragma" content="no-cache">
  <meta http-equiv="cache-control" content="no-cache">
  <meta http-equiv="expires" content="0">  
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="This is my page">
  <!--
  <link rel="stylesheet" type="text/css" href="styles.css">
  -->

 </head>
 
 <body>
 登錄失敗,請檢查用戶名或密碼!
 </body>
</html>

LoginFilter.java

package com.cityhuntshou.filter;

import java.io.IOException;

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.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginFilter implements Filter {
  private FilterConfig config;
  public void destroy() {
    

  }

  public void doFilter(ServletRequest arg0, ServletResponse arg1,
      FilterChain arg2) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) arg0;
    HttpServletResponse response = (HttpServletResponse) arg1;
    HttpSession session = request.getSession();
    
    
    //過濾器實際應(yīng)用場景之二-----編碼轉(zhuǎn)換
    
    String charset = config.getInitParameter("charset");
    
    if(charset == null)
    {
      charset = "UTF-8";
    }
    
    request.setCharacterEncoding(charset);
    
    String noLoginPaths = config.getInitParameter("noLoginPaths");
    
    
    
    if(noLoginPaths != null)
    {
    String[] strArray = noLoginPaths.split(";");
    for(int i = 0; i < strArray.length; i++)
    {
      //空元素,放行
      if(strArray[i] == null || "".equals(strArray[i]))
        continue;
        
      if(request.getRequestURI().indexOf(strArray[i]) != -1)
      {
      arg2.doFilter(arg0, arg1);
      return;
      }
    }
    }
    if(request.getRequestURI().indexOf("login.jsp") != -1
        || request.getRequestURI().indexOf("LoginServlet") != -1)
    {
      arg2.doFilter(arg0, arg1);
      return;
    }
    
    if(session.getAttribute("username") != null)
    {
      arg2.doFilter(arg0, arg1);
    }
    else 
    {
      response.sendRedirect("login.jsp");
    }
  }

  public void init(FilterConfig arg0) throws ServletException {
    config = arg0;

  }

}

LoginServlet.java

package com.cityhuntshou.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginServlet extends HttpServlet {

  /**
   * Constructor of the object.
   */
  public LoginServlet() {
    super();
  }

  /**
   * Destruction of the servlet. <br>
   */
  public void destroy() {
    super.destroy(); // Just puts "destroy" string in log
    // Put your code here
  }

  /**
   * The doGet method of the servlet. <br>
   *
   * This method is called when a form has its tag value method equals to get.
   * 
   * @param request the request send by the client to the server
   * @param response the response send by the server to the client
   * @throws ServletException if an error occurred
   * @throws IOException if an error occurred
   */
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    
  }

  /**
   * The doPost method of the servlet. <br>
   *
   * This method is called when a form has its tag value method equals to post.
   * 
   * @param request the request send by the client to the server
   * @param response the response send by the server to the client
   * @throws ServletException if an error occurred
   * @throws IOException if an error occurred
   */
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    String username = request.getParameter("username");
    String password = request.getParameter("password");
    //new String(username.getBytes("ISO-8859-1"),"UTF-8")
    System.out.println(username);
    if("admin".equals(username) && "admin".equals(password))
    {
      //校驗通過
      HttpSession session = request.getSession();
      session.setAttribute("username", username);
      response.sendRedirect(request.getContextPath()+"/success.jsp");
    }
    else 
    {
      //校驗失敗
      response.sendRedirect(request.getContextPath()+"/failure.jsp");
    }
  }

  /**
   * Initialization of the servlet. <br>
   *
   * @throws ServletException if an error occurs
   */
  public void init() throws ServletException {
    // Put your code here
  }

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
  xmlns="http://java.sun.com/xml/ns/javaee" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <display-name></display-name>
 <servlet>
  <description>This is the description of my J2EE component</description>
  <display-name>This is the display name of my J2EE component</display-name>
  <servlet-name>LoginServlet</servlet-name>
  <servlet-class>com.cityhuntshou.servlet.LoginServlet</servlet-class>
 </servlet>

 <servlet-mapping>
  <servlet-name>LoginServlet</servlet-name>
  <url-pattern>/servlet/LoginServlet</url-pattern>
 </servlet-mapping>  
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
  <filter>
    <filter-name>LoginFilter</filter-name>
    <filter-class>com.cityhuntshou.filter.LoginFilter</filter-class>
    <init-param>
      <param-name>noLoginPaths</param-name>
      <param-value>login.jsp;failure.jsp;loginServlet</param-value>
    </init-param>
    <init-param>
      <param-name>charset</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>LoginFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

運行效果:

訪問結(jié)果:

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

相關(guān)文章

  • Spring Boot集成sa-token的項目實踐

    Spring Boot集成sa-token的項目實踐

    本文主要介紹了Spring Boot集成sa-token的項目實踐,實現(xiàn)了基本的登錄和權(quán)限控制功能,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-05-05
  • 淺談java多態(tài)的實現(xiàn)主要體現(xiàn)在哪些方面

    淺談java多態(tài)的實現(xiàn)主要體現(xiàn)在哪些方面

    下面小編就為大家?guī)硪黄獪\談java多態(tài)的實現(xiàn)主要體現(xiàn)在哪些方面。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-09-09
  • 使用@Value值注入及配置文件組件掃描

    使用@Value值注入及配置文件組件掃描

    這篇文章主要介紹了使用@Value值注入及配置文件組件掃描方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • 兩種JAVA實現(xiàn)短網(wǎng)址服務(wù)算法

    兩種JAVA實現(xiàn)短網(wǎng)址服務(wù)算法

    這篇文章介紹了兩種JAVA實現(xiàn)短網(wǎng)址服務(wù)算法,一種是基于MD5碼的,一種是基于自增序列的,需要的朋友可以參考下
    2015-07-07
  • Java?@Accessors注解圖文詳解

    Java?@Accessors注解圖文詳解

    @Accessors用于改變@Data生成的getter和setter方法的生成結(jié)果,下面這篇文章主要給大家介紹了關(guān)于Java?@Accessors注解的相關(guān)資料,需要的朋友可以參考下
    2023-02-02
  • Spring?Gateway基礎(chǔ)教程

    Spring?Gateway基礎(chǔ)教程

    本文主要總結(jié)Spring?Gateway的基礎(chǔ)用法,內(nèi)容包括網(wǎng)關(guān)、Spring?Gateway工作流程、Spring?Cloud?Gateway搭建、路由配置方式、負載均衡實現(xiàn)、斷言工廠這幾個部分,感興趣的朋友跟隨小編一起看看吧
    2023-11-11
  • spring中@ComponentScan自動掃描并指定掃描規(guī)則

    spring中@ComponentScan自動掃描并指定掃描規(guī)則

    本文主要介紹了spring中@ComponentScan自動掃描并指定掃描規(guī)則,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-04-04
  • Java Spring分別實現(xiàn)定時任務(wù)方法

    Java Spring分別實現(xiàn)定時任務(wù)方法

    這篇文章主要為大家詳細介紹了Java與Spring設(shè)置動態(tài)定時任務(wù)的方法,定時任務(wù)的應(yīng)用場景十分廣泛,如定時清理文件、定時生成報表、定時數(shù)據(jù)同步備份等
    2022-07-07
  • spring cloud整合ribbon問題及解決方案

    spring cloud整合ribbon問題及解決方案

    很多小伙伴在整合ribbon都出了相同的問題,今天特地為大家整理了該問題的解決方案,文中有非常詳細的圖文解說,對出現(xiàn)同樣問題的小伙伴們很有幫助,需要的朋友可以參考下
    2021-05-05
  • 關(guān)于Java日期工具類的編寫

    關(guān)于Java日期工具類的編寫

    這篇文章主要介紹了關(guān)于Java日期工具類的編寫,在Java開發(fā)中,經(jīng)常會遇到處理日期相關(guān)的數(shù)據(jù),那么今天我們來自己寫一個工具類,文中有詳細的實例代碼以及實現(xiàn)思路,需要的朋友可以參考下
    2023-05-05

最新評論