Web tier: servlets, JSP, Web frameworks: JSF best practice question

  1. JSF best practice question (2 messages)

    Hello everyone, I am using JSF and I am trying to find the practice for my problem. I want to know what could be the best practice for the following scenario and why: Assume we have a page having 2 fields: username and password. If the username and password are correct he will be forwarded to another page containing his data, mail or whatsoever. The user enters his username and password so 2 functions are performed: 1. public int checkUser(String userName, String password); this function returns the id of the user if exists and -1 if doesnt exist. 2. public Employee getUser(int userId); this function returns an instance of the employee that has the specified userId. The question here is shall I put this function in the first page and put the employee object in the session or shall I put it on the second page and execute it once the page is loaded. Thanks.
  2. Re: JSF best practice question[ Go to top ]

    Hi, I don't know if I understand you correctly, but in general this is a matter of taste. Let me make short sketch of how the setup would be in your case. We would have two backing or managed beans - one for each JSP. On the form page you would use only properties and methods of the first bean. In it's action listener method you would check the username and password as well as load the user when it exists - so both steps in one. (E.g. when there's an error loading the user you normally want to stay on the form page, so that would be easiest when doing all the stuff in the first backing bean.) Then you would store this User object into some session attribute. Alternatively you put it directly in an attribute of the second backing bean - provided this bean has session scope. In this case you need to wire both beans to be able to access the second bean from the action listener method of the first bean. That way you provide all the data needed in subsequent steps of your application. On the second page you would access the properties and methods of the second backing bean as well as the User object you loaded in the first action listener method. Hope that helps, René Z.
  3. Keeping both the methods on the first page makes dynamic navigation really easier. Lets say in a scenario where you can go to 3 different pages after you are logged in, for example: in a system where Administrator sees a different page and regular user see a different page. In such case you just have to return a correct string after the authentication. Example-pseudo code public String ButtonAction(){ if (authenticate()) { if(admin) return "AdminPage"; else return "user"; } else { return null; message("Wrong Password"); } } Sap