求解答

来源:6-1 登录案例

emily_yq

2014-11-17 11:10

老师,代码改到完成之后,可以直接访问success.jsp页面了,跟最先的出于安全考虑,直接访问这个页面会跳转到Login.jsp界面想违背,这是怎么回事啊???都不能过滤了,求解答!!!

写回答 关注

5回答

  • 哈哈波哥
    2015-11-17 15:50:44

    我也遇到这样的问题 但觉得代码没问题

  • 好帮手慕珊
    2014-11-17 14:15:43

    亲,根据你的代码进行测试,代码是没有问题,项目部署之后,访问success.jsp,可直接跳转到Login.jsp.你看再试试


    emily_...

    我试了,还是可以,一样的会直接跳转到success.jsp页面,会输入这个界面的内容,我换个浏览器,清空了一切记录,再输入登陆界面,它一样不会跳转到Login.jsp页面。。。请帮我解决一哈,谢谢。。。

    2014-11-17 17:07:57

    共 1 条回复 >

  • 工作QQ_0004
    2014-11-17 11:29:49

    百度一下,你就知道

  • emily_yq
    2014-11-17 11:22:11

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
      <servlet>
        <description>This is the description of my J2EE component</description>
        <display-name>This is the display name of my J2EE component</display-name>
        <servlet-name>first</servlet-name>
        <servlet-class>com.imcoo.first</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>first</servlet-name>
        <url-pattern>/servlet/first</url-pattern>
      </servlet-mapping>
        <filter>
            <filter-name>firstfilter</filter-name>
            <filter-class>com.imcoo.firstfilter</filter-class>
            <init-param>
                <param-name>noLoginPath</param-name>
                <param-value>Login.jsp;fail.jsp;first</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>firstfilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>

    </web-app>

     

     

    package com.imcoo;

    import java.io.IOException;
    import java.util.Enumeration;

    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;
    import javax.servlet.http.HttpSession;

    public class firstfilter implements Filter {
        private FilterConfig config;
     @Override
     public void destroy() {
      // TODO Auto-generated method stub

     }

     @Override
     public void doFilter(ServletRequest arg0, ServletResponse arg1,
       FilterChain arg2) throws IOException, ServletException {
          HttpServletRequest request=(HttpServletRequest) arg0;
          HttpServletResponse response=(HttpServletResponse) arg1;
          HttpSession session=request.getSession();
          String noLoginPath=config.getInitParameter("noLoginPath");
          if(noLoginPath!=null){
           String[] array=noLoginPath.split(";");
           for(int i=0;i<array.length;i++){
            if(array[i]==null||"".equals(array[i])) continue;
            if(request.getRequestURI().indexOf(array[i])!=-1){
             arg2.doFilter(arg0, arg1);
             return;
            }
            
           }
           
          }
          if(session.getAttribute("name")!=null){
           arg2.doFilter(arg0, arg1);
          }
        
          else{
           response.sendRedirect(request.getContextPath()+"/Login.jsp");
          }
          System.out.println(session.getAttribute("name"));
     }

     @Override
     public void init(FilterConfig arg0) throws ServletException {
      // TODO Auto-generated method stub
              config=arg0;
     }

    }

    package com.imcoo;

    import java.io.IOException;
    import java.util.Enumeration;

    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;
    import javax.servlet.http.HttpSession;

    public class firstfilter implements Filter {
        private FilterConfig config;
     @Override
     public void destroy() {
      // TODO Auto-generated method stub

     }

     @Override
     public void doFilter(ServletRequest arg0, ServletResponse arg1,
       FilterChain arg2) throws IOException, ServletException {
          HttpServletRequest request=(HttpServletRequest) arg0;
          HttpServletResponse response=(HttpServletResponse) arg1;
          HttpSession session=request.getSession();
          String noLoginPath=config.getInitParameter("noLoginPath");
          if(noLoginPath!=null){
           String[] array=noLoginPath.split(";");
           for(int i=0;i<array.length;i++){
            if(array[i]==null||"".equals(array[i])) continue;
            if(request.getRequestURI().indexOf(array[i])!=-1){
             arg2.doFilter(arg0, arg1);
             return;
            }
            
           }
           
          }
          if(session.getAttribute("name")!=null){
           arg2.doFilter(arg0, arg1);
          }
        
          else{
           response.sendRedirect(request.getContextPath()+"/Login.jsp");
          }
          System.out.println(session.getAttribute("name"));
     }

     @Override
     public void init(FilterConfig arg0) throws ServletException {
      // TODO Auto-generated method stub
              config=arg0;
     }

    }

  • 好帮手慕珊
    2014-11-17 11:20:38

    亲,把配置文件和过滤器的代码贴出来看看吧

    emily_...

    请帮我看看吧。。谢谢

    2014-11-17 11:22:57

    共 1 条回复 >

Java Web开发技术应用——过滤器

本课程主要是从实际生活当中的过滤器入手,详细讲解了Java Web过滤器

70492 学习 · 258 问题

查看课程

相似问题