Skip to main content

Simple login application using JSF


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

Popular posts from this blog

Simple Login Application Using Spring MVC and Hibernate – Part 1

 I hope developers working in web application development might hear about MVC architecture. Almost all technologies provide support for MVC based web application development, but the success is based on many factors like reusable of the code, maintenance of the code, future adaption of the code, etc..,  The success of the Spring MVC is “ Open for extension and closed for modification ” principle. Using Spring MVC the developers can easily develop MVC based web application. We don’t need any steep learning curve at the same time we need to know the basics of spring framework and MVC architecture. The Spring MVC consists of following important components. 1. Dispatcher servlet 2. Controller 3. View Resolver 4. Model Spring MVC - Overview  The overall architecture of Spring MVC is shown here.  1. When “Dispatcher Servlet” gets any request from client, it finds the corresponding mapped controller for the request and just dispatches the request to the corresponding contro

Getting key/value pair from JSON object and getting variable name and value from JavaScript object.

 Hi, I had faced one issue like this. I have an JSON object but I don't know any key name but I need to get the all the key and corresponding value from JSON object using client side JavaScript. Suddenly I wondered whether it's possible or not, after that I had done lot of workaround and finally got this solution. See the below example.    function getKeyValueFromJSON() {     var jsonObj =  {a:10,b:20,c:30,d:50} ;     for ( var key in jsonObj) {       alert( "Key: " + key + " value: " + jsonObj[key]);     }  }  In this example I have created the one json array as string, and converted this string into JSON object using eval() function. Using for-each loop I got all the key value from jsonObj, and finally using that key I got the corresponding value.  Finally I got the alert like this,    Key: a value:10    Key: b value:20    Key: c value:30    Key: d value:50  During this workaround I got one more idea, using this same way I got

Spring - ArrayList configuration using xml bean configuration file

In this example I am going to give example for array list configuration in spring application using XML bean configuration file. This is my bean, here I have created the setter and getter method for array list variable ("countryList").    import java.util.ArrayList;   public class ArrayListConfig {       private ArrayList<String> countryList ;       public ArrayList<String> getCountryList() {             return countryList ;       }       public void setCountryList(ArrayList<String> countryList ) {             this . countryList = countryList ;       }        } This is my beans.xml file. The <list> tag is used for configuring list.       <? xml version = "1.0" encoding = "UTF-8" ?>  < beans xmlns = "http://www.springframework.org/schema/beans"       xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation = "http://www.spri