JSF simple login example
This application provides authentication for accessing application
1). JSF login example structure
2).index.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>index.html</title>
<meta http-equiv="Refresh" content="0; URL=index.faces"/>
</head>
</html>
3). index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<f:view>
<h:form>
<h:messages style="color: red;"></h:messages>
<table><h:outputText value="Enter following Information"/> <tr>
<td>
<h:outputText value="Name:"/>
</td>
<td>
<h:inputText id="name" value="#{Bean.name}" required="true">
<f:validateLength minimum="2" maximum="25"/>
</h:inputText>
</td>
</tr><tr>
<td>
<h:outputText value="Password:"></h:outputText>
</td>
<td>
<h:inputSecret id="pass" value="#{Bean.pass}" required="true">
<f:validateLength minimum="2" maximum="25"></f:validateLength>
</h:inputText>
</td>
</tr>
<tr>
<td>
<h:commandButton value="Login" action="#{Bean.login}"></h:commandButton>
</td>
</tr>
</table>
</h:form>
</f:view>
</body>
</html>
4). web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<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>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
5). 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_1_2.xsd"
version="1.2">
<managed-bean>
<managed-bean-name>Bean</managed-bean-name>
<managed-bean-class>com.satyamsoft.Bean</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>fail</from-outcome>
<to-view-id>/fail.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
6). Bean.java
package com.satyamsoft;
import java.io.Serializable;
public class Bean implements Serializable{
private static final long serialVersionUID = 1L;
String name;
String pass;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String login() throws Exception
{
if(name.equals("satyam") && pass.equals("12345")){
return "success";
}
else{
return "fail";
}
}
}
7). success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<f:view>
<table>
<tr>
<td><b>
<h:outputText value=" Hello #{Bean.name} "></h:outputText></b><br/>
<b><h:outputText value=" You are successfully logged in"></h:outputText></b>
</td>
</tr>
<tr>
<td>
<a href="/JSFLoginExample/index.faces">Back</a>
</td>
</tr>
</table>
</f:view>
</body>
</html>
8). fail.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<f:view>
<table>
<tr>
<td><b>
<h:outputText value="Enter Correct details"></h:outputText></b>
</td>
</tr>
<tr>
<td>
<a href="/JSFLoginExample/index.faces">Back</a>
</td>
</tr>
</table>
</f:view>
</body>
</html>
9). Output
No comments:
Post a Comment