带有servlet过滤器的Jsf 2.2

时间:2014-11-04 21:39:24

标签: java servlets jsf-2 servlet-filters httpsession

我使用jsf 2.2并且我想将jsf页面重定向到登录页面而没有会话我没有使用http会话here.iam尝试使用会话范围变量并检查其他变量是否为null 并将其重定向到登录页面,因为会话范围足够而不是http会话权限??

我的过滤器类是

package com.invoice;

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.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet Filter implementation class sessionfilter
 */


public class sessionfilter implements Filter {

    /**
     * Default constructor. 
     */
    public sessionfilter() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @see Filter#destroy()
     */
    public void destroy() {
        // TODO Auto-generated method stub
    }

    /**
     * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
     */
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        // TODO Auto-generated method stub
        // place your code here

        // pass the request along the filter chain
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        login b=new login();
        if(b.username=="")
        {

            httpResponse.sendRedirect("Login.xhtml");   

        }
        else{
        chain.doFilter(request, response);
        }
    }

    /**
     * @see Filter#init(FilterConfig)
     */
    public void init(FilterConfig fConfig) throws ServletException {
        // TODO Auto-generated method stub
    }

}

我的bean课程在这里

package com.invoice;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;


public class login {

    String username,userpassword,errormess,navipg;



    public String getNavipg() {
        return navipg;
    }

    public void setNavipg(String navipg) {
        this.navipg = navipg;
    }

    public String getErrormess() {
        return errormess;
    }

    public void setErrormess(String errormess) {
        this.errormess = errormess;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getUserpassword() {
        return userpassword;
    }

    public void setUserpassword(String userpassword) {
        this.userpassword = userpassword;
    }
    public String navigate()
    {
        try
        {

            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","invoice","google");
            Statement stmt=con.createStatement(); 
            ResultSet rs=stmt.executeQuery("select username,password from usertable where username='"+username+"' and password='"+userpassword+"' ");
            if(rs.next())
            {

                setErrormess("");
                navipg="invoiceinfo";


            }
            else

            {

                setErrormess("Invalid Login credentials");
                username="";
                navipg="Login";


            }

        }
        catch(Exception e)
        {

            e.printStackTrace();

        }
        return navipg;

    }
    public String logout()
    {

         FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
            return "Login";

    }




}

我的web.xml过滤器标记是

<filter>
   <filter-name>sessionfilter</filter-name>
   <filter-class>com.invoice.sessionfilter</filter-class>

</filter>
<filter-mapping>
   <filter-name>sessionfilter</filter-name>
   <url-pattern>/Jsf Invoice System/*</url-pattern>
</filter-mapping>

但页面未被重定向

1 个答案:

答案 0 :(得分:0)

b.username=="" 永远不会成为true

必读