Noor Java

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Servlets provide a component-based, platform-independent method for building Web

based applications, without the performance limitations of CGI programs. Servlets have
access to the entire family of Java APIs, including the JDBC API to access enterprise
databases. This tutorial will teach you how to use Java Servlets to develop your web
based applications in simple and easy steps.

Why to Learn Servlet?


Servlet is a simple java program that runs on server and capable of handling request
and generating dynamic response.
Using Servlets, you can collect input from users through web page forms, present
records from a database or another source, and create web pages dynamically.
Java Servlets often serve the same purpose as programs implemented using the
Common Gateway Interface (CGI). But Servlets offer several advantages in comparison
with the CGI.
• Performance is significantly better.
• Servlets execute within the address space of a Web server. It is not
necessary to create a separate process to handle each client request.
• Servlets are platform-independent because they are written in Java.
• Java security manager on the server enforces a set of restrictions to protect
the resources on a server machine. So servlets are trusted.
• The full functionality of the Java class libraries is available to a servlet. It
can communicate with applets, databases, or other software via the
sockets and RMI mechanisms that you have seen already.

Applications of Servlet
• Read the explicit data sent by the clients (browsers). This includes an
HTML form on a Web page or it could also come from an applet or a custom
HTTP client program.
• Read the implicit HTTP request data sent by the clients (browsers). This
includes cookies, media types and compression schemes the browser
understands, and so forth.
• Process the data and generate the results. This process may require talking
to a database, executing an RMI or CORBA call, invoking a Web service, or
computing the response directly.
• Send the explicit data (i.e., the document) to the clients (browsers). This
document can be sent in a variety of formats, including text (HTML or XML),
binary (GIF images), Excel, etc.
• Send the implicit HTTP response to the clients (browsers). This includes
telling the browsers or other clients what type of document is being
returned (e.g., HTML), setting cookies and caching parameters, and other
such tasks.
Audience
This tutorial is designed for Java programmers with a need to understand the Java
Servlets framework and its APIs. After completing this tutorial you will find yourself at
a moderate level of expertise in using Java Servlets from where you can take yourself to
next levels.

Prerequisites
We assume you have good understanding of the Java programming language. It will be
great if you have a basic understanding of web application and how internet works.

Client Site Application runs on the browser


Server Site Application runs on the web server. (Servlet)

XML (Extensible Markup Language) is a very popular simple text-based language that can be
used as a mode of communication between different applications. It is considered as a
standard means to transport and store data. JAVA provides excellent support and a rich set of
libraries to parse, modify or inquire XML documents. This tutorial will teach you basic XML
concepts and the usage of various types of Java based XML parsers in a simple and intuitive
way.
XML is a simple text-based language which was designed to store and transport data
in plain text format. It stands for Extensible Markup Language. Following are some of
the salient features of XML.
• XML is a markup language.
• XML is a tag based language like HTML.
• XML tags are not predefined like HTML.
• You can define your own tags which is why it is called extensible language.
• XML tags are designed to be self-descriptive.
• XML is W3C Recommendation for data storage and data transfer.

Advantages
Following are the advantages that XML provides −
• Technology agnostic − Being plain text, XML is technology independent. It
can be used by any technology for data storage and data transfer purpose.
• Human readable − XML uses simple text format. It is human readable and
understandable.
• Extensible − In XML, custom tags can be created and used very easily.
• Allow Validation − Using XSD, DTD and XML structures can be validated
easily.

The Model-View-Controller (MVC) is a well-known design pattern


in the web development field. It is way to organize our code. It specifies that a program
or application shall consist of data model, presentation information and control
information. The MVC pattern needs all these components to be separated as different
objects.
The model designs based on the MVC architecture follow MVC design pattern. The application
logic is separated from the user interface while designing the software using model designs

o Model: It represents the business layer of application. It is an object to carry the


data that can also contain the logic to update controller if data is changed.
o View: It represents the presentation layer of application. It is used to visualize the
data that the model contains.
o Controller: It works on both the model and view. It is used to manage the flow of
application, i.e. data flow in the model object and to update the view whenever
data is changed.

In Java Programming, the Model contains the simple Java classes

, the View used to display the data and the Controller contains the servlets
. Due to this separation the user requests are processed as follows:

1. A client (browser) sends a request to the controller on the server side, for a page.
2. The controller then calls the model. It gathers the requested data.
3. Then the controller transfers the data retrieved to the view layer.
4. Now the result is sent back to the browser (client) by the view.

Advantages of MVC Architecture


The advantages of MVC architecture are as follows:
o MVC has the feature of scalability that in turn helps the growth of application.
o The components are easy to maintain because there is less dependency.
o A model can be reused by multiple views that provides reusability of code.
o The developers can work with the three layers (Model, View, and Controller)
simultaneously.
o Using MVC, the application becomes more understandable.
o Using MVC, each layer is maintained separately therefore we do not require to deal with
massive code.
o The extending and testing of application is easier.

JSP technology is used to create web application just like Servlet technology. It can be
thought of as an extension to Servlet because it provides more functionality than servlet
such as expression language, JSTL, etc.

A JSP page consists of HTML tags and JSP tags. The JSP pages are easier to maintain than
Servlet because we can separate designing and development. It provides some additional
features such as Expression Language, Custom Tags, etc.

Advantages of JSP over Servlet


There are many advantages of JSP over the Servlet. They are as follows:

1) Extension to Servlet

JSP technology is the extension to Servlet technology. We can use all the features of the
Servlet in JSP. In addition to, we can use implicit objects, predefined tags, expression
language and Custom tags in JSP, that makes JSP development easy.
2) Easy to maintain

JSP can be easily managed because we can easily separate our business logic with
presentation logic. In Servlet technology, we mix our business logic with the presentation
logic.

3) Fast Development: No need to recompile and redeploy

If JSP page is modified, we don't need to recompile and redeploy the project. The Servlet
code needs to be updated and recompiled if we have to change the look and feel of the
application.

4) Less code than Servlet

In JSP, we can use many tags such as action tags, JSTL, custom tags, etc. that reduces the
code. Moreover, we can use EL, implicit objects, etc.

The Lifecycle of a JSP Page


The JSP pages follow these phases:

o Translation of JSP Page


o Compilation of JSP Page
o Classloading (the classloader loads class file)
o Instantiation (Object of the Generated Servlet is created).
o Initialization ( the container invokes jspInit() method).
o Request processing ( the container invokes _jspService() method).
o Destroy ( the container invokes jspDestroy() method).

Note: jspInit(), _jspService() and jspDestroy() are the life cycle methods of JSP.

You might also like