Introduction to J2EE
Lab Guide
© Infosys Technologies Ltd
No. 350, Hebbal Electronics City, Hootagalli
Mysore – 571186
Author(s) Muruganantham A.
Rajagopalan P.
Authorized by Dr. M. P. Ravindra
Creation Date 18-Jul-2005
Version 1.00
Document Revision Summary
Version Date Author Reviewed by Comments
0.0 18-Jul-05 Muruganantham A Rajagoplan P Initial Draft
1.00 05-Jul-06 Rajagopalan P Sureesh Joseph, Incorporation of review
Meera Gayathri comments and addition
Dharmalingam of extra assignments for
Fast Track Training
TABLE OF CONTENTS
1 BACKGROUND ...................................................................1
2 ASSIGNMENTS FOR INTRODUCTION TO J2EE..............................1
2.1 ASSIGNMENT 1: CREATING A WEB APPLICATION ................................................................... 1
2.2 ASSIGNMENT 2: DEVELOPING AND DEPLOYING A JSP APPLICATION ................................................ 4
2.3 ASSIGNMENT 3: UNDERSTAND THE IMPLICIT OBJECTS OUT AND REQUEST ........................................... 6
2.4 ASSIGNMENT 4: UNDERSTAND THE IMPLICIT OBJECT SESSION....................................................... 8
2.5 ASSIGNMENT 5: REFERRING TO THE DOCUMENTATION OF VARIOUS IMPLICIT OBJECTS ............................ 12
2.6 ASSIGNMENT 6: USING JSP:FORWARD TAG....................................................................... 13
2.7 ASSIGNMENT 7: SESSION TRACKING ............................................................................. 16
2.8 ASSIGNMENT 8: USING JSP:INCLUDE TAG ........................................................................ 17
2.9 ASSIGNMENT 9: USING JAVABEAN IN JSP....................................................................... 19
2.10 ASSIGNMENT 10: UNDERSTANDING PAGE DIRECTIVES ............................................................ 22
2.11 ASSIGNMENT 11: INCLUDE DIRECTIVE IN JSP.................................................................... 24
2.12 ASSIGNMENT 12: WORKING WITH JDBC........................................................................ 27
2.13 ASSIGNMENT 13: SHOPPING CART ............................................................................... 28
Page i
Introduction to J2EE Lab Guide Version 1.0
1 Background
This document contains the assignments to be completed as part of the hands on for the
subject Introduction to J2EE
Note: All assignments in this document must be completed in the sequence
in order to complete the course
2 Assignments for Introduction to J2EE
2.1 Assignment 1: Creating a Web Application
Objective: To understand the different steps involved in creating a new web application in
Tomcat. This assignment should make the trainee create a new application called myapp
At the end of the assignment, the trainee should know the following
Test whether Tomcat is running
The directory structure of a web application
Create a new web application in Tomcat
Background: The Apache’s Tomcat 4.1 is a Java Servlet Container, which supports both Java
Servlet and JavaServer Pages(JSP) technologies. It can be used as a standalone web server,
but it is often used behind web servers such as Apache.
Problem Description:
• Create a web application called myapp
Step 1: Double click the ‘Start Tomcat’ icon on the desktop to run Tomcat server
Step 2: Start the browser and supply the URL: http://localhost:8080
Step 3: If Tomcat is running properly, the browser will display a page similar to the one given
below
Education & Research Department 1
© Infosys Technologies Limited
Introduction to J2EE Lab Guide Version 1.0
Step 4: Create a directory myapp in any convenient location. For example, E:\temp\myapp.
Create a subdirectory WEB-INF. Create two more subdirectories classes and lib under WEB-
INF. Finally the directory structure looks similar to the following.
Classes directory can contain Java Servlet class files
Lib directory can contain other Java files used by the Servlets and JSPs. This can be
the .class file or the .jar file
Education & Research Department 2
© Infosys Technologies Limited
Introduction to J2EE Lab Guide Version 1.0
Step 5: Open server.xml file, which is present under the directory ‘C:\Program Files\Apache
Group\Tomcat 4.1\conf’
Education & Research Department 3
© Infosys Technologies Limited
Introduction to J2EE Lab Guide Version 1.0
Step 6: Under the element section, add the following
The modified server.xml file will look as follows
Step 7: Stop Tomcat Server by double clicking the ‘Stop Tomcat’ icon on the desktop
Step 8: Start Tomcat Server again. Tomcat will read server.xml and recognizes myapp as a new
web application
Summary:
At the end of this assignment, you have learnt,
Test the running status of Tomcat server
The directory structure of a typical web application
2.2 Assignment 2: Developing and Deploying a JSP Application
Objective: To understand the different steps involved in developing and deploying a new JSP
application in Tomcat.
At the end of the assignment, the trainee should know the following
Write a simple JSP program
Deploy a JSP in a web application
Education & Research Department 4
© Infosys Technologies Limited
Introduction to J2EE Lab Guide Version 1.0
Problem Description:
• Create a simple JSP program and deploy the same
Step 1: Open notepad editor and write the following code in the file LongMessageJSP.jsp and
store it under the directory myapp. This JSP file can be copied to the root of the web
application or placed in any subdirectory other than WEB-INF
Long Message JSP
This is a
long message
Education & Research Department 5
© Infosys Technologies Limited
Introduction to J2EE Lab Guide Version 1.0
Step 2: Start the browser and supply the URL
http://localhost:8080/myapp/LongMessageJSP.jsp. The following screen will be displayed.
Summary:
At the end of this assignment, you have learnt,
How to write and deploy a simple JSP program
2.3 Assignment 3: Understand the implicit objects out and request
Objective: To understand the JSP implicit objects out and request.
Background: JSP provides a number of implicit objects such as out and request. These
objects can be accessed by the JSP without explicit declaration. Implicit objects are used
within scriptlets and expressions.
Problem Description:
• Write a JSP program that accepts the name of the user as a parameter and prints the
message Hello
Step 1: Create a folder jsp under myapp directory and open notepad editor to write the
following code in the RequestTestJSP.jsp and store it under the directory jsp as shown below.
Education & Research Department 6
© Infosys Technologies Limited
Introduction to J2EE Lab Guide Version 1.0
Request Test JSP
Hello
Hello
Step 2: Write the following code for the file RequestTestJSPForm.html and Store it under the
directory myapp
Request Test JSP Form
User Name
Step 3: Start the Browser and supply the URL
http://localhost:8080/myapp/RequestTestJSPForm.html. The screen looks like the following
screen shot.
Education & Research Department 7
© Infosys Technologies Limited
Introduction to J2EE Lab Guide Version 1.0
Step 4: Type Infosys in the text box and click Ok Button. The User Name will be passed to the
JSP and the following screen will be displayed.
Summary:
At the end of this assignment, you have learnt:
How to use the implicit object request for receiving the parameters
2.4 Assignment 4: Understand the implicit object session
Objective: To understand the JSP implicit object session.
Education & Research Department 8
© Infosys Technologies Limited
Introduction to J2EE Lab Guide Version 1.0
Background: JSP provides an implicit object called session that can be used for session
tracking. Data can be shared between JSPs invoked in the same session by setting the data as
an attribute of the session object.
Problem Description:
• Write a JSP program that accepts the name of the user as a parameter and prints the
message Welcome . This JSP should set the user name as an attribute of the
session object so that another JSP invoked by the same session can retrieve the user
name from the session object and print the message Hello .
Step 1: Create a file SessionTestJSP.jsp as show below and store it under the directory jsp
Session Test JSP
Welcome
Step 2: Create a file SessionTestJSPForm.html as show below and store it under the directory
myapp
Session Test JSP Form
User Name
Education & Research Department 9
© Infosys Technologies Limited
Introduction to J2EE Lab Guide Version 1.0
Step 3: Create a file ReadSessionTestJSP.jsp as show below and store it under the directory
jsp
Read Session Test JSP
Hello
Step 4: Start the Browser and supply the URL
http://localhost:8080/myapp/SessionTestJSPForm.html. The browser will display the following
screen.
Step 5: Type Infosys in the Text box and click Ok button. The following screen will be
displayed.
Education & Research Department 10
© Infosys Technologies Limited
Introduction to J2EE Lab Guide Version 1.0
Step 6: Open another browser window and type the URL
http://localhost:8080/myapp/jsp/ReadSessionTestJSP.jsp
Step 7: The session attribute, which is set earlier is retrieved and displayed as shown in the
following screen shot
Summary:
At the end of this assignment, you have learnt:
How to share data between JSPs invoked in the same session
Education & Research Department 11
© Infosys Technologies Limited
Introduction to J2EE Lab Guide Version 1.0
2.5 Assignment 5: Referring to the documentation of various implicit objects
Objective: To learn how to refer to the documentation to understand more about the implicit
objects in JSP
Background: The JSP provides a number of implicit objects such as request, response, out,
session and application. These objects are created from some classes whose details are
available in the documentation.
Problem Description:
• Learn how to refer to the documentation to understand more about the JSP implicit
objects
Step 1: Open the browser and type the URL http://localhost:8080. The home page of Tomcat
appears on the screen
Step 2: Click the link Tomcat Documentation under the heading Documentation. The
following screen appears
Education & Research Department 12
© Infosys Technologies Limited
Introduction to J2EE Lab Guide Version 1.0
Step 3: Scroll down and click the link Servlet/JSP Javadocs under the heading Application
Developers. The Javadoc appears. The following mapping can be used to explore further.
out – javax.servlet.jsp
response – javax.servlet.http.HttpServletResponse
request – javax.servlet.http.HttpServletRequest
session - javax.servlet.http.HttpSession
application – javax.servlet.ServletContext
Summary:
At the end of this assignment, you have learnt:
How to refer to the Javadoc to understand more about the implicit object
The important methods of the implicit objects
2.6 Assignment 6: Using jsp:forward tag
Objective: To understand jsp:forward tag
Background: The tag forwards the request to another resource from the same
web app. When the JSP page encounters tag, the current page stops processing
the request and forwards the request to another web component. This component completes
the response. Execution never returns to the calling page. Therefore tag action
must occur prior to writing any HTML tag to the output.
A JSP can process some data and forward the request to another JSP for further processing.
This enables modular design. Data can be shared between the forwarding JSP and forwarded
JSP by setting them as request attributes.
Problem Description:
• Write a JSP program that accepts user name and age. The JSP should calculate the
ticket charge based on age. The request should be forwarded to another JSP for
displaying
Step 1: Open notepad editor and write the following code in BusinessLogicJSPForm.html and
store the file under the folder myapp
Testing Forward Tag
Education & Research Department 13
© Infosys Technologies Limited
Introduction to J2EE Lab Guide Version 1.0
Name
Age
Step 2: Open notepad editor and write the following code in BusinessLogicJSP.jsp and save the
file under the folder jsp.
= 5 && age 10 && age
" />
Step 3: Open notepad editor and write the following code in PresentationJSP.jsp and save the
file under the folder jsp.
Education & Research Department 14
© Infosys Technologies Limited
Introduction to J2EE Lab Guide Version 1.0
Testing jsp:forward Tag
Name:
Age:
Amount:
BusinessLogicJSP.jsp and PresentationJSP.jsp are sharing the request object.fs
Step 2: Start the Browser and supply the URL
http://localhost:8080/myapp/BusinessLogicJSPForm.html. The browser will display the
following screen.
Step 4: Type the name as Infosys and age as 25. Click on the Ok button. The browser displays
the following screen.
Education & Research Department 15
© Infosys Technologies Limited
Introduction to J2EE Lab Guide Version 1.0
Summary:
At the end of this assignment, you have learnt:
How to use the forward tag
2.7 Assignment 7: Session Tracking
Objective: To learn how to do session tracking
Background: The various methods in the implicit object session can be used for session
tracking in a JSP.
Problem Description:
• Write a JSP application which can be a part of an online shopping application.
1) The user should first login using Login.html by supplying any user name. Use
JavaScript to ensure that the user enters the name before clicking the Login
button. On clicking the Login button of Login.html, the user name supplied by
the user should be submitted to a JSP, AddItems.jsp.
2) AddItems.jsp should have a dropdown list that displays the name of many
electronic items like Cell Phone, Laptop, MP3 Player and DVD Player. The user
should be able to select one item and click a button, Add to Cart. The item
selected by the user should be submitted to a JSP, AddToCart.jsp.
3) AddToCart.jsp should add the item to the shopping cart of the user and display
a success message. It should also display a link to the page AddItems.jsp. The
Education & Research Department 16
© Infosys Technologies Limited
Introduction to J2EE Lab Guide Version 1.0
user should be permitted to add any number of items by repeating the steps 2)
and 3)
4) AddItems.jsp and AddToCart.jsp should contain another link that will take the
user to ShowItems.jsp. ShowItems.jsp should display all the items selected by
the user so far. ShowItems.jsp should also display a link that will take the user
to AddItems.jsp
5) AddItems.jsp, AddToCart.jsp and AddItems.jsp should have a link that permits
the user to logout. When the user clicks on the link, a thanks message should
be displayed along with a link to Login.html that helps the user to login once
again
6) Once the user has logged off, even if the URL for AddItems.jsp, AddToCart.jsp
or AddItems.jsp is directly typed on the browser, the user should get only an
error message along with a link to Login.html
Hint: When the user logs in, submit the username to a JSP called Login.jsp. Login.jsp can set
the username as a session attribute and forward the request to AddItems.jsp. The JSPs
AddItems.jsp, AddToCart.jsp and AddItems.jsp can first check whether the session object
contains an attribute called username. In case it is present, the JSPs can display the
appropriate pages or else error messages with a link to Login.html.
Summary:
At the end of this assignment, you have learnt:
How to write a JSP application that uses session tracking
2.8 Assignment 8: Using jsp:include tag
Objective: To understand jsp:Include tag
Background: The tag includes the response of the included page into the
original page at runtime.
Problem Description:
• Write a JSP program that includes the output of another JSP program in it
Step 1: Open notepad editor and write the following code in the MyCompanyHome.jsp file.
Store it under the jsp directory
My Company's Home Page
Education & Research Department 17
© Infosys Technologies Limited
Introduction to J2EE Lab Guide Version 1.0
Welcome, our current stock price is
Step 2: Open notepad editor and write the following code in the StockPrice.jsp file. Store it
under the jsp directory
Step 3: Start the Browser and supply the following URL
http://localhost:8080/myapp/jsp/MyCompanyHome.jsp. The browser displays the following
screen.
Summary:
At the end of this assignment, you have learnt:
Education & Research Department 18
© Infosys Technologies Limited
Introduction to J2EE Lab Guide Version 1.0
2.9 Assignment 9: Using JavaBean in JSP
Objective: To learn how to use a JavaBean in a JSP
Background: JSP provides Standard Action elements for instantiating a JavaBean and to set
and get its properties.
Problem Description:
• Write a JSP to accept the details of an employee. Instantiate a JavaBean, Employee,
and store these data in this object. The JSP should also retrieve all these data from the
JavaBean and print them on the browser
Step 1: Open notepad editor and write the following code to create JavaBean called
Employee.java and store it under the directory myapp\WEB-INF\classes\mypackage
package mypackage;
public class Employee{
private String name;
private int employeeNo;
private String department;
private double salary;
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setEmployeeNo(int employeeNo){
this.employeeNo = employeeNo;
}
public int getEmployeeNo(){
return employeeNo;
}
public void setDepartment(String department){
this.department = department;
}
public String getDepartment(){
return department;
}
public void setSalary(double salary){
this.salary = salary;
}
public double getSalary(){
Education & Research Department 19
© Infosys Technologies Limited
Introduction to J2EE Lab Guide Version 1.0
return salary;
}
}
Step 2: Compile the above JavaBean to generate .class file and place it in the folder
mypackage
Step 3: Create a file EmployeeBeanTestJSP.jsp with the following code and store it under the
jsp directory
Testing Bean with JSP
Name :
Emp# :
Department :
Salary :
Step 4: Create a file EmployeeBeanTestJSPForm.html with the following code and store it
under the directory myapp
Testing Beans with JSP
Employee Name
Employee #
Department
Salary
Education & Research Department 20
© Infosys Technologies Limited
Introduction to J2EE Lab Guide Version 1.0
Step 5: Start the Browser and supply the URL
http://localhost:8080/myapp/EmployeeBeanTestJSPForm.html and supply the information as
shown in the following screen
Step 6: Click Ok button. The browser will display the following screen
Summary:
At the end of this assignment, you have learnt:
How to instantiate a JavaBean and set and get its properties in a JSP program
Education & Research Department 21
© Infosys Technologies Limited
Introduction to J2EE Lab Guide Version 1.0
2.10 Assignment 10: Understanding page directives
Objective: To understand JSP Page directives
Background: The JSP provides page directive element to give information about the page to
the container. The JSP page directive defines page-specific properties such as the content
type for this page’s response, whether this page should have the implicit session object. This
page directive includes various attributes such as import, session, errorPage, isErrorPage,
contentType etc
Problem Description:
• Create JSP that forwards the request to an error page whenever an error is
encountered. Also create the JSP that works as the error page. The error page should
display a proper error message
Step 1: Open notepad editor and write the following code in the ErrorPageTestJSP.jsp and
store it under the directory jsp.
Testing JSP Directives
The page directive used with the import attribute imports the java.io.FileReader class. The
errorPage attribute gives the container the URL of the web page to which the request is to be
forwarded in case an error occurs in the page. The JSP is trying to open a file that does not
exist. This results in an exception.
Step 2: Open notepad editor and write the following code in the ErrorPageJSP.jsp and store it
under the directory jsp as shown below.
Education & Research Department 22
© Infosys Technologies Limited
Introduction to J2EE Lab Guide Version 1.0
Error
The following error occurred
The isErrorPage attribute says whether the current page is an error page. The default is false.
Step 3: Start the browser and supply the URL
http://localhost:8080/myapp/jsp/ErrorPageTestJSP.jsp. The browser displays the following
Summary:
At the end of this assignment, you have learnt:
Various page directive attributes like import, errorPage and isErrorPage
Education & Research Department 23
© Infosys Technologies Limited
Introduction to J2EE Lab Guide Version 1.0
2.11 Assignment 11: Include Directive in JSP
Objective: To understand JSP include directive and its use
Background: The JSP provides include directive element to include an HTML or JSP page in
the response of another JSP.
Problem Description:
• Create JSP program to understand and use include directive
Step 1: Open notepad editor and write the following code in the Header.html file. Store the
Header.html file under the myapp directory
Harrison and Co Inc
Step 2: Create another file Footer.html with the following code and store it under the myapp
directory
H
Harrison and Co Inc
3880 K St. NW.Suite 3164 - Washington, DC 20006
phone 221-24224103, fax 221-24224104
Education & Research Department 24
© Infosys Technologies Limited
Introduction to J2EE Lab Guide Version 1.0
© Copyright 2005 Harrison and Co Inc.
write2us@harrison.com
Step 3: Create a file IncludeMe.jsp with the following code and store it under the jsp
directory
Step 4: Create a file IncludeTestJSP.jsp with the following code and store it under the jsp
directory
Include Test JSP
Education & Research Department 25
© Infosys Technologies Limited
Introduction to J2EE Lab Guide Version 1.0
Hello
This page has a static header and footer
The header and footer are inlcuded using the
include directive
");
%>
Step 5: Start the Browser and supply the URL
http://localhost:8080/myapp/jsp/IncludeTestJSP.jsp. The browser displays the following
screen.
Summary:
At the end of this assignment, you have learnt:
Education & Research Department 26
© Infosys Technologies Limited
Introduction to J2EE Lab Guide Version 1.0
How to use Include directive
2.12 Assignment 12: Working with JDBC
Objective: To create a JSP program that accesses a database
Background: A JSP can contain any Java code in tags. But according to the best
practices, one should try to minimize the amount of code that appears in a JSP. This is
because JSP is primarily used for creating the presentation logic and will be maintained by
web developers and not by programmers. While creating a JSP to access a database, it would
be better to include the JDBC code in a separate class. The JSP should just instantiate the
object and call the appropriate methods.
Problem Description:
• Create JSP application to print the name of all employees from the emp table of Oracle
Step 1: Open notepad editor and write the following code to create the program
EmployeeDB.java and store it under the directory myapp\WEB-INF\classes\mypackage
package mypackage;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.List;
import java.util.ArrayList;
public class EmployeeDB{
public List getEmployees() throws Exception{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection connection = DriverManager.getConnection ("URL", "UserName",
"Password");
Statement statement = connection.createStatement();
String query = "Select ename from EMP";
ResultSet resultSet = statement.executeQuery(query);
List list = new ArrayList();
while(resultSet.next()){
String name = resultSet.getString("ename");
list.add(name);
System.out.println(name);
}
connection.close();
return list;
Education & Research Department 27
© Infosys Technologies Limited
Introduction to J2EE Lab Guide Version 1.0
}
}
Step 2: Compile the above program to generate .class file and place it in the folder
mypackage
Step 3: Create a file JDBCBestPracticeJSP.jsp with the following code and store it under the
jsp directory
Employees
");
}
%>
Step 4: Start the Browser and supply the URL
http://localhost:8080/myapp/jsp/JDBCBestPracticeJSP.jsp
The list of all employees will be listed on the screen
Summary:
At the end of this assignment, you have learnt:
How to create a JSP application that accesses the database
How to separate the business logic from the presentation logic
2.13 Assignment 13: Shopping Cart
Objective: To create a JSP program that accesses a database
Background: While developing a JSP application, very frequently one will come across a
situation where the JSP has to access data from the database for the presentation logic.
Problem Description:
Education & Research Department 28
© Infosys Technologies Limited
Introduction to J2EE Lab Guide Version 1.0
• Create a table called Items with three columns – ItemNo (PK), Description and Price.
Modify AddItem.jsp in assignment 7 so that the JSP would read the names of the items
from the Items table and display them in the dropdown list.
Summary:
At the end of this assignment, you have learnt:
How to create a dropdown list in HTML and include data from a database table
Education & Research Department 29
© Infosys Technologies Limited