1111import jakarta .servlet .http .HttpServletRequest ;
1212import jakarta .servlet .http .HttpServletResponse ;
1313import java .io .IOException ;
14+ import util .WebContext ;
1415
1516// WebFilter annotation chỉ định filter này áp dụng cho mọi URL pattern
1617@ WebFilter ("/*" )
@@ -29,20 +30,18 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
2930 HttpServletRequest httpRequest = (HttpServletRequest ) request ;
3031 HttpServletResponse httpResponse = (HttpServletResponse ) response ;
3132
32- // Lấy context path của ứng dụng (phần URL trước servlet path)
33- String contextPath = httpRequest .getContextPath ();
34- // Lấy URI được yêu cầu hoàn chỉnh
35- String requestURI = httpRequest .getRequestURI ();
36- // Xác định đường dẫn thực tế được yêu cầu, bỏ qua context path
37- String path = requestURI .substring (contextPath .length ());
33+ WebContext .setCurrentRequest (httpRequest );
34+ WebContext .setCurrentResponse (httpResponse );
3835
39- // Kiểm tra nếu đường dẫn không bắt đầu bằng "/assets" (thường là các tài nguyên tĩnh)
40- if (!path .startsWith ("/assets" )) {
41- // Nếu không phải tài nguyên tĩnh, xử lý đường dẫn bằng cách sử dụng router đã định nghĩa
42- Router .route (httpRequest , httpResponse );
43- } else {
44- // Nếu là tài nguyên tĩnh, cho phép request tiếp tục đi qua các filter tiếp theo (nếu có)
45- chain .doFilter (request , response );
36+ try {
37+ String path = httpRequest .getRequestURI ().substring (httpRequest .getContextPath ().length ());
38+ if (!path .startsWith ("/assets" )) {
39+ Router .route ();
40+ } else {
41+ chain .doFilter (request , response );
42+ }
43+ } finally {
44+ WebContext .clear ();
4645 }
4746 }
4847}
0 commit comments