//验证不成功,让用户登录。
hres.sendRedirect(LOGIN_PAGE);//////验证不成功转到index.htm页面 ==>>
String contextPath = hreq.getContextPath();
String redir = contextPath + "/index.htm";
hres.sendRedirect(redir);

解决方案 »

  1.   

    servlet的運行路徑是不一樣的啦﹐按照你的情況看﹐使用絕對路徑就行了String LOGIN_PAGE="/index.htm";當然啦﹐樓上的代碼通用性更好
      

  2.   

    你的WEB及SERVLET存放路径对吗?
      

  3.   

    我改成这个hres.sendRedirect("http://localhost:8081/lgm.htm");
    都不行的啊。
      

  4.   

    你在web.xml中定义了吗》,贴出来
      

  5.   

    isLogin=(String)session.getAttribute("isLogin");        
    得到是什么,看看对不?你先打印出来看看
      

  6.   

    <?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE web-app
        PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd"><web-app>
      <display-name>Welcome to Tomcat</display-name>
      <description>
         Welcome to Tomcat
      </description><!-- JSPC servlet mappings start -->    <servlet>
            <servlet-name>org.apache.jsp.index_jsp</servlet-name>
            <servlet-class>org.apache.jsp.index_jsp</servlet-class>
        </servlet>    <servlet-mapping>
            <servlet-name>org.apache.jsp.index_jsp</servlet-name>
            <url-pattern>/index.jsp</url-pattern>
        </servlet-mapping><!-- JSPC servlet mappings end -->    <filter>
         <filter-name>auth</filter-name> 
               <filter-class>com.jspdev.ch8.SignonFilter</filter-class> 
        </filter>
         
       <filter-mapping> 
           <filter-name>auth</filter-name> 
           <url-pattern>/ch8/security/*</url-pattern>  
       </filter-mapping>   <filter-mapping> 
           <filter-name>auth</filter-name> 
           <url-pattern>/ch8/hello/*</url-pattern>  
       </filter-mapping>    <filter-mapping> 
           <filter-name>auth</filter-name> 
           <url-pattern>/ch8/admin/*</url-pattern>  
       </filter-mapping>
    我的web.xml的定义
       
    </web-app>
      

  7.   

    isLogin=(String)session.getAttribute("isLogin");        
    得到是什么,看看对不?你先打印出来看看
    --这个有问题吗
      

  8.   

    这是JSP文件<%
    session.setAttribute("isLogin","true");
    %>
    通过以下代码简单实现登录:<br>
    <font color=red>session.setAttribute("isLogin","true");</font>
    <br>
    现在已经登录,点击<a href="security/index.jsp?test=a test value">这里</a>进入受限制的页面。
      

  9.   

    你的白屏,应该是没有进行sendRedirect,所以你应该在fiter中看看看看是不是你的验证有问题,不如先设程if(true)『...』
      

  10.   

    我也想if(true)打印出什么,但是我这个Servlet已经编译成class文件了,而且并没有写在main()中,我不知道如何才能显示出我要打印的信息。System.out.println("被SignonFilter拦截一个未认证的请求");这个可以吗,但信息在哪里会显示出来呢。
      

  11.   

    显示在控制台,您用的是什么服务器,tomcat or resin
      

  12.   

    上下文环境不对,hres.sendRedirect(LOGIN_PAGE);//////验证不成功转到index.htm页面
    是jsp中的写法,在jsp中上下文环境由系统处理,在servlet中你可以用如下跳转:
    getServletContext().getRequestDispatcher(LOGIN_PAGE).forward(request,response);
      

  13.   

    to dryr
    加入getServletContext().getRequestDispatcher(LOGIN_PAGE).forward后提示SignonFilter.java:55: cannot resolve symbol
    symbol  : method getServletContext ()
    location: class com.jspdev.ch8.SignonFilter
    getServletContext().getRequestDispatcher(LOGIN_PAGE).forward(hreq,hres);
    ^
    1 error
      

  14.   

    我知道了,你的web.xml不完整,
      <servlet>
        <servlet-name>Aupload</servlet-name>
        <servlet-class>com.smarteam.vcms.basicmng.servlet.Aupload</servlet-class>
        <load-on-startup>3</load-on-startup>
      </servlet>  <servlet-mapping>
        <servlet-name>Aupload</servlet-name>
        <url-pattern>/GroupCustmerMng/gc/Aupload</url-pattern>
      </servlet-mapping>
      

  15.   

    请问一下你的类有没有扩展javax.servlet.http.HttpServlet这个类啊,如果没有扩展那getServletContext()方法是要出错的,那是HttpServlet的方法。
      

  16.   

    其实,如果你只是处理http请求的话,你完全没有必要如此定义方法public void doFilter(final ServletRequest req,final ServletResponse res,FilterChain chain)throws IOException,ServletException;
    你可以如下定义:
    public void doFilter(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res,FilterChain chain)throws IOException,ServletException
    这样的话,你在方法里面也不用把 HttpServletRequest  hreq = (HttpServletRequest )req; HttpServletResponse hres = (HttpServletResponse)res;将ServletRequest、ServletResponse转化成HttpServletRequest、HttpServletResponse了。
      

  17.   

    你没有交代,究竟验证成功的时候能否跳转,如果能,就是Sevlet的问题,如果不能,就可能是web.xml的问题了。建议用 RequestDispatcher的forward()方法去跳转。
    另外,你的servlet是继承了哪个类或接口呢?
    public void doFilter(final ServletRequest req,final ServletResponse res,FilterChain chain) 方法是从哪里来的?会处理什么问题?
      

  18.   

    楼主你光up有个屁用啊,你去试一下啊,你把你的类
    extends javax.servlet.http.HttpServlet
    然后去调用我给你的那个方法应该就可以的,再去试一下