Homework 1
1. Create the web application project
Choose File > New Project (Ctrl-Shift-N; -Shift-N on Mac) from the main menu. Select Web Application
from the Java Web category and click Next.
Type Homework1 for the project name and set the project location.
Deselect the Use Dedicated Folder option, if selected. Click Next.
Set the server to the GlassFish Server and set the Java EE Version to Java EE 6 Web or Java EE 7 Web. Click
Next.
Select the JavaServer Faces checkbox and use the default JSF 2.x libraries.
2. Create the database
https://netbeans.org/kb/docs/ide/mysql.html
3. Create the login page
Replace the code generated with
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@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=UTF-8">
<title>LOGIN Page</title>
</head>
<body>
<f:view>
<h1><h:outputText value="Login" /></h1>
<h:form>
Welcome to our site!<br />
Username: <h:inputText value="#{user.currentUser.username}" /> <br />
Password: <h:inputSecret value="#{user.currentUser.password}" /> <br />
<h:outputText value="#{user.message}" /> <br />
<h:commandButton action="#{user.getCompleted}" id="Login" value="Login" /> <br />
</h:form>
</f:view>
</body>
</html>
Modify the web.xml file from the Configuration file package
4. Create the managed bean
@ManagedBean (name="user")
@SessionScoped
public class UserBean implements Serializable{
/**
* Creates a new instance of UserBean
*/
private String username;
private String password;
}
The login method:
public String getCompleted(){
if(user is admin)
return "Succes";
}else
if(user is regular user)
return "User";
}
else {
return "Error";
}
}
5. The faces.config file
Used to navigate between pages
The file must be added if it does not exist in the Configuration Files package
Each method from the managed bean that navigates between the pages must return a string
The string is used in the faces.config file in a navigation rule
o <navigation-rule>
o <description>
o
o </description>
o <from-view-id>/login_page.jsp</from-view-id>
o <navigation-case>
o <from-outcome>Succes</from-outcome>
o <to-view-id>/index.xhtml</to-view-id>
o </navigation-case>
o <navigation-case>
o <from-outcome>Error</from-outcome>
o <to-view-id>/login_page.jsp</to-view-id>
o </navigation-case>
o <navigation-case>
o <from-outcome>User</from-outcome>
o <to-view-id>/user.jsp</to-view-id>
o </navigation-case>
o </navigation-rule>
<?xml version='1.0' encoding='UTF-8'?>
<!-- =========== FULL CONFIGURATION FILE ================================== -->
<faces-config version="2.1"
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_1.xsd">
<navigation-rule>
<description>
</description>
<from-view-id>/login_page.jsp</from-view-id>
<navigation-case>
<from-outcome>Succes</from-outcome>
<to-view-id>/index.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>Error</from-outcome>
<to-view-id>/login_page.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>User</from-outcome>
<to-view-id>/user.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
6. Connect to the database
Use JDBC and queries
Use JPA or Hibernate
o https://netbeans.org/kb/docs/web/hibernate-webapp.html#02
7. Finish the application