A Simple JSF Example
First, create a simple login page containing JSTL tags and references to a managed bean. A user can log in and the JSP will interface directly with the managed bean to authenticate the user against the back-end storage. The file should go into the main project directory.
login.jsp (WebContent/)
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %> <html> <head> <title>Login Page</title> </head> <body> <f:view> <h:form id="helloForm"> <Center> <H2>Logon Page</H2> </Br></Br> <table> <tr> <td> <h:outputLabel for="input1"> <h:outputText id="nameLabel" value="User Name"/> </h:outputLabel> </td> <td> <h:inputText id="input1" value="#{logonBean1.userName}" size="20"/> </td> </tr> <tr> <td> <h:outputLabel for="input2"> <h:outputText id="passwordLabel" value="Password"/> </h:outputLabel> </td> <td> <h:inputSecret id="input2" value="#{logonBean1.password}" size="20"/> </td> </tr> <tr> <td></td> <td> <h:commandButton id="logon" action="#{logonBean1.validate}" value="Logon"> </h:commandButton> </td> </tr> </table> </Center> </h:form> </f:view> </body> </html>
Next, a simple welcome page for authenticated users. The file should go into the main project directory.
welcome.jsp (WebContent/)
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %> <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> <html> <head> <title>Greeting Page</title> </head> <body> <f:view> <h:form id="greetingForm"> <Center> <H2>Greeting Page</H2> </Br></Br> <H4>Hello <h:outputText value="#{logonBean1.userName}"/>! You have been successfully authenticated. </H4> </Br></Br> <h:commandLink id="link" action="logout"> <h:outputText value="Logout"/> </h:commandLink> </Center> </h:form> </f:view> </body> </html>
The two JSP pages above use the backing bean below, a simple Java object that can be easily modified using annotations for persistence or transaction state management. The file should go into your web-inf/classes/logon directory.
LogonBean.java (WEB-INF/classes/logon)
package logon; import java.util.*; public class LogonBean { private String userName; private String password; public LogonBean() { } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName=userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password=password; } public String validate() { String flag="failure"; ResourceBundle logonCredential = ResourceBundle.getBundle("Logon"); Enumeration keys = logonCredential.getKeys(); while (keys.hasMoreElements()) { String key = (String)keys.nextElement(); String value = logonCredential.getString(key); if (userName.equalsIgnoreCase(key)&&password.equals(value)) { flag="success"; return flag; } } }
The bean above uses the property file below to verify the user credentials. The file should go in your project's "classes" directory.
Login.properties (WEB-INF/classes directory)
admin=password
The last thing you need before running the application on the server is a faces-config.xml and a deployment descriptor file (web.xml). They should be configured like the following examples.
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>logonBean1</managed-bean-name> <managed-bean-class>logon.LogonBean</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> </managed-bean> <navigation-rule> <display-name>login</display-name> <from-view-id>/login.jsp</from-view-id> <navigation-case> <to-view-id>/welcome.jsp</to-view-id> </navigation-case> </navigation-rule> </faces-config>
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/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Webbeans</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <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> <context-param> <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name> <param-value>resources.application</param-value> </context-param> <context-param> <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description> <param-name>javax.faces.STATE_SAVING_METHOD</param-name> <param-value>client</param-value> </context-param> <context-param> <description> This parameter tells MyFaces if javascript code should be allowed in the rendered HTML output. If javascript is allowed, command_link anchors will have javascript code that submits the corresponding form. If javascript is not allowed, the state saving info and nested parameters will be added as url parameters. Default is 'true'</description> <param-name>org.apache.myfaces.ALLOW_JAVASCRIPT</param-name> <param-value>true</param-value> </context-param> <context-param> <description> If true, rendered HTML code will be formatted, so that it is 'human-readable' i.e. additional line separators and whitespace will be written, that do not influence the HTML code. Default is 'true'</description> <param-name>org.apache.myfaces.PRETTY_HTML</param-name> <param-value>true</param-value> </context-param> <context-param> <param-name>org.apache.myfaces.DETECT_JAVASCRIPT</param-name> <param-value>false</param-value> </context-param> <context-param> <description> If true, a javascript function will be rendered that is able to restore the former vertical scroll on every request. Convenient feature if you have pages with long lists and you do not want the browser page to always jump to the top if you trigger a link or button action that stays on the same page. Default is 'false' </description> <param-name>org.apache.myfaces.AUTO_SCROLL</param-name> <param-value>true</param-value> </context-param> <listener> <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class> </listener> </web-app>