Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Trivial login form in JSF 2.0

DZone's Guide to

Trivial login form in JSF 2.0

· Java Zone
Free Resource

Just released, a free O’Reilly book on Reactive Microsystems: The Evolution of Microservices at Scale. Brought to you in partnership with Lightbend.

This tip is an example of how you can create a trivial login form in JSF 2.0 . The credentials are hardcoded as final variables – in real application, you need will compare the entered values agains a database.

. . .
@ManagedBean
@SessionScoped
public class User implements Serializable {

   private static final long serialVersionUID = 1L;
 
   private final String userName = "User";
   private final String userPassword = "12345";
   private String name;
   private String password;

   public String getName() {
      return name;
   }
 
   public void setName(String name) {
      this.name = name;
   }

   public String getPassword() {
      return password;
   } 

   public void setPassword(String password) {
      this.password = password;
   }
 
   public String login() {
      if((userName.equals(name))&&(userPassword.equals(password))){
         return "welcome";
   } else {
         return "index";
   }
  }
}

The login page should look something as below:

  . . .
       <h:form> 
       User : <h:inputText value="#{user.name}" />
       Password : <h:inputSecret value="#{user.password}" />
       <h:commandButton action="#{user.login}" value="Submit" />
       </h:form>
   . . .

If the userName and userPassword corresponds with the name and password entered, you will be redirected to the welcome.xhtml page. Otherwise, you will remain on the login form page.

 

From http://e-blog-java.blogspot.com/2011/06/trivial-login-form-in-jsf-20.html

Strategies and techniques for building scalable and resilient microservices to refactor a monolithic application step-by-step, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}