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

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

Meteor tutorial - Part II - Building mobile applications

In my previous blog post , I have explained basics of meteor. In this tutorial I am going to add mobile support to the existing meteor application. Installing mobile SDKs Before going to start we need to install IOS and android SDKs (Software development kit). Here I am installing SDKs for iOS. Please keep in mind this is one time process. For installing iOS SDKs, we need to invoke “meteor install-sdk ios”. The same way you can install android SKDs by invoking “meteor install-sdk android” command. Adding mobile platform support I am using already created application called “myapp” and going to add iOS platform support by invoking a command “ meteor add-platform ios ”. We can add android platform by invoking a command  “ meteor add-platform android ”. Running application on mobile emulator Once we add mobile platform then we can run our application by using real mobile device or emulator. I have added IOS platform to myapp already. Now I am going to ru...

Meteor tutorial - Part I

What is Meteor? Meteor is a complete open source platform for building web and mobile application by using JavaScript. It is a combination of NodeJs, MongoDB and client side JavaScript. Installing meteor Meteor supports windows, linux and Mac operating systems. Invoke below command in terminal for installing meteor in mac operating system. Check below link for installing meteor on other operating systems. http://docs.meteor.com/#/basic/quickstart Creating an application The command “meteor create <<app_name>>” is used for creating new meteor application. I have created an app called “myapp” and will see how we can run the application. Running an application “meteor run” command is used for running meteor application. Internally it start a node js server and by default that will listen 3000 port number. Just make sure you are in project directory (myapp) before invoking run command. ' Other useful meteor commands Meteor provides lot of usefu...