web component development with servlets

61
A Seminar Training Report On Web Component Development With Servlets & JSP TechnologiesIn partial fulfillment of requirements for the degree of Bachelor of Technology In Computer Science 1

Upload: padmesh-soni

Post on 05-Mar-2015

121 views

Category:

Documents


2 download

DESCRIPTION

this is report of Summer Training of B. Tech. program scheduled

TRANSCRIPT

Page 1: Web Component Development With Servlets

A Seminar Training Report On

“Web Component Development With

Servlets & JSP Technologies”

In partial fulfillment of requirements for the degree of

Bachelor of Technology In

Computer Science

Department of Computer Science

Submitted To: - Submitted By:-Mr. Yogesh Kumar (H.O.D) Padmesh Soni CS/08/55

APEX INSTITUTE OF ENGINEERING & TECHNOLOGY, SITAPURA, JAIPUR RAJASTHAN TECHNICAL UNIVERSITY

2011

1

Page 2: Web Component Development With Servlets

Acknowledgements

I take this opportunity to express my profound sense of gratitude and respect to all those who

helped me throughout the duration of this project. I acknowledge the effort of those who have

contributed significantly to my project. I express my sincere gratitude and thankfulness towards

our H.O.D Er. Yogesh Kumar (AIET), for his valuable time and guidance.

I feel privileged to offer my sincere thanks and deep sense of gratitude to Mr. Arun Gupta at

NIIT Pvt. Ltd. for expressing their confidence in me by letting me work on a project of this

magnitude and using latest technologies and providing their support, help & encouragement in

implementing this project.

I am grateful to all our friends for providing critical feedback & support whenever required.

There are times in such projects when the clock beats you time & again & you run out of energy,

you just want to finish it once & forever. Parents made us endure such times with their unfailing

humour & warm wishes.

I regret any inadvertent omissions.

Padmesh Soni

2

Page 3: Web Component Development With Servlets

PREFACE

In today’s competitive world, computers have assumed critical importance in every field. It has

become an essential qualification for almost all ventures. In my Bachelor degree Of Computer

Science I have been prepared to be at part with the industrial requirements. So, the industrial

training is a must and the project work undertaken during this training exhibits the practical as

well as the theoretical knowledge of the student.

As part of my industrial training I undertook a project in Adv. JAVA, MY SQL at NIIT Private

Limited, Jaipur. While doing this project I learned a lot and brushed up my earlier knowledge.

This training has provided me an opportunity to have practical, which has increased my sphere of

knowledge to a great extent. Now I am better equipped to handle the real things than anyone else

who has not undergone any such training does.

During my stay here I learned how an actual Web Site progresses, what sort of problems actually

occurs during the development of such websites.

3

Page 4: Web Component Development With Servlets

LIST OF CONTENTS

Contents Page No

Front page…………….……………………………………........................ 1

Acknowledgement………………………...….…………………………... 2

Preface……………………………………………………........................... 3

List of Contents………………………………………………………......... 4

Company Profile…………………………………………………………...... 6

Introduction to Web Application Technologies ………………………....…. 8

Developing a View Component …………………………………….…....... 15

Developing a Controller Component……………………………………….. 19

Developing Dynamic Forms………………………………………………… 21

Sharing Application Resources Using the Servlet Context ………………….22

Designing the Business Tier …………………………………………………24

4

Page 5: Web Component Development With Servlets

Developing Web Applications Using Struts ……………………………… 26

Developing Web Applications Using Session Management ……………... 29

Using Filters in Web Applications ………………………………………... 34

Integrating Web Applications with Databases...………………………....... 37

Developing JSP Pages………………………………………………........... 39

Introduction to Project……………………………………………………… 40

Interface Required………………………………………………………. …. 41

Snap Shots of Project……………………………………………………….. 42

Bibliography………………………………………………………………… 48

COMPANY PROFILE

5

Page 6: Web Component Development With Servlets

Introduction: 

