Table of contents
1.
Introduction
2.
JSF h:inputSecret
2.1.
Tag
2.2.
Attribute Table
2.3.
Example
2.4.
Output
3.
Frequently Asked Questions
3.1.
Can inputSecret Tag of JSF take multi-line input?
3.2.
Can this JSF tag have spaces in the input?
3.3.
What is the main use of this tag?
4.
Conclusion
Last Updated: Mar 27, 2024

JSF h:inputSecret in JSF

Author Gurleen Kaur
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JSF is a user interface framework based on server-side components. It's a programming language that's used to create web applications. It has a well-defined programming model and a robust API and tag library. Facelets is the default templating system in the latest version of JSF 2. It's written in the Java programming language.

The JSF Tag libraries add components to web pages and link them to server-side objects. Tag handlers that implement the component tag are also included. The h:inputSecret tag creates a password-type HTML input element. Let’s discuss it in deep in this article.

JSF h:inputSecret

It's a standard password field that takes one line of text without any spaces and displays it as a series of asterisks as you type. Put it another way; it's used to make an HTML password field that allows a user to type a string without it appearing in the field.

Tag

JSF Tag:

<h:inputSecret value = "password" />

Rendered Output:

<input type = "password" name = "random value" value = "password" />

Attribute Table

Example


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://xmlns.jcp.org/jsf/html">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h:form>
            <h:inputSecret id="pass" value="${controlDemo.password}"/><br/><br/>
            <h:commandButton value="Click"/><br/><br/>
            <h:outputText value="${controlDemo.password}"/>
        </h:form>
    </h:body>
</html>

ControlDemo.java

package com.codingninjas;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
@ManagedBean
@RequestScoped
public class ControlDemo {
    /**
     * Creates a new instance of ControlDemo
     */
    // password string
    private String password;
    // to get the password
    public String getPassword()
    {
        return password;
    }
    
    //to set the password
    public void setPassword(String password)
    {
        this.password=password;
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

faces-config.xml

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
              xmlns="http://xmlns.jcp.org/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
    <managed-bean>
        <managed-bean-name>test</managed-bean-name>
        <managed-bean-class>com.codingninjas.Test</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>
</faces-config>

Output

The application initially looks like this:

As you see, once you enter text, it is not visible:

The output when the button is clicked:

Frequently Asked Questions

Can inputSecret Tag of JSF take multi-line input?

No, this tag can not take multi-line input. They can only have single-line values.

Can this JSF tag have spaces in the input?

No, inserting spaces between input is not allowed in this tag; the given input by the user must be joined without spaces.

What is the main use of this tag?

This tag is mainly used when the data entered should be hidden, for example, password.

Conclusion

In this article about JSF, we have extensively discussed the inputSecret tag. We started with an introduction to JSF, about this tag, and why to use them, with the help of an example.

You can refer to our guided paths given on Coding Ninjas Studio to educate yourself in Data Structures and AlgorithmsJavaScriptCompetitive ProgrammingSystem Design, and more! To check your competency in coding, try several mock test series and participate in the contests on Coding Ninjas Studio! If you have just begun your learning process and are looking for tech giants like Uber, Microsoft, Amazon, etc., it would help to look at the problemsinterview experiences, and interview bundles for the preparation of placements.

Do upvote our blogs if you find them helpful and engaging!

Happy learning!

Live masterclass