控制用户通过url地址直接访问,需要控制如果用户未登录则跳转到登陆页面
写了个filter,下为代码片段public class AuthFilter implements Filter {
private static final String LOGIN_URL = "/login.jsp";
public void destroy() {
} public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)servletRequest;
HttpServletResponse response = (HttpServletResponse)servletResponse;
HttpSession session = request.getSession(true);
String currentURL = request.getRequestURI();
System.out.println("currentURL==========" + currentURL);
String targetURL = currentURL.substring(currentURL.indexOf("/", 1), currentURL.length()); {{注:这快报错java.lang.StringIndexOutOfBoundsException: String index out of range: -1
java.lang.String.substring(Unknown Source)}}
System.out.println("targetURL=" + targetURL);
if (!targetURL.equals(LOGIN_URL)) {
if (session == null || session.getAttribute("user") == null) {
System.out.println("request.getContextPath()=="+ rquest.getContextPath());
response.sendRedirect(request.getContextPath() + "/index.jsp");
return;
}
}
chain.doFilter(request, response);
} public void init(FilterConfig arg0) throws ServletException {
}
写了个filter,下为代码片段public class AuthFilter implements Filter {
private static final String LOGIN_URL = "/login.jsp";
public void destroy() {
} public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)servletRequest;
HttpServletResponse response = (HttpServletResponse)servletResponse;
HttpSession session = request.getSession(true);
String currentURL = request.getRequestURI();
System.out.println("currentURL==========" + currentURL);
String targetURL = currentURL.substring(currentURL.indexOf("/", 1), currentURL.length()); {{注:这快报错java.lang.StringIndexOutOfBoundsException: String index out of range: -1
java.lang.String.substring(Unknown Source)}}
System.out.println("targetURL=" + targetURL);
if (!targetURL.equals(LOGIN_URL)) {
if (session == null || session.getAttribute("user") == null) {
System.out.println("request.getContextPath()=="+ rquest.getContextPath());
response.sendRedirect(request.getContextPath() + "/index.jsp");
return;
}
}
chain.doFilter(request, response);
} public void init(FilterConfig arg0) throws ServletException {
}
解决方案 »
- jboss7在哪里配置maxPostSize参数?
- java 自定义String类,为什么会默认执行自定义的String类呀
- 急!struts2与spring整合的问题
- tomcat 与 jdk 问题
- Request timed out waiting for an available thread to run是什么回事,是由什么引起的?
- 【求助】一个EJB的简单问题,高手快来!企盼回复,在线=
- 关于struts中如何引用页面中的变量的问题
- 菜鸟问题:请问如何在EJB中返回一个ResultSet到JSP(Servlet)~Help me, 急~~~
- 关于weblogic7.0的一点小问题,在线等,请高手指点
- java 怎么样读取一个字符的Unicode 值?
- 求一个完整的struts源码系统!网上找了很多不完整!
- JAVA Applet 嵌入问题
is either negative or greater than the size of the string. For
some methods such as the charAt method, this exception also is
thrown when the index is equal to the size of the string.currentURL长度小于1?