NIIT is a part of the HCL group which has a big presence in the Indian InfoTech industry. The Indian computer education industry is now worth around Rs. 6.5 bn. NIIT has pioneered the computer education business in India by creating a nation-wide network of training schools. There is a strong marketing focus with the company tapping uncharted overseas markets to expand the training business with the help of local business partners. Courses have been designed for special segments like computer illiterate mothers / senior citizens. NIIT has around 650 centers, out of which around 50 are owned. NIIT had a head-start of more than 2 years over Aptech. 

The management has a big emphasis on quality. All the aspects of NIIT’s business have ISO 9000 certifications. The company is now busy implementing Crosby’s Complete Quality Management System at all its locations to provide high-quality, zero-defect service to all its customers. 

NIIT is the first company in India to adapt the Economic Value Added (EVA) concept as a measure of corporate performance. Around four hundred front-line managers at NIIT have been trained in EVA to extract maximum resource productivity at their respective business areas. 

6

Page 7: Web Component Development With Servlets

Major line of Business: 

It is one of the larger computer & software training organizations

Select Products and Services: 

Computer Training: through franchises 

Software Services (Educational Multimedia): NIIT is the largest educational multimedia developer in the country. It started educational software development in 1991 with a contract for IBM and now has impressive clients in educational multimedia, namely AT & T - GIS, Chase Manhattan, IBM Thailand, NETG, Oracle, etc. Key Differentiating Factors: 

NIIT is one of the leading players in the computer training industry in India. NIIT has moved to be a developer of computer-based training tools and is now also a software services provider 

Location:

1. Introduction to Web Application Technologies

7

Address:

NIIT Pvt. Ltd.Vaishali Nagar Centre, Office No. 2,3 & 4, 3rd Floor, Unique Aspire Building, Cosmo Colony, Amrapali Marg, Viashali Nagar, JaipurRajasthan, India

Phone No. 91-141- 4086300

E-mail.

[email protected]

Page 8: Web Component Development With Servlets

Web Application Technologies:

The HTTP (Hyper Text Transfer Protocol) is similar to many other Internet protocols, for example SMTP and FTP because HTTP is a protocol to transfer data from the server to client. One fundamental difference between most other Internet protocols and HTTP is that the typical Internet protocol supports several requests per connection. HTTP supports only one request per connection.

There is no need for the server to hold the connection open, therefore the server does not need to manage the connection’s state. HTTP was created in conjunction with the related HTML standard. HTML is a document display language that lets users link from one document to another.The combination of the HTTP protocol and the HTML page description language constitutes the foundational technologies of the World Wide Web.

HTTP Client-Server Architecture:

Fig: HTTP Client-Server Architecture

For every exchange over the web using HTTP, there is a request and a response. The web browser sends a single request to the server. The web server determines which file is being requested and sends the data in that file back as the response. The browser interprets the response and represents the content on the screen.

Web Site Structure:

8

Page 9: Web Component Development With Servlets

A web site is a collection of HTML pages and other media files that contains all the content that is visible to the user on a given web server.

The index.html file is a special file used when the user requests a Uniform Resource Locator (URL) that ends in a slash character (/). The web server presents the user with a directory listing for that URL unless an index.html file exists in that directory. If that is the case, then the web server sends the index.html file as the response to the original URL.

Uniform Resource Locator:

A URL is canonical name that locates a specific resource on the Internet. It consists of:

protocol://host:port/path/file

For example:

http://www.soccer.org:80/league/Spring2001.html

If the port no. is the standard port for the given protocol, then that no. can be ignored in the URL.

Java Platform, Enterprise Edition 5 (Java EE 5):

Java EE is the industry standard for developing portable, robust, scalable and secure server-side. Java technology applications. It is built on the solid foundation of Java Platform, Standard Edition (Java SE).

9

Page 10: Web Component Development With Servlets

Execution of CGI Programs:Early in the development of HTML, the designers created a mechanism to permit a user to invoke a program on the web server. This mechanism was called the Common Gateway Interface (CGI).When a web site includes CGI processing, this is called web application.

Fig: Running a Single Instance of a CGI Program

Fig: Running Multiple Instances of a CGI Program

10

Page 11: Web Component Development With Servlets

Execution of Java Servlets:

Fig: Running a Single Instance of a Servlet

Fig: Running Multiple Instances of Servlet

