문제 설명
사용자가 찾을 수 없는 페이지를 요청했는지 감지하는 필터를 만드는 방법은 무엇입니까? (How to make a filter to detect if the user requested a page that is not found?)
각 요청 전에 요청된 페이지가 있는지 여부를 감지하도록 앱에 필터를 만들고 싶습니다. 존재하지 않는 경우 사용자에게 오류 페이지를 전달합니다.
페이지가 존재하는지 어떻게 감지합니까?
필터가 있고 사용하지 않는 솔루션이 필요합니다. web.xml 태그 메소드.
참조 솔루션
방법 1:
If you don't have authentication, you can.
- Make a
Filter
- Use HttpServletResponseWrapper and override the
sendError()
andsetStatus()
- Pass the wrapped response through
chain.doFilter(req, wrapper)
- If you get a
sendError()
in your wrapper, see if it's a 404. - Take appropriate response.
You may also have to override getOutputStream()
and getWriter()
to avoid the response to be flushed to the client before you get a chance to do stuff.
방법 2:
You can directly configure it in web.xml
<error‑page>
<error‑code>404</error‑code>
<location>/yourCustom404.jsp</location>
</error‑page>
Or Create a filter and Use HTTPURLConnection programatically detect the page exist or not.
방법 3:
"Page exists" is not something trivial. Pages need not to exist physically as files.
Apart from the option mentioned by org.life.java to simulate a request using HttpURLConnection
, you can create a HttpServletResponseWrapper
, override the setStatus
method, and whenever it is set to 404
, take extra measures.
(by Mahmoud Saleh、Martin Algesten、jmj、Bozho)