Académique Documents
Professionnel Documents
Culture Documents
(Part-I)
Servers
A server is a computer that responds to requests from a client
Typical requests: provide a web page, upload or download
a file, send email
A server is also the software that responds to these requests;
a client could be the browser or other software making these
requests
Typically, our little computer is the client, and someone elses
big computer is the server
However, any computer can be a server
It is not unusual to have server software and client software
running on the same computer
Apache
Apache is a very popular server
66% of the web sites on the Internet use Apache
Apache is:
Full-featured and extensible
Efficient
Robust
Secure (at least, more secure than other servers)
client
client
Server
J2EE Architecture
What is a Servlet ?
Java Servlet
is a simple, consistent mechanism for extending the
functionality of a web server
Are precompiled Java programs that are executed on the server
side.
Require a Servlet container to run in
Servlets
A servlet is like an applet, but on the server side.
Unlike applets, servlets do not display GUI, its work is done
behind the scenes on the server and only the results of the
servlets processing are returned to the client.
Client sends a request to server
Client
Server
Client
Servlet
Advantages:
Efficient: Performance is better, as running a servlet doesnt require creating a separate
process each time. A servlet stays in memory, so it doesnt have to be reloaded each time.
There is only one instance handling multiple requests, not a separate instance for every
request.
Powerful: Java servlets let you easily do several things that are difficult or impossible with
regular CGI. Servlets can also share data among each other, making useful things like
database connection pools easy to implement.
Convenient. We already know Java. Why learn Perl too? Besides the convenience of
being able to use a familiar language, servlets have an extensive infrastructure for
automatically parsing and decoding HTML form data, reading and setting HTTP headers,
handling cookies, tracking sessions, and many other such utilities.
Portable. Servlets are written in Java and follow a well-standardized API.
Inexpensive: once you have a Web server, no matter the cost of that server, adding
servlet support to it (if it doesn't come preconfigured to support servlets) is generally free or
cheap.
Security: Java security manager on the server enforces a set of restrictions to protect
resources on a server machine.
Disadvantage:
Less choice of languages (CGI scripts can be in any language)
Complied Servlets are complied into byte-code, thus can execute more quickly
than common scripting languages. Servlet -side just-in-time compliers also
dramatically improve the performance of the JVM. Complied code is more compact
and secure than non-complied options.
Crash-resistant - Nothing is total crash proof, but as they are written in Java and
complied on JVM, they are more crash-resistant. JVM does not allow servlets direct
access to memory locations, thereby eliminating crashes that result from invalid
memory access.
Cross Server Servlets can run on virtually every popular server in use today.
Durable Servlets are durable objects. Durable means that they remain in the
memory until specifically instructed to be destroyed. Thus, servlets only need to be
instantiated a single time in order to service many requests.
Written in Java
Destruction
(Destroy method)
Servlet Class
Garbage Collection
1. init()
When a server loads a servlet, it runs the servlet's init() method.
Even though most servlets are run in multi-threaded servers, there are no
concurrency issues during servlet initialization.
This is because the server calls the init method once, when it loads the
servlet, and will not call it again unless it is reloading the servlet.
The server can not reload a servlet until after it has removed the servlet by
calling the destroy method.
Initialization is allowed to complete before client requests are handled (that
is, before the service method is called) or the servlet is destroyed.
When a servlet is first started up, its init (ServletConfig config) method is
called
init should perform any necessary initializations
init is called only once, and does not need to be thread-safe
2. service()
The service() method is the heart of the servlet.
Each request message from a client results in a single call to the servlet's
service() method.
The service() method determines the kind of request and calls the
appropriate method (doGet() or doPost()) for handling the request and
sends response to the client using the methods of the response object.
Every servlet request results in a call to
service(ServletRequest request, ServletResponse response)
service calls another method depending on the type of service
requested
Usually you would override the called methods of interest, not service
itself
service handles multiple simultaneous requests, so it and the methods
it calls must be thread safe.
3. destroy()
The method is run once; the server will not run it again until after it reloads
and reinitializes the servlet.
Tomcat
Tomcat is the Servlet Engine than handles servlet
requests for Apache
Tomcat is a helper application for Apache
Its best to think of Tomcat as a servlet container