A Java servlet is a Java technology program that, similar to a CGI program, runs on the server. The types of tasks that you can run with servlets are similar to those you can run with CGI. However, the underlying executing architecture is different.The basic processing steps for Java servlets are quite similar to the steps for CGI. However, the servlet runs as a thread in the web container instead of in a separate OS process. The web container itself is an OS process, but it runs as a service and is available continuously.

Java Servlets:

Servlets run within the Java EE component container architecture. This container is called the web container is a Java Virtual Machine (JVM) tool interface that supplies an implementation of the servlet API. Servlet instances are components that are managed by the web container to respond to HTTP requests.

11

Page 12: Web Component Development With Servlets

Fig: Web Server Architecture with JAVA Servlets

Java Server Pages Technology:

JSP pages are just one way of implementing the concept of HTML pages with embedded code, or template pages. Three examples of other popular technologies available for creating HTML with embedded code are PHP, ASP and Ruby on Rails.

JSP pages are converted by the web container into a servlet instance. That servlet then processes each request to that JSP page

.

Fig: Processing of JSP page

12

Page 13: Web Component Development With Servlets

Web Application-Three-Tier Architecture:

Model-View-Controller (MVC) Architecture in a Web Application:

Three-tier architecture and MVC architecture are not the same. They are not even related concepts. Three-tier architecture and MVC architecture are completely separate (orthogonal) concepts. Even the topology of each is different: three-tier is a linear (layered) architecture; Model-View-Controller is triangular. A fundamental concept of three-tier architecture is that all communication passes through the middle ware tier. In MVC architecture the controller communicates with the model (and in some implementation with the view also), and the view obtains its date directory from the model.

13

Page 14: Web Component Development With Servlets

Model 2 Architecture:

Fig: Deployment Diagram of a web container using Model 2 architecture A Servlet acts as the controller, which verifies form data, updates the model with the

form data, and selects the next view as the response. A JSP page acts as the view. This renders the HTML response, retrieving data from the

model necessary to generate the response, and provides HTML forms to permit user interaction.

Java technology classes act as the model, which implements the business logic of the web application.

Web Application Migration:

Fig: Application Design Matrix

14

Page 15: Web Component Development With Servlets

2. Developing a View Component

There are four fundamental types of views: Data Presentation

This includes any data presented on visible screen. There are many forms of presentation including graphs, spreadsheets, listings, and so on.

Data FormsForms for data entry are also considered view components.

Navigational aidsThis type of view includes menus, hyperlink, site maps and so on.

Informational screens or pop-upsThis type of view includes welcome text, instructions, help screen, error messages, confirmation dialogs and so on.

HTTP Revisited:

HTTP GET Method:

A browser issues an HTTP GET request when: The user selects a link in the current HTML page The user enters a URL in the location field or the address field.

HTTP Request:

15

Page 16: Web Component Development With Servlets

Example Headers:

HTTP Response:

16

Header Use

Accept The MIME types the client can receive

Host The internet host and port number of the resource being requested

Referer The address from which the Request-Universal Resource Identifier (URI) was obtained

User-Agent The information about the client originating the request

Page 17: Web Component Development With Servlets

Example Headers:

Request & Response Headers:

17

Header Use

Content-Type

A MIME type (such as text/html) which classifies the type of data in the response

Content-Length

The length (in bytes) of the payload of the response

Server An informational string about the server that responded to this HTTP request

Cache-Control

A directive for the web browser (or proxies) to indicate whether or not the content of the response should be cached

Page 18: Web Component Development With Servlets

Sequence Diagram of an HTTP GET Request:

Configuring a Servlet Definition:

18

Page 19: Web Component Development With Servlets

3. Developing a Controller Servlet

HTTP GET & POST Methods:

Developing a Controller Servlet:

19

A form-processing (controller) servlet needs to:Retrieve form parameters from the HTTP request.Perform any data conversion on the form parameters.Verify the form parameters.Execute the business logic.Dispatch to the next view component based on the results of the previous steps.

The HTTP GET method is used when:The processing of the request is idempotent.The amount of form data is small.You want to allow the request to be bookmarked.

The HTTP POST method is used when:The processing of the request changes the state of the server, such as storing data in a database.The amount of form data is large.The contents of the data should not be visible in the URL (for example, passwords).

Page 20: Web Component Development With Servlets

Servlet API Retrieve Form Parameter:

20

Page 21: Web Component Development With Servlets

4. Developing Dynamic Forms

Servlet Life Cycle:

The ServletConfig API:

Note: It is used when developing a user detail in online submission forms like as registrations.

21

Page 22: Web Component Development With Servlets

5. Sharing Application Resources Using the Servlet Context

Servlet Context:

A web application is a self contained collection of static and dynamic resources. The web application deployment descriptor is used to specify the structure and services

used by a web application. The Servlet Context object is the runtime representation of the web application.

Servlet Context API:

Web Application Life Cycle:

When the web container is started, each web application is initialized. When the web container is shut down, each web application is destroyed. A Servlet Context listener can be used to receive these web application life cycle events.

22

Page 23: Web Component Development With Servlets

The Servlet Context Listener API:

23

Page 24: Web Component Development With Servlets

6. Designing the Business Tier

Describing the Analysis Model: An analysis model is used to bridge the gap between use case analysis and component

design. An analysis model consists of three abstract component types:

Component Symbol Description

Boundary Communicates between the user and the system.

Service Provides a services-oriented layer between boundary and entity components.

Entity Represents domain objects and persistent data.

Designing a Service Component:

Service components add a service layer between boundary and entity components. This section describes two fundamentals types of services: entity-related services & facades.

Entity Service:

Some entity-related operations cannot be performed by the entity component itself: Creation- Creating a new instance of the entity. Retrieval- Retrieving a unique instance in the data store. Selection- Retrieving a set of instances in the data store. Aggregation- Performing a calculation (such as an average) over a set of instances. Deletion- Removing an instance from the data store.

Façade Service:

24

Page 25: Web Component Development With Servlets

A Façade service might be used to reduce coupling between boundary components and other services:

High Coupling Low Coupling

25

Page 26: Web Component Development With Servlets

7. Developing Web Applications Using Struts

Struts Framework:

Struts is an open source framework that is a subproject of the Jakarta project under the Apache Software Foundation. The Struts framework also satisfies the MVC pattern by using so called action classes to implement the controller components. A Struts application use only one infrastructure servlet, called the ActionServlet.

Front Controller Pattern:

The Front Controller pattern solves two major problems:

1. Decouple view and controller componentsYou have separated the code of your views and controllers, but the controller servlets still need to know the URL path to the view component. Struts introduces a level of indirection in which the view is specified by a symbolic name that is mapped to the view path in the Struts configuration file.

2. Abstract any common infrastructure processing from the application-specific

controllers.

Struts MVC Framework:

Framework provides following elements: Infrastructure servlet controller Base classes Configuration files

Why use a framework like Struts: Provides flexible, extensible infrastructure for MVC Lets you focus on what is important to your application, such as:

Application Controllers Model Components views

Struts Activity Diagram:

26

Page 27: Web Component Development With Servlets

Struts Action Class:

27

Page 28: Web Component Development With Servlets

You need to do following:

Configuring the Struts infrastructure controller. Configure a servlet mapping for struts controller. Configure the action mappings. Install the struts library files.

28

Page 29: Web Component Development With Servlets

8. Developing Web Applications Using Session Management

HTTP and Session Management:

HTTP is a stateless protocol. Each request and response message connection is independent of all others. This is significant because from one request to another (from the same user) the HTTP server does not maintain a reference to the previous request. Therefore, the web container must create a mechanism to store session information for a particular user.

The web container can keep a session object for each user:

Designing Web Applications to Use Session Management:

The session management module can be design with three main steps:

1. Design multiple, interacting views for a use case.2. Create a Struts action application controller for each activity in the use case.3. Create a unique Struts URL for each activity in the use case.

Using Session Management in a Web Application:

Using Session Management: Each activity-specific action must store attributes (name/object pairs) that are used by

other requests within the session. Any action can access any attributes that has already been set by processing a previous

request. At the end of session, the action might destroy the session object.

29

Page 30: Web Component Development With Servlets

Session API:

Storing Session Attributes:

Only one session object is created for a given client within a single web application. However, several use cases in the same web application can share the session object. Therefore, you should create attribute names that avoid ambiguity within the complete web application.

Accessing Session Attributes:

Anyone can use the getAttribute() method to retrieve data in the session object.

Destroying the Session:

30

Page 31: Web Component Development With Servlets

Using Cookies for Session Management: Cookies are sent in a response from the web server. Cookies are stored on the client’s computer. Cookies are stored in a partition assigned to the web server’s domain name. Cookies can

be further partitioned by a path within the domain. All Cookies for that domain (and path) are sent in every request to that web server. Cookies have a lifespan and are flushed by the client browser at the end of that lifespan.

Cookies API:

Performing Session Management Using Cookies:

Fig: Web Container Session ID Cookie Storage

31

Page 32: Web Component Development With Servlets

Fig: Web Container Session ID Cookie Retrieval

Using URL-Rewriting for Session Management:

Fig: URL-Rewriting Session Management

32

URL-rewriting is used when cookies cannot be used.The server appends extra data on the end of each URL.The server associates that identifier with data it has stored about that session.With this URL:http://host/path/file;jsessionid=123 session information is jsessionid=123.

Page 33: Web Component Development With Servlets

URL-Rewriting Implications:

URL-rewriting is used when cookies cannot be used. The server appends extra data on the end of each URL. The server associates that identifier with data it has stored about that session. With this URL:

o http://host/path/file;jsessionid=123 session information is jsessionid=123.

Every HTML page that participates in a session (using URL-rewriting) must include the session ID in all URLs in those pages. This requires dynamic generation.

Use the encodeURL method on the response object to guarantee that the URLs include the session ID information.

33

Page 34: Web Component Development With Servlets

9. Using Filters in Web Applications

Web Container Request Cycle:

Request processing by the web container Applying filters to an incoming request Applying filters to a dispatched request

Web Container Request Processing: Request and response objects are created for each incoming request.

Fig: Request and Response Applying Filters to Incoming Request:

A filter intercepts the request before it gets to the requested resource. A response is returned to the client through the filter.

34

Page 35: Web Component Development With Servlets

Filters can be used for many activities in a web application, such as:o Blocking access to a resource based on user identity or role membershipo Auditing incoming requestso Compressing the response data streamo Transforming the responseo Measuring and logging servlet performance

Filters applied to a Dispatch: Filters can be applied to an internal dispatch, such as a request forward or include.

Filters API:

35

Page 36: Web Component Development With Servlets

The init method is called once when the filter instance is first created. Use the init method to:

o Perform one-time initialization of resources the filter uses over its lifetimeo Retrieve the initialization parameters configured in the deployment descriptor

The doFilter method is the filter equivalent of a servlet’s service method. As a developer, you implement the doFilter method to do the following:

o Perform the operations you want to occur every time the filter is invoked.o Decide whether to pass the request to the next component in the filter chain or

halt the request entirely.To pass on the request, call the doFilter method on the FilterChain reference.

The destroy method is the last method called in the life cycle of a filter instance. Use the destroy method to clean up any resources allocated in the init method.

Configuring the Filter:

Typically, filters are applied to requests from a client. You can specify the dispatcher element in a filter mapping. This determines what type (or types) of requests invoke the filter. Valid values are:

o REQUEST – The filter is applied if the request is from a client.o INCLUDE – The filter is applied if the request is from a request dispatcher

include.o FORWARD – The filter is applied if the request is from a request dispatcher

forward.o ERROR – The filter is applied if the request is a result of an error condition.

36

Page 37: Web Component Development With Servlets

10. Integrating Web Applications with Databases

Designing a Web Application:

Design the domain objects of your application. Design the database tables that map to the domain objects. Design the business services (the model) to separate the database code into classes using the data access object (DAO) pattern.

Data Access Object (DAO) Pattern:

The data access object (DAO) pattern separates the business logic from the data access (data storage) logic. The data access implementation (usually JDBC technology calls) is encapsulated in DAO classes. The DAO pattern permits the business logic and the data access logic to change independently. For example, if the DB schema changes, then you would only need to change the DAO methods, and not the business services or the domain objects.

