0

I am currently trying to do login & logout JSF authentication method for my project.

I am going through several classes and connection with database in order to get value from database.

When i'm finished compiling all necessary attributes on my classes query, i run and it shows me error as follows: Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

I assume that I may have mistake while i created a class to load my JDBC driver to pass the URL. Here is the connection class:

public class DataConnect {
public static Connection getConnection() {
    try {
        Class.forName("org.hibernate.dialect.MySQLDialect");
        Connection con = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/epda_assignment?zeroDateTimeBehavior=convertToNull", "root", "kok123");
        return con;
    } catch (ClassNotFoundException | SQLException ex) {
        System.out.println("Database.getConnection() Error -->"
                + ex.getMessage());
        return null;
    }
}

public static void close(Connection con) {
    try {
        con.close();
    } catch (SQLException ex) {
    }
}

LoginBean.java

public class LoginBean implements Serializable{

private String email2;
private String msg;
private String password2;

public String processRequest() {
    boolean valid = UserDao.validate(email2, password2);
    if (valid) {
        HttpSession session = SessionUtil.getSession();
        session.setAttribute("email2", email2);
                    session.setAttribute("password2", password2);
        return "after_login";
    } else {
        FacesContext.getCurrentInstance().addMessage(
                null,
                new FacesMessage(FacesMessage.SEVERITY_WARN,
                        "Incorrect Username and Passowrd",
                        "Please enter correct username and Password"));
        return "home";
    }
}
 //logout event, invalidate session
public String logout() {
    HttpSession session = SessionUtil.getSession();
    session.invalidate();
    return "home";
}

home.xhtml

<table>
                <tr>
                <td> <h:outputLabel for="email2">Enter your Email: </h:outputLabel></td>
                <td><h:inputText id="email2" value="#{loginBean.email2}"/></td>
            </tr>
                <tr>
                <td> <h:outputLabel for="passwrod2">Password: </h:outputLabel></td>
                <td><h:inputText id="password2" value="#{loginBean.password2}"/></td>
            </tr>
                <td><h:commandButton id="submit-button" value="Submit" action="#{loginBean.processRequest}"/></td>
                <td></td>
            </table>

after_login.xhtml

<h:body>
    <h:form>
    <p>Welcome #{registerBean.fullName_}</p>
    <h:commandLink action="#{loginBean.logout}" value="Logout"></h:commandLink>
</h:form>    
</h:body>

Please help me to find the mistake i didn't see. If the problem is not on the above class, where could be the problem?

  • Please show the JSF Bean code – Simon Martinelli Mar 19 '20 at 15:34
  • I have made change include with LoginBean class –  Mar 19 '20 at 15:42
  • and what are you doing in "home" and "after_login"? – Simon Martinelli Mar 19 '20 at 16:13
  • the browser won't redirect because this is an infinite loop. for example you are on the page `test` and there you perform an action so you redirect or refresh the page but not more than once. somehow you always refresh/redirect to `test` so you are stuck in an infinite loop. just add an if statement to check if you are not on the site – fuggerjaki61 Mar 19 '20 at 16:59
  • also can you add a little more information like it was asked in the previous comments? – fuggerjaki61 Mar 19 '20 at 17:20
  • Dear @SimonMartinelli, i have added information about home.xhtml and after_login.xhtml –  Mar 20 '20 at 05:33

1 Answers1

0

Problem

The browser (firefox) doesn't load the site because of an infinite redirect. Somehow you are redirecting to the same page over and over so firefox terminates the requests to save performance.

So since you did not edit the question to be more exact, I can only provide an example problem like yours.

Imagine you are on the site test and on this site you perform some actions and based on some results you want to go to the page test (so a refresh) or abc. Somehow the result is always the same so you redirect forever and firefox stops it not to waste performance.

OR

Similar to the previous example: Imagine you have to sites and one redirect to another so you go from test to abc and then back forever. (I don't think firefox would recognize and stop this)


Solution

The solution for this problem is quite simple: Just add an if-statement before you redirect so you only redirect when you are not on that site.

OR

Pass an information that the action should not redirect.


Example 1:

HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();

if (!"yourSite".equals(request.getRequestURI()) {
    return "/yourSite.xhtml";
} else {
    return null;
}

Example 2:


Edit

In your case I would also add an if-statement to check if you are not on the current site you want to redirect.

Like this:

HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();

if (!"home".equals(request.getRequestURI()) {
    return "home";
} else {
    return null;
}

It's also the same with after_login. Just replace home with it. Then it should work.


Some related questions:

No redirection after button click

Community
  • 1
  • 1
fuggerjaki61
  • 822
  • 1
  • 11
  • 24
  • Hi, thank you for your answer. I have added new information on my case as the previous comment requested. –  Mar 20 '20 at 05:42