一种方法是实施
Filter
例如:
package package;import javax.servlet.*;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class FilterImplementation implements Filter{ public void init(FilterConfig filterConfig) throws ServletException {...} public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // if you detect an illegal request, throw an exception or return without calling chain.doFilter. chain.doFilter(request, response); } public void destroy() {...}}将以下内容添加到web.xml中:
<filter> <filter-name>MyFilter</filter-name> <filter-class>package.FilterImplementation</filter-class></filter><filter-mapping> <filter-name>MyFilter</filter-name> <url-pattern>/*</url-pattern></filter-mapping>
编辑
您需要了解的有关请求哪个页面的所有信息都在
request参数中。
ServletRequest但是,参数类型几乎总是为a,
HttpServletRequest因此您可以执行以下操作:
if (request instanceof HttpServletRequest){ HttpServletRequest hrequest = (HttpServletRequest) request; String uri = hrequest.getRequestURI(); // you should be able to just use this String uri = hrequest.getRequestURL(); // otherwise there are more in-depth fields}


