我在Tomcat中有一个webapp,其中有一个主JSP文件,该文件在页面中心包含另一个JSP文件。我想直接拒绝对该文件的访问,而只允许直接访问主索引页面。
另外,我不希望用户能够直接从我的Web应用程序获取图像。
如何使用Tomcat拒绝这些请求?我希望所有请求都转发到我的主页。
一种方法是实施 Filter
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因此您可以执行以下操作:
request
ServletRequest
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 }