1. Login page.
index.jsp.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSF Login Application</title>
</head>
<body>
<f:view>
<h:form>
<table style="width:80%;border:1px solid black;margin-top:2px;margin-left:10%;margin-right:10%;">
<tr>
<td style="text-align:center;font-width:10px;font-weight:bold;">JSF Login Application</td>
</tr>
</table>
<table style="width:80%;border:1px solid black;margin-top:2%;margin-left:10%;margin-right:10%;">
<tr>
<td>
<h:outputText>User Name:</h:outputText>
</td>
<td>
<h:inputText label="User name" id="username" value="#{loginBean.userName}" required="true"/>
<h:message for="username"/>
</td>
</tr>
<tr>
<td>
<h:outputText>Password:</h:outputText>
</td>
<td>
<h:inputSecret label="Password" id="password" value="#{loginBean.password}" required="true"/>
<h:message for="password"></h:message>
</td>
</tr>
<tr>
<td colspan=2>
<h:commandButton value="Login" action="#{loginBean.login}"></h:commandButton>
</td>
</tr>
</table>
</h:form>
</f:view>
</body>
</html>
2. Success page.
success.jsp.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Success</title>
</head>
<body>
<p>Your are successfully login into your application</p>
</body>
</html>
3. Failure page.
Failue.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSF Login Application</title>
</head>
<body>
<p>Oops....Login Failed.....!</p>
</body>
</html>
4. Faces configuration.
Faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<managed-bean>
<managed-bean-name>loginBean</managed-bean-name>
<managed-bean-class>loginPack.LoginBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<navigation-rule>
<from-view-id>index.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>success.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>failure</from-outcome>
<to-view-id>failure.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
5. Managed bean
Name: LoginBean.java
package loginPack;
public class LoginBean {
private String userName;
private String password;
public void setUserName(String userName){
this.userName = userName;
}
public String getUserName(){
return userName;
}
public void setPassword(String password){
this.password = password;
}
public String getPassword(){
return password;
}
public String login(){
String status=new String();
if((userName.equals("Tiger"))&&(password.equals("Tiger")))
status = "success";
else
status = "failure";
return status;
}
}
If your user name is Tiger and your password is Tiger then it is allowed you to login, otherwise not.
Comments