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.

JSF Validation In Login Application

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

<f:view>
<html>
<head><title>JSF Simple Login Example</title></head>

<body>
<h:form>
<table>
<tr>
<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>
</tr>
<tr>
<td><h:outputText value="Enter Password:
" /></td>
<td><h:inputSecret id="password" value="
#{SimpleLogin.password}" required="true" />
<font color="#FF0000"><h:message for="
password"/></font></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><h:commandButton value="Login" action
="#{SimpleLogin.CheckValidUser}" /></td>
</tr>
</table>
</h:form>
</body>
</html>
</f:view>

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(){
        if(loginname.equals("chandan") 
&& 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"
"http://java.sun.com/dtd/web-facesconfig_1_1.dtd">

<faces-config>
<managed-bean>
<managed-bean-name>
SimpleLogin
</managed-bean-name>
<managed-bean-class>
roseindia.SimpleLogin
</managed-bean-class>
<managed-bean-scope>
request
</managed-bean-scope>
</managed-bean>
<navigation-rule>
<from-view-id>/login.jsp</from-view-id>
<navigation-case>
<from-action>
#{SimpleLogin.CheckValidUser}
</from-action>
<from-outcome>
success
</from-outcome>
<to-view-id>
resultforsuccess.jsp
</to-view-id>
</navigation-case>
<navigation-case>
<from-action>#{SimpleLogin.CheckValidUser}
</from-action>
<from-outcome>fail</from-outcome>
<to-view-id>resultforfail.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>

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">

<web-app>
    <context-param>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>server</param-value>
    </context-param>    

    <!-- Faces Servlet -->
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup> 1 </load-on-startup>
    </servlet>

	<!-- Faces Servlet Mapping -->
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
</web-app>

Download This Complete Application.