Home » Enterprise Java » jsf » Password Example with JSF 2.0

About Thodoris Bais

Thodoris Bais
Thodoris is an Oracle Certified Associate Java Programmer and currently works as a Junior Software Developer, for Intrasoft International S.A. He holds a diploma at Informatics & Telecommunications Engineering and is interested in continuous development.

Password Example with JSF 2.0

If you didn’t notice from the previous example, we started a mini example series for the JSF Tag Library, so in the next couple examples, we are going to deal with several simple, but quite useful JSF tags. Today, we ‘ll deal with a password field. While on JSF, we can use the following tag, in order to render an HTML input of a password field: <h:inputSecret>

To get the meaning, imagine that the fore-mentioned xhtml’s tag is equal to HTML’s <input type="password">. So, let’s get into the full example.
 
 
 

1. Managed Bean

Here is our simple Managed Bean, which handles the password.

UserBean.java

package com.javacodegeeks.enterprise.jsf.password;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class UserBean implements Serializable{

	private String password;

	public String getPassword() {
		return password;
	}

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

2. Our Pages

As in the previous example, we need two separate pages; Let’s have a look at them:

index.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"    
      xmlns:h="http://java.sun.com/jsf/html">
 
    <h:head>
    	<title>JSF Password Example</title>
    </h:head>
    <h:body> 
    	<h1>JSF 2.0 Password Example</h1>
    	
    	<h:form>
    		Password : <h:inputSecret value="#{userBean.password}" />
    		<h:commandButton value="Submit" action="response" />
    	</h:form>
    </h:body>
</html>

response.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"    
      xmlns:h="http://java.sun.com/jsf/html">
 
    <h:head>
    	<title>JSF Password Example</title>
    </h:head>
    <h:body> 
    	<h1>JSF 2.0 Password Example - Response Page</h1>
    	
    	The password is : <h:outputText value="#{userBean.password}" />
    </h:body>
</html>

3. Demo

Let’s take a quick demo, by trying to access the following URL: http://localhost:8080/PasswordJSF

image

And after clicking the button, our response page:

image2

This was an example of TextBox in JSF 2.0. You can also download the source code for this example: PaswordJSF

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of