JDBC API:

The JDBCTM API is the Java technology API for interacting with a relational DBMS. The JDBC API includes interfaces that manage connections to the DBMS, statements to perform operations, and result sets that encapsulate the result of retrieval operations. Techniques are described for designing and developing a web application, in which the JDBC technology code is encapsulated using the DAO design pattern. An incorrect technique is to create a connection object for each request, but this approach is extremely slow and does not scale well.

37

Page 38: Web Component Development With Servlets

Traditional Approaches to a Database Connection:

Use DriverManager.getConnection to create database connections with every request. Create a connection and store it as a member variable of the servlet. Use a connection pool to recycle connections. Can use servlet context to store the connection pool:

o A custom connection pool might present maintenance problems.o Servlet context is not available to business tier components (such as DAOs).

Java EE application servers provide a namespace, which can be accessed using JNDI APIs.

Java EE application servers must support storing DataSource resources in JNDI namespace.

DataSource is an object which encapsulates the information to connect to the database:o Database URLo Drivero User name and password

Most servers provide a database connection pool that is accessed using the DataSource.

DataSource API:

38

Page 39: Web Component Development With Servlets

11. Developing JSP Pages

Java Server Pages Technology:

JSP pages are just one way of implementing the concept of HTML pages with embedded code, or template pages. Three examples of other popular technologies available for creating HTML with embedded code are PHP, ASP and Ruby on Rails.

JSP pages are converted by the web container into a servlet instance. That servlet then processes each request to that JSP page

Steps of JSP Processing:

Processing of JSP:

39

Page 40: Web Component Development With Servlets

12. Introduction to Project

Overview:

DVD Library Management is a report of recent occurrences, information of DVD’s that has

lately taken type.

DVD Library Management is the management of DVD’s which is presented by admin these

are Padmesh and Jack with additional users.

The world is changing at a very fast rate and so as they ways to access different medium. This

project pattern has only three features which are describe by me the whole 11 modules of this

report.

In my summer training period I have done a course in J2EE and worked on a simple project of a

DVD Library Management website.

DVD Library Management is a module of simple three features in which admin and additional

users are add their DVDs according to genre and show them with their preferences of title, year,

genre.

Purpose

The purpose of my project is only that from this I can understand the processing of Servlets with

their additional features.

40

Page 41: Web Component Development With Servlets

Interface Required

Platform Used : - Window XP

Language Used: - HTML, J2EE (Servlets)

Database Used : - MY SQL

Software Used : - Netbeans 7, Glassfish and Apache Servers.

Hardware Requirements:

Processor : - 456 MHz Or Above

Ram : - 1 GB And Above

TOOLS:

Front-End :- J2EE

Back-End : - MY SQL

41

Page 42: Web Component Development With Servlets

Modules of the Project & Their Description

The Project has number of modules, which perform specified task. Following are the

name and description of the modules of the project.

Authentication

Required :

This Module Accepts the user name and searches in the

data-directory if the information is valid then programs

continue otherwise programs access by the admin

authentication.

Home: This module have 4 parts these are Display my DVD

Library, Add a new DVD, Set User Pref and last one is

Home also.

Display DVDs: This module is used for display all DVDs.

Add DVD: This module is used for add a new DVD in the file of

DVDs.

Preferences: This module is used for select the preferences of DVDs

to show them.

Success: This module is used for show that DVD has been added

successfully.

Add DVD Form: This module is used for add DVD form.

Set Preferences: This module is used for html form of preferences.

Snapshots of the Project

42

Page 43: Web Component Development With Servlets

Fig: LoginFilter Authentication

43

Page 44: Web Component Development With Servlets

Fig: Home Page

Fig: Add DVD Page

44

Page 45: Web Component Development With Servlets

Fig: Set User Preferences Page

Fig: Success Page

45

Page 46: Web Component Development With Servlets

Fig: List Shown Page

Fig: Error Shown Page

46

Page 47: Web Component Development With Servlets

Bibliography

www.google.com

www.w3schools.com

www.java.com

http://struts.apache.org/

47