Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Presentation on java servlets
1. Department of Computer Science
Govt Post Graduate College Dargai Malakand
Presenting By: Aamir SohailPresenting By: Aamir Sohail
Subject: Advance OOP(Java)Subject: Advance OOP(Java)
Java ServletsJava Servlets
2. Page 2
What are the java servlets?
OR
Servlets are java-based server side programs
Java Servlets are programs that run on a Web server
and act as a middle layer between a requests coming
from a Web browser or other HTTP client and
databases or applications on the HTTP server.
??
3. Page 3
Server…Server…
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 client could be the browser or other software
making these requests
Typically, My little computer is the client, and
someone else’s big computer is the server
4. Page 4
Servlets…
Servlets are java-based server side programs
Servlets are Java technology’s answer to Common
Gateway Interface (CGI) programming.
They are programs that run on a Web server, acting
as a middle layer between a request coming from a
Web browser or other HTTP client and databases or
applications on the HTTP server.
5. Page 5
Servlets…Servlets…
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).
If we have this
Why we need
Servlets????
7. Page 7
CGI…
CGI stands for “Common Gateway Interface”
Client sends a request to server
Server starts a CGI script
Script computes a result for server
Another client sends a request
CGI Creates a new procces
Server returns response to client
8. Page 8
Servlets…Servlets…
Client sends a request to
server
Server starts a servlet
Servlet computes a result
for server and does not quit
Another client sends a
request
Server calls the servlet
again
Etc.
Server returns response to
client
9. Page 9
Servlets vs. CGI scripts
Advantages:
Running a servlet doesn’t require creating a separate
process each time
A servlet stays in memory, so it doesn’t have to be
reloaded each time
There is only one instance handling multiple requests,
not a separate instance for every request
10. Page 10
The Advantages of Servlets Over “Traditional” CGI
Java servlets are more efficient,
easier to use,
more powerful,
more portable,
safer,
and cheaper than traditional CGI and many alternative CGI-like
technologies
11. Page 11
With traditional CGI, a new process is started for each
HTTP request. If the CGI program itself is relatively
short, the overhead of starting the process can dominate
the execution time.
With servlets, the Java virtual machine stays running and
handles each request with a lightweight Java thread, not a
heavyweight operating system process
12. Page 12
Similarly, in traditional CGI, if there are N requests to
the same CGI program, the code for the CGI program
is loaded into memory N times.
With servlets, however, there would be N threads, but
only a single copy of the servlet class would be loaded.
This approach reduces server memory requirements and
saves time by instantiating fewer objects
13. Page 13
The Servlet Life Cycle
In Slide 10 and 11 (The Advantages of Servlets Over
“Traditional” CGI), I referred to the fact that only a
single instance of a servlet gets created, with each
user request resulting in a new thread that is handed
off to doGet or doPost as appropriate.
I’ll now explain how servlets are created and
destroyed, and how and when the various methods are
invoked. I give a quick summary here, then elaborate in
the following subsections.
14. Page 14
After this, each user request results in a thread that
calls the service method of the previously created
instance.
Multiple concurrent requests normally result in multiple
threads calling service
The service method then calls doGet, doPost, or
another method, depending on the type of HTTP
request it received.
When the servlet is first created, its init method is
invoked, so init is where you put one-time setup code.
15. Page 15
Finally, when the server decides to unload a
servlet, it first calls the servlet’s destroy
method.
16. Page 16
The init Method…
The init method is called when the servlet is first created;
it is not called again for each user request. So, it is used
for one-time initializations.
The servlet is normally created when a user first invokes a
URL corresponding to the servlet, but you can also specify
that the servlet be loaded when the server is first started
17. Page 17
The service Method
Each time the server receives a request for a servlet, the
server spawns a new thread and calls service.
The service method checks the HTTP request type (GET,
POST, PUT, DELETE, etc.) and calls doGet, doPost, doPut,
doDelete, etc
18. Page 18
Destroy Method
Called when server deletes servletinstance.
Not called after each request
19. Page 19
how to create servletshow to create servlets (Basic Structure)(Basic Structure)
20. Page 20
In the above program a basic servlet that handles
GET requests. GET requests, for those unfamiliar
with HTTP, are the usual type of browser requests
for Web pages.
A browser generates this request when the user
enters a URL on the address line, follows a link from
a Web page, or submits an HTML form that either
does not specify a METHOD or specifies
METHOD="GET".
21. Page 21
Servlets can also easily handle POST requests, which
are generated when someone submits an HTML form
that specifies METHOD="POST".
22. Page 22
Advantages of Servlets over other server side
technologies (ASP, CGI etc)
A servlet can be imagined to be as an applet
running on the server side. Some of the other
server side technologies available are Common
Gateway Interface (CGI), server side
JavaScript and Active Server Pages (ASP).
Advantages of servlets over these server
side technologies are as follows:
23. Page 23
Persistent
Servlets remain in memory until explicitly destroyed. This helps in
serving several incoming requests.
Servlets establishes connection only once with the database and can
handle several requests on the same database.
This reduces the time and resources required to establish connection
again
and again with the same database.
Whereas, CGI programs are removed from the memory once the request is
processed and each time a new process is initiated whenever new request
arrives.
24. Page 24
Portable
Since servlets are written in Java, they are portable.
That is, servlets are compatible with almost all operating systems.
The programs written on one operating system can be executed on
other operating system
25. Page 25
Server-independent
Servlets are compatible with any web server available today.
Most of the software vendors today support servlets within their
web server products.
On the other hand, some of the server side technologies like
server side JavaSricpt and ASP can run on only selected web
servers.
The CGI is compatible with the web server that has features to
supports it.
26. Page 26
Fast
Since servlets are compiled into bytecodes, they can
execute more quickly as compared to other scripting
languages. The bytecode compilation feature helps
servlets to give much better performance. In
addition, it also provides advantage of strong error
and type checking.
27. Page 27
How to run servlet?
There are Six Steps to Running Your First Servlet
Create a directory structure under Tomcat for
your application.
Write the servlet source code. You need to import
the javax.servlet package and the
javax.servlet.http package in your source file.
Compile your source code.
Create a deployment descriptor.
Run Tomcat.
Call your servlet from a web browser.
28. Page 28
Create a Directory Structure under Tomcat
When you install Tomcat, several subdirectories are
automatically created under the Tomcat home directory
(%TOMCAT_HOME%). One of the subdirectories is webapps.
The webapps directory is where you store your web applications.
A web application is a collection of servlets and other contents
installed under a specific subset of the server's URL
namespace.
A separate directory is dedicated for each servlet application.
Therefore, the first thing to do when you build a servlet
application is create an application directory
29. Page 29
Compile Your Source Code
For your servlet source code to compile, you need to include in
your CLASSPATH environment variable the path to the
servlet.jar file. The servlet.
jar is located in the commonlib subdirectory under
%CATALINA_HOME%.