当前位置:网站首页>Servlet advanced
Servlet advanced
2022-06-12 07:56:00 【Watermelon 77】
filter The interception process :
Filter Three methods in the interface :
init Method : After the filter object is created , Execute initialization method .
doFilter Method : In this method, interception and release are implemented .
destroy Method : After the filter is destroyed , perform .
Implement the program :
package cn.itcast.chapter08.filter;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().print(“hello Filter”);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
establish MyFilter Of Filter class , Intercept access servlet resources , If you want to release , perform doFilter Method :
package cn.itcast.chapter08.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;
/**
Servlet Filter implementation class MyFilter
*/
public class MyFilter implements Filter {/**
- @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
// Allow access to target resources
chain.doFilter(request, response);
System.out.println(“ Output content , Represents the interception response ”);
}
/**
- @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
}
- @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
}
stay web.xml In the document servlet To configure :
MyFilter
/MyServlet
filter mapping :
Use wildcards “*” Intercept all user requests : There are three forms of configuration :
Full catalog match : such as /MyServlet
Directory matching : such as /,/aa/ On behalf of intercepting aa All requests in the directory
Extension matching : such as .do,.action, Wrong writing :/*.do
Different ways to intercept requests :
REQUEST: When the user visits the page directly ,web The container will call the filter
INCLUDE: If the target resource passes RequestDispatcher Of include() Method access , Then the filter will be called . besides , The filter will not be called .
FORWARD: If the target resource passes RequestDispatcher Of forward() Method access , Then the filter will be called . besides , The filter will not be called .
ERROR: If the target resource is called through the declarative exception handling mechanism , Then the filter will be called
filter chain :
Create two filters MyFilter01 and MyFilter02:
MyFilter01:
packagecn.itcast.chapter08.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;
/**
Servlet Filter implementation class MyFilter01
*/
public class MyFilter01 implements Filter {/**
- @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}
/**
- @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
response.getWriter().print(“hello MyFilter01-------before
”);
chain.doFilter(request, response);
response.getWriter().print(“hello MyFilter01-------after
”);
}
/**
- @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
}
- @see Filter#destroy()
}
MyFilter02:
package itcast.chapter08.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;
/**
Servlet Filter implementation class MyFilter02
*/
public class MyFilter02 implements Filter {/**
- @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}
/**
- @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
response.getWriter().print(“hello MyFilter02-----------before
”);
chain.doFilter(request, response);
response.getWriter().print(“hello MyFilter02-----------after
”);
}
/**
- @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
}
- @see Filter#destroy()
}
FilterConfig Interface :
边栏推荐
- Numerical calculation method chapter6 Iterative method for solving linear equations
- Conda創建虛擬環境報錯,問題解决
- Voice assistant -- vertical class perpetual motion machine -- automated iteration framework
- 20220524 backbone deep learning network framework
- Rnorm function of R language generates positive distribution data, calculates descriptive statistical summary information of vector data using sum function of epidisplay package, and visualizes ordere
- Topic 1 Single_Cell_analysis(2)
- Voice assistant - overall architecture and design
- Kalman filter encapsulation function
- Voice assistant - Multi round conversation (process implementation)
- Getting started with Jetson nano Series IV: common skills of NVIDIA Jetson nano
猜你喜欢

『Three.js』辅助坐标轴

Voice assistant - overall architecture and design

Vs 2019 MFC connects and accesses access database class library encapsulation through ace engine

Exposure compensation, white increase and black decrease theory

Chapter V - message authentication and digital signature

Voice assistant - Introduction and interaction process

Meter Reading Instrument(MRI) Remote Terminal Unit electric gas water

Support vector machine (SVM)

Rich dad, poor dad Abstract

The computer is connected to WiFi but can't connect to the Internet
随机推荐
Classic paper review: palette based photo retrieval
Mathematical knowledge - derivation - Basic derivation knowledge
Rnorm function of R language generates positive distribution data, calculates descriptive statistical summary information of vector data using sum function of epidisplay package, and visualizes ordere
Leetcode notes: Weekly contest 276
2021.10.27-28 scientific research log
LeetCode笔记:Weekly Contest 296
Topic 1 Single_ Cell_ analysis(1)
Getting started with Jetson nano Series IV: common skills of NVIDIA Jetson nano
Windows10 configuration database
Preliminary knowledge next New concepts such as isr/rsc/edge runtime/streaming in JS
Some summaries of mathematical modeling competition in 2022
数值计算方法 Chapter5. 解线性方程组的直接法
Voice assistant -- Qu -- semantic role annotation and its application
tar之多线程解压缩
Compiling principle on computer -- function drawing language (III): parser
Cold start problem of recommended system
FPGA based communication system receiver [packet detection] development document
R language dplyr package mutate_ At function and one_ The of function converts the data type of a specified data column (specified by a vector) in dataframe data to a factor type
Introduction to coco dataset
R语言e1071包的naiveBayes函数构建朴素贝叶斯模型、predict函数使用朴素贝叶斯模型对测试数据进行预测推理、table函数构建混淆矩阵