Anda di halaman 1dari 13

Mohammad Ali Jinnah University Islamabad

Department of Computer Science,


Faculty of Computing

Lab Manual for Enterprise Application Development


Lab 03: Servlet Configuration using web.xml

Lab 03: Servlet Configuration using web.xml

Table of Contents
1.

Introduction

2.

Activity Time boxing

3.

Objective of the experiment

4.

Concept Map
4.1 Servlet Life Cycle:
4.2 Starting Servlets
4.2.1 HTTP Methods
4.3 Servicing a Servlet

3
3
4
4
5

5.

Homework Task before Lab

6.

Procedure and Tools


6.1 Tools
6.2 Setting up to use Oracle
6.3 Setting up to use NetBeans IDE 7.0.1 or latest
6.3.1 Load Database
6.3.2 Adding Tomcat Server
6.3.3 Create new Web Application
6.3.4 Adding MYSQL/Oracle JDBC Connector Library
6.4 Walkthrough Tasks
6.4.1 HelloServlet
6.4.2 Servlet Configuration Using web.xml
6.4.3 Accessing initialization parameters in Servlet
6.5 Getting the Parameters Values

5
5
6
7
7
7
7
7
7
7
8
8
9

7.

Practice Tasks
7.1 Practice Task 1

11
11

8.

Evaluation Tasks (unseen)

12

9.

Evaluation Criteria

13

10.

Further Reading

13

Page | 2

Lab 03: Servlet Configuration using web.xml

Lab 3: Servlet Configuration using web.xml


1. Introduction
A servlet is a Java programming language class that is used to extend the capabilities of servers
that host applications accessed by means of a request-response programming model. Although
servlets can respond to any type of request, they are commonly used to extend the applications
hosted by web servers. For such applications, Java Servlet technology defines HTTP-specific
servlet classes.
The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing
servlets. All servlets must implement the Servlet interface, which defines life-cycle methods.
When implementing a generic service, you can use or extend the Generic Servlet class provided
with the Java Servlet API. The Http Servlet class provides methods, such as doGet and doPost, for
handling HTTP-specific services.
Relevant Lecture Reading
Lecture Slides 4-5
2. Activity Time boxing
Table 1: Activity Time Boxing
Task
Activity Name
No.
6.2
Setting up to use MYSQL
6.3
Setting up to use NetBeans
IDE 7.0.1
6.4
Walkthrough Tasks
7
Practice Tasks
8

Evaluation Task (Unseen)

Activity time

Total Time

10 min
10 min

10 min
10 min

70 min
70 min
As per time specified for 40 min
each task
25 min for each assigned 50 min
task

3. Objective of the experiment

Building Servlets for use on the World Wide Web


Configuring Servlets using web.xml
Request dispatching: Servlet to Servlet communication
Application context and communication with the container via a Servlet.

4. Concept Map
4.1 Servlet Life Cycle:
The life cycle of a servlet is controlled by the container in which the servlet has been deployed.
Page | 3

Lab 03: Servlet Configuration using web.xml


When a request is mapped to a servlet, the container performs the following steps.
1. If an instance of the servlet does not exist, the web container
a. Loads the servlet class.
b. Creates an instance of the servlet class.
c. Initializes the servlet instance by calling the init method.
2. Invokes the service method, passing request and response objects.
If the container needs to remove the servlet, it finalizes the servlet by calling the servlets
destroy method.
4.2 Starting Servlets
Servlets init(ServletConfig) or init() methods are Called when the servlet is called for the first
time from a client. It is a code for initialization that is called only once (e.g., creating the tables
of the database) Initialization parameters are server specific. The configuration parameters are
taken from the file web.xml that is under tomcat_home/dir_path/WEB-INF/
4.2.1 HTTP Methods

POST:
Data sent in two steps
Designed for Posting information
Browser contacts server
Sends data
GET:
Contacts server and sends data in single step
Appends data to action URL separated by question mark
Designed to get information

Page | 4

Lab 03: Servlet Configuration using web.xml


4.3 Servicing a Servlet
Every call to the servlet creates a new thread that calls the service method. The service methods
check the type of request (GET, POST, PUT, DELETE, TRACE, OPTION) and call the appropriate
method: doGet, doPost, doPut, doDelete, DoTrace, doOption. It is recommended to implement
doPost and doGet instead of implementing service. Why? The method doPost can call doGet in
order to reuse code.
5. Homework Task before Lab
Review Class Lectures.
6. Procedure and Tools
In this section we describe procedures and tools that you required to complete this lab.
6.1 Tools

Microsoft Windows XP ,Windows 7, or latest


Internet Browser (Internet Explorer, Mozilla Firefox or Google Chrome etc.)
NetBeans IDE 7.0.1 or latest
MySQL/Oracle/Postgess

Page | 5

Lab 03: Servlet Configuration using web.xml

6.2 Setting up to use Oracle


