Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2011
    Posts
    36
    Rep Power
    0

    Default How can I improve -> Simple JSF Login example

    Hi everyone,

    I've successfully implemented a simple Login using JSF, now I need to know how can I improve my code. All comments will be appreciated.

    index.jsp
    Java Code:
    <%@ 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>Index</title>
    </head>
    <body>
    	<%
    		response.sendRedirect("login-input.jsf");
    	%>
    </body>
    </html>
    login-input.xhtml
    Java Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
    	xmlns:h="http://java.sun.com/jsf/html"
    	xmlns:f="http://java.sun.com/jsf/core">
    
    <h:head>
    	<title>Login input</title>
    </h:head>
    
    <h:body>
    	<fieldset>
    		<legend>Enter you login data</legend>
    		<h:form>
    			<table>
    				<tr align="left">
    					<th>Username</th>
    					<th><h:inputText value="#{loginForm.userLogin}" /></th>
    				</tr>
    				<tr align="left">
    					<th>Password</th>
    					<th><h:inputSecret value="#{loginForm.userPassword}" /></th>
    				</tr>
    				<tr align="left">
    					<th><h:commandButton value="Submit" action="#{loginForm.doLogin}" />
    					</th>
    				</tr>
    			</table>
    		</h:form>
    	</fieldset>
    </h:body>
    
    </html>
    login-ok.xhtml
    Java Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
    	xmlns:h="http://java.sun.com/jsf/html"
    	xmlns:f="http://java.sun.com/jsf/core">
    
    <h:head>
    	<title>Login OK</title>
    </h:head>
    
    <h:body>
    	<h2>Welcome #{loginForm.userName}, you have a successful Login.</h2>
    	<ul>
    		<li>User ID: #{loginForm.userId}</li>
    		<li>User login: #{loginForm.userLogin}</li>
    		<li>User password: #{loginForm.userPassword}</li>
    	</ul>
    	<h3>
    		Return to <a href="index.jsp">Login page</a>.
    	</h3>
    </h:body>
    
    </html>
    login-missing-data.xhtml
    Java Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
    	xmlns:h="http://java.sun.com/jsf/html"
    	xmlns:f="http://java.sun.com/jsf/core">
    
    <h:head>
    	<title>Login error - missing data</title>
    </h:head>
    
    <h:body>
    	<h2>Login error - missing data</h2>
    	<p>You must insert User login details.</p>
    	<h3>
    		Back to <a href="index.jsp">Login page</a>.
    	</h3>
    </h:body>
    
    </html>
    login-error.xhtml
    Java Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
    	xmlns:h="http://java.sun.com/jsf/html"
    	xmlns:f="http://java.sun.com/jsf/core">
    
    <h:head>
    	<title>Login error</title>
    </h:head>
    
    <h:body>
    	<h2>Login error</h2>
    	<ul>
    		<li>User login: #{loginForm.userLogin}</li>
    		<li>Password: #{loginForm.userPassword}</li>
    	</ul>
    	<h3>
    		Invalid login, back to <a href="index.jsp">Login page</a>.
    	</h3>
    </h:body>
    
    </html>
    faces-config.xml

    Java Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xi="http://www.w3.org/2001/XInclude"
     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">
     <managed-bean>
      <managed-bean-name>loginForm</managed-bean-name>
      <managed-bean-class>login.LoginForm</managed-bean-class>
      <managed-bean-scope>session</managed-bean-scope>
     </managed-bean>
     <navigation-rule>
      <from-view-id>/login-input.xhtml</from-view-id>
      <navigation-case>
       <from-outcome>error</from-outcome>
       <to-view-id>/login-error.xhtml</to-view-id>
      </navigation-case>
      <navigation-case>
       <from-outcome>ok</from-outcome>
       <to-view-id>/login-ok.xhtml</to-view-id>
      </navigation-case>
      <navigation-case>
       <from-outcome>missing</from-outcome>
       <to-view-id>/login-missing-data.xhtml</to-view-id>
      </navigation-case>
     </navigation-rule>
    </faces-config>
    web.xml
    Java Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" 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-app_3_0.xsd">
     <display-name>SimpleLogin</display-name>
     <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>*.jsf</url-pattern>
     </servlet-mapping>
    </web-app>
    UserPOJO.java
    Java Code:
    package login;
    
    public class UserPOJO {
    
    	private int userId;
    	private String userName;
    	private String userLogin;
    	private String userPassword;
    
    	public UserPOJO() {
    	}
    
    	public UserPOJO(int userId, String userName, String userLogin,
    			String userPassword) {
    		super();
    		this.userId = userId;
    		this.userName = userName;
    		this.userLogin = userLogin;
    		this.userPassword = userPassword;
    	}
    
    	@Override
    	public String toString() {
    		return "User [userId=" + userId + ", userName=" + userName
    				+ ", userLogin=" + userLogin + ", userPassword=" + userPassword
    				+ "]";
    	}
    
    	public int getUserId() {
    		return userId;
    	}
    
    	public void setUserId(int userId) {
    		this.userId = userId;
    	}
    
    	public String getUserName() {
    		return userName;
    	}
    
    	public void setUserName(String userName) {
    		this.userName = userName;
    	}
    
    	public String getUserLogin() {
    		return userLogin;
    	}
    
    	public void setUserLogin(String userLogin) {
    		this.userLogin = userLogin;
    	}
    
    	public String getUserPassword() {
    		return userPassword;
    	}
    
    	public void setUserPassword(String userPassword) {
    		this.userPassword = userPassword;
    	}
    }
    UserUtils.java
    Java Code:
    package login;
    
    public class UserUtils {
    
    	/**
    	 * Define all available Users, static so we just have one instance
    	 */
    	private static UserPOJO[] availableUsers = {
    			new UserPOJO(0, "Antonio Godinho", "log001", "pass001"),
    			new UserPOJO(1, "Maria Alberta", "log002", "pass002"),
    			new UserPOJO(2, "Joao Farias", "log003", "pass003") };
    
    	/**
    	 * 
    	 * @return all available Users
    	 */
    	public static UserPOJO[] getAvailableUsers() {
    		return availableUsers;
    	}
    
    	/**
    	 * 
    	 * @param userPassword
    	 * @return User if Password exists
    	 */
    	public static UserPOJO findUser(String userLogin, String userPassword) {
    		for (UserPOJO user : availableUsers) {
    			if (user.getUserLogin().equals(userLogin)
    					&& user.getUserPassword().equals(userPassword)) {
    				return user;
    			}
    		}
    		return null;
    	}
    
    	/**
    	 * Non instantiatable class
    	 */
    	private UserUtils() {
    	}
    }
    LoginForm.java
    Java Code:
    package login;
    
    import javax.annotation.ManagedBean;
    
    @ManagedBean
    public class LoginForm {
    
    	// Bean Properties
    	private UserPOJO user;
    	private int userId;
    	private String userName;
    	private String userLogin;
    	private String userPassword;
    
    	public UserPOJO getUser() {
    		return user;
    	}
    
    	public int getUserId() {
    		return userId;
    	}
    
    	public void setUserId(int userId) {
    		this.userId = userId;
    	}
    
    	public String getUserName() {
    		return userName;
    	}
    
    	public void setUserName(String userName) {
    		this.userName = userName;
    	}
    
    	public String getUserLogin() {
    		return userLogin;
    	}
    
    	public void setUserLogin(String userLogin) {
    		this.userLogin = userLogin;
    	}
    
    	public String getUserPassword() {
    		return userPassword;
    	}
    
    	public void setUserPassword(String userPassword) {
    		this.userPassword = userPassword;
    	}
    
    	// Action
    	public String doLogin() {
    		if (userLogin.isEmpty() || userPassword.isEmpty()) {
    			return "missing";
    		} else {
    			user = UserUtils.findUser(userLogin, userPassword);
    			if (user == null) {
    				return "error";
    			} else {
    				userId = user.getUserId();
    				userName = user.getUserName();
    				return "ok";
    			}
    		}
    	}
    }
    Thank you for your time
    There is not knowledge that it is not power!
    buyapentiumjerk.blogspot.com

  2. #2
    hugomx is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default

    Looks great, so as a tip you can avoid
    Java Code:
     if (userLogin.isEmpty() || userPassword.isEmpty()) {
    			return "missing";
    		}
    instead using
    Java Code:
    <h:inputText value="#{loginForm.userLogin}" required="true"/>
    As any way is great

  3. #3
    Join Date
    Apr 2011
    Posts
    36
    Rep Power
    0

    Default

    Hi hugomx,

    thank you for your answer, I didn't knew about that.
    I'm now using PrimeFaces and added a bit more of complexity, I've created a new Class Company for all Users and I'm trying to use Hibernate with all this, let me see what I can come up with. One question, normally I'd create a List<UserPOJO> companyUsers; in class Company to get all Users allocated to that Company, but I've been reading some tutorials and some say to use HashMap instead of List but this seems to be the prefered way with JSF 1.x?
    There is not knowledge that it is not power!
    buyapentiumjerk.blogspot.com

Similar Threads

  1. What can i do to improve this? Explanation please
    By biggerthanblue in forum New To Java
    Replies: 1
    Last Post: 02-19-2011, 06:55 AM
  2. Improve my GUI!
    By AJArmstron@aol.com in forum New To Java
    Replies: 8
    Last Post: 04-27-2010, 09:17 PM
  3. how to improve my security?
    By anthrax in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-13-2009, 09:08 AM
  4. Replies: 0
    Last Post: 06-25-2008, 12:41 PM
  5. how to improve the performance of JWS?
    By dinesh kaushik in forum Java Applets
    Replies: 0
    Last Post: 11-21-2007, 08:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •