fillets_listener
fillets
A Java class that implements a special interface to filter request resources (jsp, servlet, html)
A filter is a program that runs on the server and is executed prior to requesting resources (Servlet or jsp, html). Filters are the most practical technology in javaweb technology
Function
Filter target resources (Servlet, jsp)
How to use
via xml configuration
by way of annotation
Filter life cycle
init(FilterConfig): Initialize
doFilter(ServletReqeust req, ServletResponse resp, FilterChain chain): method to perform filtering
destroy(): destroy
Life cycle description
1. When the server starts, the filter is initialized, and the init method of the filter is executed.
2. Whenever the path of a request satisfies the configuration properties of the filter, the doFilter method of the filter will be executed once.
3. When the server is shut down normally, destroy the filter and execute the destroy method of the filter
map path
For selective filter requests, you need to configure different mapping paths for the filter, so that the filter can filter the requests you want to filter
Complete path matching, starting with "/"
Filters can only intercept path
directory matching, starting with "/" and ending with "*"
All paths under the current project can intercept
extension matching, starting with "*"
Intercept suffix
interception method
Filters can be used to distinguish between different ways of accessing resources, and there are different interception methods
DispatcherType.REQUEST
defaults to filter requests and redirects sent from browsers
DispatcherType.FORWARD
only filters forwarded requests
@WebFilter(urlPatterns = "/demo02" ,dispatcherTypes = {
DispatcherType.FORWARD,DispatcherType.REQUEST})
public class FileDemo02 implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("111");
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
}
}
filter chain
When a filter receives a request, call chain.doFilter to access the next matching filter. If the current filter is the last filter, call chain.doFilter to access the target resource
Invaild symbol
Idea analysis
Create a form for making speeches
Create a txt file, store illegal characters in it
Create a Filter, intercept the request, read the illegal characters in the txt file into the memory in the init method to
obtain the parameters in the request, and respond to the request Check the parameters for illegal characters.
If the speech does not contain illegal characters, it will be released.
If the speech contains illegal characters, it will be intercepted and the user will be prompted for illegal speech.
package illegalchec;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
@WebFilter("/*")
public class IllegaFilter implements Filter {
private List<String> illegalWords = new ArrayList<String>();
//new一个list
@Override
public void init(FilterConfig filterConfig) throws ServletException {
try{
InputStream is = filterConfig.getServletContext().getResourceAsStream("IllegaWords.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"));
String line = null;
while((line = reader.readLine()) != null){
illegalWords.add(line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
response.setContentType("text/html;charset = utf-8");
request.setCharacterEncoding("utf - 8");
String message = request.getParameter("message");
for (String illegalWord : illegalWords){
if (message != null && message.trim().contains(illegalWord)){
response.getWriter().write("含有非法字符");
return;
}
}
filterChain.doFilter(request,response);
}
@Override
public void destroy() {
}
}
Listener
The listener is a Java class used to monitor the changes of other JavaBean
In javaweb, the listener is
requestqa, session, servletContext(application) which monitors the state of the three domain objects
The application of the listener
is mainly used in Swing programming and
Android, and a large number
of events in js are used.
The term of the listener
Event source: the object being listened to. The target object
Listener object: the listening object
Event: the name of the event source behavior
Registration (binding): register the "listener object" to the "event source"
event object: get the "event source" in the "listener object"
ServletContextListener
monitors the destruction and creation of ServletContext objects
Creation: When the ServletContext server starts, the server creates a separate ServletContext for each web application
Destroy: when the server is shut down, or removed from the project
Use of ServletContextListener
Write a class to implement the ServletContextListener interface
Register listener in web.xml