[Expected Time: 10 min]
(If Oracle is not installed / or working, Use Alternate database MYSQL or Postgress)
1. Configure database Using Database configuration in Program file>oracle 10g
2. Run SqlPlus follow steps as shown in Figure below.

2
3
4

3. Type quit to close Sqlplus.


4. Reopen Sqlplus and login using username and password created.
5. Create table using query as follows:
SQL> create table user _login (username varchar2 (30), password varchar2 (40));
6. Type commit.
7. Use insert Query to insert values in database table.
SQL> insert into user login values ('abc', 'xyz');
8. Type commit.
9. To view data inserted in database table use select query as follows.
Page | 6

Lab 03: Servlet Configuration using web.xml


SQL> select * from user_login;
USERNAME

PASSWORD

------------------------- -----------------------abc

xyz

6.3 Setting up to use NetBeans IDE 7.0.1 or latest


As in previous lab
6.3.1 Load Database
As in previous lab
6.3.2 Adding Tomcat Server
As in previous lab
6.3.3 Create new Web Application
As in previous lab
6.3.4 Adding MYSQL/Oracle JDBC Connector Library
1.
2.
3.
4.

Right click on Libraries>Add Library


Click Import Library
Scroll to MySQL Driver
Add.

Adding Oracle JDBC Connector Library


1. Right click on Libraries>Add JAR/Folder.
2. Browse to location of driver and Add.
6.4 Walkthrough Tasks

[Expected Time: 20 min]

This section is designed such a way that you can complete the following tasks independently.
However if there is any ambiguity you can refer it to the lab instructor.
6.4.1
1.
2.
3.
4.
5.

HelloServlet
Right click on Source Packages>New>Servlet.
Type servlet name HelloServlet.
Type Package name com.servlets.HelloServlet. Click next
Check Add information to Deployment Descriptor (web.xml).
Click Finish.
Page | 7

Lab 03: Servlet Configuration using web.xml


6.4.2 Servlet Configuration Using web.xml
1. Open web.xml. Located in webpages>WEB-INF
2. Notice the information added of new Servlet Created.

3. Within servlet tag we can add initialization parameter information. Lets add two
Parameters University and Website. Shown as below:

4. Each Parameter has two tags: its name and its value. Throughout the application we can
access the parameter value by calling its name.
5. Save the changes (Ctrl+s) and close web.xml
6.4.3 Accessing initialization parameters in Servlet
1. Open HelloServlet.
Page | 8

Lab 03: Servlet Configuration using web.xml


2. Since any information retrieved from web.xml will be in String format therefore we Create
two class members of string type i.e. uniName and uniUrl.
3. Create an init(ServletConfig) method and call each individual parameter by using
config.getInitParameter (parameterName ).
4. Display the retrieved information in web browser using processRequest method.

5. Right click on Servlet and select Run File.

6.5 Getting the Parameters Values


1. Create new Servlet ColorExample. Add following code.
Page | 9

Lab 03: Servlet Configuration using web.xml

2. Create another Servlet setColors.


3. Add following information.

4. Run ColorExample.

Page | 10

Lab 03: Servlet Configuration using web.xml

7. Practice Tasks
This section will provide more practice exercises which you need to finish during the lab. You
need to finish these tasks in the required time. Once you have completed your tasks kindly place
them in the folder provided by your lab instructor.
7.1 Practice Task 1
[Expected Time: 50 min]
Create a servlet that establishes a connection using init parameters to define the details of the
connection.
1. Pass the Retrieved information to establishConnection().
Page | 11

Lab 03: Servlet Configuration using web.xml


2. Once connection is established.
3. Create "Registration" class consisting of following mentioned field name as class
members.
First name
Gender
Last Name
Skills
Father Name
Hobbie
Profession
4. Generate setters and getters.
5. Create a registration form.
First name
Last Name
Father Name
Profession (option to Select 1 from list of all professions)
Gender
Skills
(option to select 5 from list of 10)
Hobbies (option to select no more than 8 of 20)
6. Prompt user to provide required information
7. Display information when submitted.
8. Provide user friendly interface to manage data. i.e. User should be able to
a. Update
b. Delete
c. Filter based on
i. Profession
ii. Gender
8. Evaluation Tasks (unseen)
Unseen tasks will be given at the time of the lab

[Expected Time: 50 min]

Page | 12

Lab 03: Servlet Configuration using web.xml

9. Evaluation Criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each
task is assigned the marks percentage which will be evaluated by the instructor in the lab whether
the student has finished the complete/partial task(s).
Table 2: Evaluation of the Lab
Sr. No
1
2

Task No.
5
6.2

6.3

3
4

6.4
8

Task Description
Homework
Setting up to use
MySQL
Setting up to use
NetBeans IDE
Walk Through Tasks
Evaluation
Tasks
(unseen)
Total

Grade
5
10
10
30
45
100

10. Further Reading


HTML W3 Tutorial
http://www.w3schools.com/html/
HTML code Tutorial
http://www.htmlcodetutorial.com/
CSS Basic
http://www.cssbasics.com

Page | 13

Anda mungkin juga menyukai