一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java Web過濾器詳解

Java Web過濾器詳解

2020-12-22 15:13cityhuntshou Java教程

這篇文章主要為大家詳細介紹了Java WEB過濾器的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下

過濾器是什么玩意?

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

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

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

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

4.轉換圖像格式

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

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

Java Web過濾器詳解

login.jsp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<%@ 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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<%@ 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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<%@ 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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?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過濾器詳解

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

原文鏈接:http://www.cnblogs.com/cityhuntshou/p/7443688.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 免费看美女被靠到爽的视频 | 免费视频专区一国产盗摄 | 国语对白做受xxxx | 亚洲视频在线免费观看 | 91色视| 精品卡1卡2卡三卡免费视频 | 日韩综合久久 | 亚洲ss| 四虎影视免费观看免费观看 | 99久久精品无码一区二区毛片 | 亚洲精品人成网在线播放影院 | 国产精品女主播大秀在线 | 美女1819xxxx| 欧美日韩国产成人综合在线影院 | 亚洲aⅴ天堂 | 青草青草久热精品视频在线网站 | 91热爆在线 | 精品国产香蕉 | 日韩一区二区不卡 | 村上里沙40分钟在线观看 | 亚洲高清一区二区三区久久 | 日韩精品欧美国产精品亚 | 国产成人免费在线视频 | 肉色欧美久久久久久久蜜桃 | 精品牛牛影视久久精品 | 免费观看国产精品 | ts人妖系列在线专区 | 免费在线看a| 电车痴汉(han) | 撕开老师的丝袜白丝扒开粉嫩的小 | 国产亚洲精品视频中文字幕 | 天天干天天日天天射天天操毛片 | 美女和男人一起差差 | avtt在线观看| 欧美兽皇video| 精品久久香蕉国产线看观看亚洲 | 特大黑人娇小亚洲女mp4 | 香蕉eeww99国产精选播放 | 亚洲26uuuu最新地址 | 久久久91精品国产一区二区 | 亚洲国产果果在线播放在线 |