What is the first method called when a filter is initialized in a web application?
4 phương ánCó giải thích
Các phương án trả lời
Chọn một phương ánLời giảiChọn phương án để mở
Đáp án đúng: "init()". Filter có vòng đời ba bước do container điều khiển: init(FilterConfig) được gọi đúng một lần ngay sau khi container tạo thực thể filter và trước khi yêu cầu đầu tiên đi qua, doFilter() chạy cho từng yêu cầu khớp mẫu ánh xạ, destroy() chạy khi ứng dụng bị tháo; vì vậy nơi đọc tham số khởi tạo hay mở tài nguyên dùng lâu dài chính là init(). "initFilter()" không nằm trong giao diện Filter, đặt tên này thì container không bao giờ gọi tới và tham số khởi tạo sẽ không được đọc. "initialize()" cũng không thuộc hợp đồng của Filter, đây là tên quen thấy ở nơi khác chứ không phải trong đặc tả servlet. "setupFilter()" tương tự là tên tự đặt; container chỉ gọi đúng chữ ký khai trong giao diện mà lớp filter cài đặt nên một tên gần nghĩa vẫn bị bỏ qua hoàn toàn.
Cùng phần 10
Which XML elements are used to define a servlet filter in the web.xml file?Có lời giảiCâu hỏiWhich method is mandatory in a Filter class?Có lời giảiCâu hỏiConsider the code in the LoggingFilter filter:
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
System.out.println( new Date());
chain.doFilter(request, response);
}
And in the web.xml:
<filter>
<filter-name>LoggingFilter</filter-name>
<filter-class>LoggingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoggingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Assuming that the client sends a request to open the index.jsp page, arrange the following steps correctly to describe the processing inside server.
1: the LoggingFilter intercepts the request to display current date
2: the server compiles and processes the JSP file to generates an HTML response.
3: The final HTML content from index.jsp is sent back to the client's browser.
4: the request continues to index.jsp
5: the HTML response from index.jsp is sent back to the LoggingFilter.Có lời giảiCâu hỏiCả 26 câu của chủ đề26 câu
Xuất hiện trong 2 bộ đề
Đi tiếp từ đây