The filter can add some additional operations before the request arrives at the JSP or Servlet or before the response is returned to the client. If multiple JSP pages and servlets need to perform the same or similar operation, we can extract the operation to a Servlet filter, and intercept the JSP or Servlet that you are interested in by matching the path.
Let's take a few examples of typical filter applications, which can be widely used by readers:
◆ Encoding conversion
The garbled problem has plagued many Web application developers because Java-based Web applications may run on different Web applications, operating system platforms, or hardware servers, different environments have their own default encoding types. In the data conversion process, different default encoding is the beginning of the garbled problem. Therefore, when developing JSP, you often need to encode and convert the data. We can create a Servlet filter for encoding conversion before the request enters the service program.
◆ Add a uniform title or footer
Some web pages require a uniform title header or footer. You can use the Servlet filter to add a uniform title and footer to the web page before the response is returned to the client.
◆ Implement Security Control
After logging on to the system, the user information object is usually put into the session. You can use the Servlet filter to determine whether there are user information objects in the session before the request enters the JSP or Servlet. If yes, the user has logged on. If not, the user has not logged on. The request is redirected to the logon page.
The Servlet filter provided in this section is designed to implement security control. Next we will develop this Servlet.
1. Open the wizard for creating Servlet filters.
Similar to creating a standard Servlet, specify the class name and package name of the filter in this step. Enter LoginCheckFilter in Class name, and the package name is bookstore. Servlet. Click Next to Next.
2. Specify the Path Matching mode of the filter
◆ Name: logincheckfilter, which specifies the Name of the filter.
◆ URL pattern:/*, specifying the filter path matching mode. Here we use the filter to filter all requests.
Click Finish to create the filter.
Open the LoginCheckFilter. java File Created by the wizard, and enter the code shown in bold, as shown below:
- Package bookstore. servlet;
- ...
- Public class LoginCheckFilter
- Extends HttpServlet implements Filter
- {
- Public void doFilter (ServletRequest request, ServletResponse
Response, FilterChain filterChain)
- {
- Try
- {
- // Convert the request and response types
- HttpServletRequestHttpRequest= (HttpServletRequest) request;
- HttpServletResponseHttpResponse= (HttpServletResponse) response;
- BooleanIsValid=True;
- StringUriStr=HttpRequest. GetRequestURI (). toUpperCase ();
- If (uriStr. indexOf ("LOGIN. JSP") =-1 &&
- UriStr. indexOf ("SWITCH. JSP") =-1 &&
- HttpRequest. getSession (). getAttribute ("ses_userBean") = null)
- {
- IsValid=False;
- }
- If (isValid)
- {
- FilterChain. doFilter (request, response );
- } Else
- {
- HttpResponse. sendRedirect ("/webModule/login. jsp ");
- }
- } Catch (ServletException sx)
- {
- FilterConfig. getServletContext (). log (sx. getMessage ());
- } Catch (IOException iox)
- {
- FilterConfig. getServletContext (). log (iox. getMessage ());
- }
- }
- ...
- }
- DoFilter method in Servlet
- Configure Servlet Filter
- Install Servlet and JSP development tools
- Java Servlet getting started
- What is a Servlet filter?