CSC444 - Assignment 3
CSC444 - Assignment 3
Assignment 3
JEE - (10%)
JSP, Servlet, DAO, JDBC Full project
example
New requirements:
- add on a registration page with 2 columns
(username, password)
- make sure it is insertable to database when
register as a new user.
Before start, if you open Oracle XE and encounter error msg like this, means your Oracle Services is
stopped,
Sometimes, when you open Eclipse and restart Server of the Dynamic Web
Project, but encounter this msg(8080 in used why?, how to solve it?
From here, we know that port 8080 being used my Listerner Oracle TNSLSNR
OR Maybe you thought is Server being restarted, so you try to go Tomcat start the server again.
UiTM Melaka Kampus Jasin CSC444 Enterprise Java Programming (A3)
Then go to Eclipse Restart the server again, but now the error message is different.
UiTM Melaka Kampus Jasin CSC444 Enterprise Java Programming (A3)
How to change?
DONE….
1. The user enters his username and password in the fields displayed by the JSP - index.jsp -
2. When the user submits, the servlet responsible for handling the request is called -
LoginServlet -
3. The Servlet is responsible for calling the appropriate method in the DAO so that it can
indirectly interact with the DB.
It is also responsible for setting and updating data saved in the bean, which will be used later
by the DAO.
In our application,
UiTM Melaka Kampus Jasin CSC444 Enterprise Java Programming (A3)
o The LoginServlet creates a new instant of the UserBean and fills it with the username
and the password entered by the user. The DAO will use this bean later to compare
between the user input and the DB data
o The Servlet calls the "login" method in the "UserDAO" to start performing its task
4. The Login method, in the DAO, is responsible for checking whether the data entered by the
user exists in the DB or not.
In addition, it has to update the Bean's data that will be used later by the servlet.
In our application,
▪ Otherwise, the DAO sets the "valid" attribute of the bean to false
5. Finally, the Servlet will check the validity of the user (by reading the valid attribute of the
bean) and redirect to the appropriate JSP .
o If valid, the servlet will
▪ Add the bean as an attribute to the session. The bean will be used by the JSP
to display the user's first and last names
▪ Redirect to home.jsp- That will welcome the user
o If invalid, the servlet will redirect to invalidLogin.jsp- That will ask the user to sign
up
Open eclipse
Create a new "Dynamic Web Project"
Name it "LoginExampleProject"
Create the JSP
In the "Web Content" folder, create a new JSP
Name it "index.jsp"
Place this code
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
<title>Login Page</title>
</head>
<body>
<form action="login">
</form>
</body>
</html>
As you can see; (in the LoginPage which is index.jsp) when the user submits, the JSP calls
"LoginServlet".This LoginServlet is intended to handle the Business logic associated with the request.
package com.example;
import java.io.IOException;
import javax.servlet.ServletException;
UiTM Melaka Kampus Jasin CSC444 Enterprise Java Programming (A3)
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class LoginServlet
*/
public class LoginServlet extends HttpServlet {
try {
user = UserDAO.login(user);
if (user.isValid()) {
else
response.sendRedirect("invalidLogin.jsp"); // error page
}
The login servlet instantiates a Bean that is of type "UserBean", and then calls the DAO named
"UserDAO".
Our UserBean is a class representing the User table in our Database (where each column in the user table
has a corresponding instance variable with a setter and a getter method).
The DAO, as said before, contains methods needed to communicate with the data source. In our example,
the only needed method is the login method that checks if the username and password inputted by the
user are valid or not.
Before implementing the DAO, you need to prepare your Data Source.
password varchar2(20)
);
Insert 3 users;
insert into myuser values ("Raymond", "Chew", "ray", "1234");
insert into myuser values ("Mohammad", "Ali", "ali", "1234");
insert into myuser values ("James", "Bond", "james", "1234");
Name it myuser
Create the columns: 'FirstName', 'LastName', 'username', and 'password'
Refer to your DB as a data source from "Administrative tools" in Control Panel
Please follow these steps to implement the Bean and the DAO
package com.example;
package com.example;
import java.text.*;
import java.util.*;
import java.sql.*;
try {
// connect to DB
currentCon = ConnectionManager.getConnection();
stmt = currentCon.createStatement();
rs = stmt.executeQuery(searchQuery);
boolean more = rs.next();
if (stmt != null) {
try {
stmt.close();
} catch (Exception e) {
}
stmt = null;
}
UiTM Melaka Kampus Jasin CSC444 Enterprise Java Programming (A3)
if (currentCon != null) {
try {
currentCon.close();
} catch (Exception e) {
}
currentCon = null;
}
}
return bean;
}
}
The DAO uses a class named "ConnectionManager" to get a connection with the DB.
package com.example;
import java.sql.*;
import java.util.*;
try {
System.out.println("connected222");
Class.forName(DB_DRIVER);
System.out.println("connected333");
try {
con = DriverManager.getConnection(DB_CONNECTION,DB_USER,DB_PASSWORD);
System.out.println("connected");
}
UiTM Melaka Kampus Jasin CSC444 Enterprise Java Programming (A3)
catch (ClassNotFoundException e) {
System.out.println(e);
}
return con;
}
}
• home.jsp: Displays a message to welcome the user, using his first and last names (retrieved from
the DB)
• invalidLogin.jsp: Displays a message to inform the user that he is not a registered user
No.6. Steps to create the home.jsp (After login successfully from index.jsp)
<html>
<head>
<meta http-equiv="Content-Type"
UiTM Melaka Kampus Jasin CSC444 Enterprise Java Programming (A3)
content="text/html; charset=windows-1256">
<title> User Logged Successfully </title>
</head>
<body>
<center>
<%
UserBean currentUser = (UserBean) session.getAttribute("currentSessionUser");
%>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=windows-1256">
<title>Invalid Login</title>
</head>
<body>
<center>
Sorry, you are not a registered user! Please sign up first
</center>
</body>
</html>
UiTM Melaka Kampus Jasin CSC444 Enterprise Java Programming (A3)
Run the application, If you do not know any of the following steps, please check Steps 5-8 in
the JSP Example
Set index.jsp to be your Home page (from web.xml)