web.xml配置
<filter>
<filter-name>jspFilter</filter-name>
<filter-class>com.cb.cbms.coreserver.servlet.JspFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>jspFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Java代码
package com.cb.cbms.coreserver.servlet;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 不允许直接访问jsp
* 所有对jsp的直接访问,跳转到首页面
* @author eason
*
*/
public class JspFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
String url = httpServletRequest.getRequestURI();
if(url != null && url.endsWith(".jsp")) {
httpServletResponse.sendRedirect(httpServletRequest.getContextPath());
return;
}
chain.doFilter(request, response);
}
@Override
public void destroy() {
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
}
禁止jsp跳转过滤器编写
最新推荐文章于 2021-03-27 22:04:07 发布