JSF Validation In Login Application

This section gives you the best approach for the login application. You have learnt about the login application without validating the application. But in this section you will learn how to validate a form in JSF using it's tags.

<%@ page contentType="text/html"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>

<head><title>JSF Simple Login Example</title></head>

<td><h:outputText value="Enter
Login ID: " /></td>
<td><h:inputText id="loginname"
value="#{SimpleLogin.loginname}" required="true" />
<font color="#FF0000">
<h:message for="loginname"/></font></td>
<td><h:outputText value="Enter Password:
" /></td>
<td><h:inputSecret id="password" value="
#{SimpleLogin.password}" required="true" />
<font color="#FF0000"><h:message for="
<td><h:commandButton value="Login" action
="#{SimpleLogin.CheckValidUser}" /></td>

Here is the code for the resultforfail.jsp file:

Login Failed!

Here is the code for the resultforsuccess.jsp file:

Login Successful!

Here is the code for the Backing Bean SimpleLogin.java file:

package roseindia;

public class SimpleLogin{
	String loginname;
	String password;

	public SimpleLogin(){}

	public String getLoginname(){
		return loginname;

	public void setLoginname(String loginname){
		this.loginname = loginname;

	public String getPassword(){
		return password;

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

	public String CheckValidUser(){
&& password.equals(
"chand")){ System.out.println("chandan"); return "success"; } else{ return "fail"; } } }

faces-config.xml file code:

<?xml version="1.0"?>
<!DOCTYPE faces-config PUBLIC
"-//Sun Microsystems, 
Inc.//DTD JavaServer Faces Config 1.1//EN"


web.xml file code:

<?xml version="1.0"?> 
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 
2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">


    <!-- Faces Servlet -->
        <servlet-name>Faces Servlet</servlet-name>
        <load-on-startup> 1 </load-on-startup>

	<!-- Faces Servlet Mapping -->
        <servlet-name>Faces Servlet</servlet-name>

Download This Complete Application.

Share on Google+Share on Google+

JSF Validation In Login Application

Posted on: February 26, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: JSF Validation In Login Application   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image