Home Jsf JSF Simple Login Application

Related Tutorials


 
 

Share on Google+Share on Google+

JSF Simple Login Application

Advertisement
This is the login application in which you will get the complete login facilities whatever should be in a web based application for protecting the page or the website accessibility. This application provides authentication for accessing application after
<%@ 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}" />
</td>
</tr>
<tr>
<td><h:outputText value="Enter Password: " /></td>
<td><h:inputSecret id="password" 
value="#{SimpleLogin.password}" />
</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.

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 26, 2008

Related Tutorials

Discuss: JSF Simple Login Application   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:9
bheem vimal
October 25, 2011
Query regarding jsf 1.2 login

i got same problem.please assist me. my directory structure is as webapps-->JsfMysql1 -->(pages directory & WEB-INF directory) pages-->(login.jsp,success.jsp,fail.jsp) WEB-INF-->(src & lib directory and web.xml & faces-config.xml) src-->java-->mydbtest-->login-bean.java --------------------------------------------------------------- code for "login .jsp" is as: ------------------------------------------------------------------------------------------ <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%> <%@taglib uri="http://java.sun.com/jsf/core" prefix="f" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Data Connectivity</title> </head> <link rel="stylesheet" type="text/css" href="CSS/style.css"> <body> <f:view> <h:form id="f1"> <h2>Login Please:</h2> <table width="250" border="0" cellspacing="0" cellpadding="2"> <tr> <td colspan="2"> <h:message for="username" styleClass="errorMsg"/><br> <h:message for="password" styleClass="errorMsg"/> </td> </tr> <tr> <td><h:outputText value="Username : "/></td> <td><h:inputText id="username" value="#{login_bean.username}" required="true" styleClass="input_text"/></td> </tr> <tr> <td><h:outputText value="Password : "/></td> <td><h:inputSecret id="password" value="#{login_bean.password}" required="true" styleClass="input_text"/></td> </tr> <tr> <td colspan="2" align="center"> <h:commandButton action="#{login_bean.checkValidUser}" value="Login" type="submit"/></td> </tr> </table> </h:form> </f:view> </body> </html> ------------------------------------------------------------------------ code of "login-bean.java" ----------------------------------------------------------------- package mydbtest; import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped; import java.sql.*; import java.util.*; @ManagedBean(name="login_bean") @RequestScoped public class login_bean { private String username; private String dbpassword; private String password; private String dbusername; public String getDbpassword() { return dbpassword; } public String getDbusername() { return dbusername; } Connection con; Statement ps; ResultSet rs; String SQL_Str; public void dbData(String UName) { try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost:3306/logintest","root","vimal"); ps = con.createStatement(); SQL_Str="Select * from login where UName like ('" + UName +"')"; rs=ps.executeQuery(SQL_Str); rs.next(); dbusername=rs.getString(1).toString(); dbpassword=rs.getString(2).toString(); } catch(Exception ex) { ex.printStackTrace(); System.out.println("Exception Occur :" + ex); } } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String checkValidUser() { dbData(username); if(username.equalsIgnoreCase(dbusername)) { if(password.equals(dbpassword)) return "valid"; else { return "invalid"; } } else { return "invalid"; } } } ------------------------------------------------------------------------------------ code of "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>login_bean</managed-bean-name> <managed-bean-class>mydbtest.login_bean</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> </managed-bean> <navigation-rule> <description>Loging Page</description> <from-view-id>pages/login.jsp</from-view-id> <navigation-case> <from-action>#{login_bean.checkValidUser}</from-action> <from-outcome>valid</from-outcome> <to-view-id>pages/success.jsp</to-view-id> </navigation-case> <navigation-case> <from-action>#{login_bean.checkValidUser}</from-action> <from-outcome>invalid</from-outcome> <to-view-id>/pages/fail.jsp</to-view-id> </navigation-case> </navigation-rule> <navigation-rule> <description>ReLoging Page</description> <from-view-id>/pages/fail.jsp</from-view-id> <navigation-case> <from-action>#{login_bean.checkValidUser}</from-action> <from-outcome>valid</from-outcome> <to-view-id>/pages/success.jsp</to-view-id> </navigation-case> <navigation-case> <from-action>#{login_bean.checkValidUser}</from-action> <from-outcome>invalid</from-outcome> <to-view-id>/pages/fail.jsp</to-view-id> </navigation-case> </navigation-rule> <application> <message-bundle>messages.message</message-bundle> </application> </faces-config> -------------------------------------------- code of "web.xml" -------------------------------------------------------------- <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <listener> <listener-class>com.sun.faces.config.ConfigureListener</listener-class> </listener> <!-- 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> <welcome-file-list> <welcome-file>/pages/login.jsf</welcome-file> </welcome-file-list> </web-app> --------------------------------------- code of "success.jsp" --------------------------------------------------------- <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%> <%@taglib uri="http://java.sun.com/jsf/core" prefix="f" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>SuccessPage</title> </head> <body> <f:view> <h:form> <h2>Hello <h:outputText value="#{login_bean.username}"/>, you are successfully login.</h2> </h:form> </f:view> </body> </html> ------------------------------------------------- code of "fail.jsp" ------------------------------------------------------------- <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%> <%@taglib uri="http://java.sun.com/jsf/core" prefix="f" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>LOGIN</title> </head> <link rel="stylesheet" type="text/css" href="CSS/style.css"> <body> <f:view> <h:form id="login_frm"> <h2>Login Please:</h2> <table width="250" border="0" cellspacing="0" cellpadding="2"> <tr> <td colspan="2"> Incorrect Username or Password!!<br> <h:message for="username" styleClass="errorMsg"/><br> <h:message for="password" styleClass="errorMsg"/> </td> </tr> <tr> <td><h:outputText value="Username : "/></td> <td><h:inputText id="username" value="#{login_bean.username}" required="true" styleClass="input_text"/></td> </tr> <tr> <td><h:outputText value="Password : "/></td> <td><h:inputSecret id="password" value="#{login_bean.password}" required="true" styleClass="input_text"/></td> </tr> <tr> <td colspan="2" align="center"> <h:commandButton action="#{login_bean.checkValidUser}" value="Login" type="submit"/></td> </tr> </table> </h:form> </f:view> </body> </html> ---------------------------------------------------------------------------------- database name: logintest table name: login table attribute is uname varchar(11) upass varchar(11) ------------------------------------------------------------------------------------- I tried this above code for jsf 1.2 It runs but not displaying any meassage like login success or fail It stucks on login .jsp page. Please assist me soon.
bala
December 22, 2011
Very good one

Nice website to learn
Arunprasad
April 10, 2012
Navigation not working.

In this above program . i can't perform navigation i.e.After click the login button next page doesn't open(success or Failure) again shows the login page
sam
April 24, 2012
post links missing

You have not provided the links for resultforfail.jsp and resultforsuccess.jsp..and downloading you application doesn't help either, those files r missing..
nour
June 23, 2012
text

hello, I am newbie in jsf I want to secure my application with a session but I ais no EDEE how gere in jsf qulque helps me, thank you
mdmeraj
July 10, 2012
login application

login name and password example u can search in php c# and vb.net and also this
oumayma bounouh
July 26, 2012
jpa jsf

plz, i have already done this exemple, and it runs correctly. but if i have a database how could I check or verify if the login and the password exist in this Data base or not? (I have used JPA)... Sincerly
siva
December 26, 2013
Jsf in Jsp page program

simple and easy undertatend
siva
December 26, 2013
Jsf in Jsp page program

simple and easy undertatend
DMCA.com