0

this is the code I have in my index.java

public class index{
static final String DATABASE_URL = "jdbc:mysql://localhost:3306/try?zeroDateTimeBehavior=convertToNull";
Connection con;
Statement stmt;
ResultSet rs;
PreparedStatement pstmt;


private String id;
private String password;
private String message;
private String action;



public String getId() {return id;}
public void setId(String id) {this.id = id;}

public String getMessage() {return message;}

public String getPassword() {return password;}
public void setPassword(String password) {this.password = password;}

public String login(){
    try{
    Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/try?user=root&password=123");
    String sql = "Select * FROM personnel WHERE pID =? AND password = ?";
    pstmt = con.prepareStatement(sql);
    pstmt.setString(1, id);
    pstmt.setString(2,password);
    rs=pstmt.executeQuery();
    if(rs.next()){
    action = "table";

    }
    else{action = null;
    message="wrong password";}
    }
    catch(Exception e){}
    return action;
}   
}

public String modify(){
return "modify";}}

I built 3 pages first one for login:

<h:head>
    <title>Log in</title>
</h:head>
<h:body>
    <h:form>
        <h:outputText id="output" value="#{index.message}"/>
        <br/>
        IDnumber:  <h:inputText id="personnelID" value ="#{index.id}"/>
        <br/>
         Password:  <h:inputSecret id="password" value ="#{index.password}"/><h:commandButton value="submit" action="#{index.login()}"/>
    </h:form>
</h:body>

second for display a table:

<h:head>
    <title>table</title>
</h:head>
<h:body>
    <h:form>

    My Tasks:
    ID =<h:outputText id="output" value="#{index.id}"/>
    <h:commandButton value="Modify My Task" action="#{index.modify()}"/>

    </h:form>
</h:body>

third for modify:

<h:head>
    <title>Modify</title>
</h:head>
<h:body>
    <h:form>
        ID =<h:outputText id="output" value="#{index.id}"/>
    </h:form>

</h:body>

My problem is, the table page can still display the id i used to log in, but the third page just return id as null. What am I doing wrong??

  • Can you show the exception logs that you have as a result of this – GingerHead Jun 06 '14 at 14:58
  • @Ginger Head right, if this was the log u talking about Info: visiting unvisited references Info: visiting unvisited references Info: visiting unvisited references Info: visiting unvisited references Info: Initializing Mojarra 2.2.0 ( 20130502-2118 https://svn.java.net/svn/mojarra~svn/tags/2.2.0@11930) for context '/SQL' Info: Loading application [SQL] at [/SQL] Info: SQL was successfully deployed in 903 milliseconds. – user3677469 Jun 06 '14 at 17:29

2 Answers2

0

In your index.java you the following method:

public String login()

Which handles the call from your login page and processes it and finally displays your table page.

Now when your table page loads, it has a command button that must be handled by a method in your index.java.

But as you can see there is no such method in that class to keep the id in the mentioned object.

You need to add a new method:

public String modify()

to get along with the normal process.

GingerHead
  • 8,130
  • 15
  • 59
  • 93
0

I think you just missed the @SessionScoped Tag before the class login.java. The values of id and password updated the first time you clicked submit, but they will lose their values when you navigate to another page like modify because that is another request and they are by default @NoneScoped, see here.

There is an additional closing curly bracket } that closes the class login.java before the method modify, make sure that method is in the class. Hope this is useful, good luck.

Community
  • 1
  • 1
TiyebM
  • 2,684
  • 3
  • 40
  • 66
  • 1
    thank you for your help. i miss pasting the tag on the question indeed :) I can see the problem after u described it. Can u teach me what i need to do to keep the value when I enter another page? – user3677469 Jun 06 '14 at 18:03
  • The @SessionScoped tag would be enough to keep the data for each user during the session no matter which site he/she navigate to in your application. The session ID is generated by the server depending on the browser, when you close the browser and reopen it and visit the same page, you get a new session ID, unless it is already registered as a cookie, So that would do it, best wishes. – TiyebM Jun 06 '14 at 18:31
  • At least you can say it was the right answer!, I voted your question by the way. – TiyebM Jun 06 '14 at 18:42
  • thank you, i tried to vote ur answer but i dont have enough score ;) but thank you, it really did help a lot:D – user3677469 Jun 06 '14 at 18:59