Java Web – Filters

Application of Java Web development technology - filter < p class =" best text mb-10 "> a filter is a program that runs on the server before the related servlet or JSP page. The filter can be attached to one or more servlet or JSP pages, and can check the request information to enter these resources. After that, the filter can make the following choices:

Filter life cycle

1. Instantiate web XML is based on Web. XML when the web container starts XML instantiation

2. Initialize init ()

3. Filter (dofilter)

4. Destroy ()

to configure:

You can configure it quickly through the design interface

The filter needs to implement the interface javax servlet. Filter, I thought it was a class... I've been looking for it for a long time

Three methods need to be implemented

destroy() {}

doFilter(ServletRequest arg0,ServletResponse arg1,FilterChain arg2) IOException,ServletException {}

init(FilterConfig arg0) ServletException {}

Implementing logic in dofilter

If multiple filters correspond to one path

Then according to the web The sequential execution filter defined in XML

Request - > filter 1 - > filter 2 - > servlet - > filter 2 - > filter 1 - > User

Filter classification: (the default is request)

Async: asynchronously executes the filter and business logic content in the servlet.

Error: processing error page

Forward: through request getRequestDispatcher("url"). forward(request,response); perhaps

Include: through request getRequestDispatcher("url"). include(request,response); perhaps

Request: direct access through link or response sendRedirect("url");

On the web Configure error page in XML

Configure the filter by annotation on the class

@WebFilter(filterName="...", value={"/....jsp"},dispatcherTypes={DispatcherType.REQUEST,DispatcherType.ASYNC})

public class FilterName implements Filter {...}

Case: login verification. If there is no login, the page that can be accessed only after login cannot be accessed directly through URL

(I suddenly find that there is a servlet option on the right-click, and my heart is broken... Every time I create a new Java class

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>