91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Java Web過濾器詳解

發布時間:2020-09-14 12:43:06 來源:腳本之家 閱讀:147 作者:cityhuntshou 欄目:編程語言

過濾器是什么玩意?

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

過濾器的應用場景:
1.對用戶請求進行統一認證,保證不會出現用戶賬戶安全性問題

2.編碼轉換,可在服務端的過濾器中設置統一的編碼格式,避免出現亂碼

3.對用戶發送的數據進行過濾替換

4.轉換圖像格式

5.對響應的內容進行壓縮

其中,第1,2場景經常涉及。

Java Web過濾器詳解

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();
    
    
    //過濾器實際應用場景之二-----編碼轉換
    
    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>

運行效果:

Java Web過濾器詳解

Java Web過濾器詳解

Java Web過濾器詳解

Java Web過濾器詳解

訪問結果:

Java Web過濾器詳解

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

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

海晏县| 河东区| 乐山市| 巴彦淖尔市| 北流市| 伊金霍洛旗| 偃师市| 舟山市| 阳西县| 兴业县| 永春县| 仁寿县| 天台县| 崇信县| 平罗县| 西乌| 大兴区| 巴楚县| 晴隆县| 象山县| 保山市| 车险| 东至县| 揭东县| 常德市| 墨脱县| 西宁市| 桦川县| 青冈县| 临江市| 安达市| 东光县| 饶阳县| 蓝田县| 保山市| 宁乡县| 九台市| 洪湖市| 关岭| 莱西市| 观塘区|