Login application in JSF

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

Hello, we have learned how to create a Hello World application in JSF. This article will extend the same to create a sample login application in jsf.

  • This will demonstrate how can we navigate from one page to another.
  • Sending and retrieving data to and from a managed bean. and using the same in another page.

What do we need?

We will have following components in our login application in jsf.

  • Login bean
  • index.xhtml : It will contain login form
  • success.xhtml : It will be shown along with username who logged in if login is successful.
  • failure.xhtml : It will be shown if user credentials are invalid
  • faces-config.xml : JSF configuration file.
  • web.xml : Your web application configuration file.

 

LoginBean.java

LoginBean code above provided by Madhav in comments (Many thanks madhav! well I was too lazy I have to agree 😀 )

index.xhtml:

Create a login form in index.xhtml for your login application in jsf.

 success.xhtml

 

 failure.xhtml

 faces-config.xml

 web.xml

We’re all done for our login application in jsf. Simply deploy it under tomcat and start it. Hit the url

http://localhost:8080/JSF2LoginDemo/faces/index.xhtml

You will see the login page. The credentials madhav/password as username/password will show the welcome message while wrong credentials will show you invalid credentials message.

 

 

 

 

Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

29 thoughts on “Login application in JSF

  • Pingback:login xhtml jsf

  • January 11, 2015 at 3:59 pm
    Permalink

    where is the Login Bean?

    Reply
    • January 13, 2015 at 7:35 pm
      Permalink

      It seems I did not post login bean code at all 🙂 thanks for pointing this out. As of now I’ve updated the tutorial roughly. I will update it with original code when I find out that old workspace and the project I had created for tutorial. Sorry and Thank you Kevin 🙂

      Reply
  • April 11, 2015 at 3:04 pm
    Permalink

    Here you go for LoginBean :

    import javax.faces.bean.ManagedBean;

    @ManagedBean
    public class LoginBean {

    private String username;
    private String password;

    /**
    * @return the username
    */
    public String getUsername() {
    return username;
    }

    /**
    * @param username the username to set
    */
    public void setUsername(String username) {
    this.username = username;
    }

    /**
    * @return the password
    */
    public String getPassword() {
    return password;
    }

    /**
    * @param password the password to set
    */
    public void setPassword(String password) {
    this.password = password;
    }

    public String login() {
    if (username.equals(“madhav”)) {
    return “success”;
    } else
    return “failure”;
    }

    }

    Reply
  • July 1, 2015 at 5:03 pm
    Permalink

    thank u for your login form. tell
    how to use the images in jsf

    Reply
    • July 2, 2015 at 9:56 am
      Permalink

      you can keep the images in your web app in some folder for e.g. WebContent/images and in then use in your jsf page.

      Reply
  • July 12, 2015 at 10:27 am
    Permalink

    Can you share a structure of the project

    Reply
  • July 12, 2015 at 11:09 am
    Permalink

    Sir..thank you so much..I learn a lot from your blogs..God Bless..May you continue in sharing your knowledge to us…

    Reply
    • July 13, 2015 at 9:21 am
      Permalink

      I am really glad you find my blog useful. Keep providing feedback so that I can improvise 🙂

      Reply
  • July 19, 2015 at 6:46 pm
    Permalink

    where is Managed bean ?

    Reply
  • October 23, 2015 at 6:10 pm
    Permalink

    when the jsf 2.0 supports implicit navigation system then why you are using the navigatian rule in faces config file…

    Reply
    • October 26, 2015 at 11:02 am
      Permalink

      Hi Abhishek, thank you for your comment. I just explained that navigation can be done using faces config file. Nothing else.

      Reply
  • June 21, 2016 at 11:43 pm
    Permalink

    Hello Mr. Prasad Kharkar

    Thanks for this great tutorials.
    I would like you to please explain how to do the password and email verification from the Database
    with jsf please.
    Thanks you very much Sir.

    Best Regards
    John

    Reply
  • June 22, 2016 at 11:42 pm
    Permalink

    Thank you Sir for your prompt response.
    What i really want is how i can verify the email and password given by the user during login against the one in the database using JPA,JSF, Eclipse.
    Wildfly and EJB are very new to me.
    i am just a beginner.

    Thank you again Sir
    Best Regards
    John.

    Reply
    • June 23, 2016 at 7:57 am
      Permalink

      Hi John,

      This is how you would like to go,

      1. From your JSF LoginBean, you make query to database for retrieving password e.g. “SELECT password FROM LOGIN_DETAILS WHERE username =
      2. You need to pass username retrieved from loginform to query and get password from database.
      3. If password from database matches with password entered in login form, then login is successful.
      4. You can redirect to desired page .
      Reply

  • June 24, 2016 at 11:44 pm
    Permalink

    Thanks again Sir for your positive and quick response, please can you clarify this answer with an example that would help me alot Sir.

    Best Regards
    John.

    Reply
  • June 27, 2016 at 9:01 pm
    Permalink

    Thanks again Sir for your positive and quick response, please can you clarify this answer with an example that would help me Sir.

    Best Regards
    John.

    Reply
    • June 28, 2016 at 8:30 am
      Permalink

      Hi John, if you want to create apps using JPA, JSF, then you need to have knowledge about atleast one servlet container, I hope you know about tomcat. For EJB, you will have to use Wildfly or jboss7. If you do not want to use EJB, then you can use UI layer as JSF, ORM layer as JPA and any one of your favourite database. Please go t through the link I’ve provided in previous comment to get an overview of what you want. Try creating a layered application and if you face any problems, do let me know, I will try my best to help.

      Reply
  • July 30, 2016 at 10:09 am
    Permalink

    When I run the above Login application Im getting HTTP Status 404 error The requested resource is not available.Please let me know what to do?

    Reply
  • August 1, 2016 at 2:47 pm
    Permalink

    Im new to JSF can you please let me know where will i find the log file?

    Reply
    • August 4, 2016 at 1:29 pm
      Permalink

      Im not getting any error messages in error log.

      Reply
  • September 26, 2016 at 9:26 am
    Permalink

    Hi, I’m trying your example. For me, the index page is not rendering. its blank.

    Reply
  • May 5, 2017 at 8:11 pm
    Permalink

    Well explained. If the password gets hashed, that would be even better!

    Reply
  • December 19, 2017 at 5:44 pm
    Permalink

    hi, i m new for prime faces. can you give me the example. i want to save empname,empnum using managed bean. give me the explanation also thala. i need basic easy tutorial for maven,annotation,bean plz plz provide something..

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *