Entry
Java: Servlet: What are servlets? [applet / application]
May 25th, 2006 15:22
Knud van Eeden,
----------------------------------------------------------------------
--- Knud van Eeden --- 07 September 2003 - 01:56 am ------------------
Java: Servlet: What are servlets? [applet / application]
---
Description:
A Java program is usually one of 3 different types:
-An applet
-- or --
-A servlet
-- or --
-An application
---
An applet is for use on the client side in a browser.
---
A servlet is similar to an applet but for use on a computer server.
---
An application is any other type of program.
---
The main difference between an applet and a servlet is thus where they
are running and on which type of computer they are running. The applet
runs thus typically on a client computer, and the servlet runs
typically on the server computer (where many users could access it
(simultaneously)) where also a special type of web server must be
installed. The application could run on both client computer and server
computer.
---
The main difference between an applet and a servlet, respectively an
application is that the output of the applet and the servlet is seen in
the web browser window, where the output of the application is
typically seen in a console window or on the screen outside the web
browser. The applet and the servlet have typically no or restricted
access to the (local) computer resources (e.g. file system) and run
more in a sandbox, where the application typically has more access.
And the applet and servlet do not contain a 'main' method, but the
application does. And the applet and the servlet do not need the
program 'java.exe' to run, where the application does.
---
The main similarities between an applet and a servlet is that they both
are subclasses of a certain parent class (they EXTEND from a certain
(parent) class (respectively 'Applet' and 'HttpServlet')), and contain
no 'main' class, and both show their output in the web browser window.
---
The main similarities between an applet, a servlet and an application
is that they all contain their corresponding Java source code and are
compiled using e.g. 'javac.exe' (which is installed when you install
the 'JDK'), which gives as an output a .class file. And a Java Virtual
Machine needs to be installed (this follows also with the installation
of the JDK on that computer) on each computer (be it client or server
computer) they are running, in order to interpret this .class file
to processor dependent machine code.
---
To see the analogies between this types of program better, and to get a
holistic overview via induction:
In the special case that all 3 types of programs should be existing
simultaneously, then a typical scenario could be:
1. -Your Java applet program would be running on your (sometimes on)
'Microsoft Windows XP' client computer
1. -E.g.
If not already present, possibly download and install the
'Sun Java JDK', create a 'HelloWorldApplet.java' file
containing no 'main' class and extending the parent Applet
class (here called 'Applet'), save that files in any file
directory on that computer, compile it with 'javac.exe' to a
'HelloWorldApplet.class' file, open your browser (e.g.
'Microsoft Internet Explorer'), create a .htm file which
contains at least the tag <APPLET code="path to your
HelloWorldApplet.class">, load that .htm file in your
browser, then your applet runs via that web browser and shows
it output on the client computer screen (e.g. as some text
and or pictures and or other multimedia information)
2. -Your Java servlet program would be running on your (always on)
'Microsoft NT' server computer or (always on) 'Microsoft 2000
Server' computer or (always on) 'Microsoft 2003' server computer
1. -E.g.
If not already present, possibly download and install the
'Sun Java JDK' (or otherwise Sun J2EE' or 'Sun Java JSDK')
create a 'HelloWorldServlet.java' file containing no 'main'
class and extending the parent Servlet class (here called
'HttpServlet'). Install and configure a servlet aware web
server (e.g. you download, install and configure (via the
'server.xml' and 'web.xml' configuration files (similar to
the 'httpd.conf' configuration file on Apache)) the 'TomCat'
web server (which is a variation of the 'Apache' web server)
software program on your server computer. This web server
will start to listen continuously on a certain port (e.g.
8080 by default)). Compile your servlet .java program with
'javac.exe' to a 'HelloWorldServlet.class' file. Store this
'HelloWorldServlet.class' file in a very specific directory
on your server computer (e.g the directory where that web
server looks for files, e.g. in the directory '..\tomcat' in
subdirectory 'common' in the subdirectory 'classes' (similar
to the Apache 'htdocs' directory). Make sure that web server
is running (e.g. starting it via a batch file or as a
service). Then you contact that (e.g. 'TomCat') web server
(e.g. on port 8080) by opening a web browser (e.g. 'Microsoft
Internet Explorer' (running on the same server computer for
testing purposes, or otherwise on any client computer
connected via the network to that server computer)). As a URL
you type in the address field of that web browser in general
typically:
http://<the IP address of the computer on which that web
server runs>:<the port on which that web server listens/servlet/<the
web server subdirectory in which the servlet is stored>/<the filename
of your servlet (without the .class at the end)>
E.g.
http://myMicrosoftWindows2003ServerIpAddress:8080/servlet/HelloWorldSer
vlet
This web browser sends it to the output port on the client
computer. The client computer sends it to that web server
(e.g. via the network (e.g. the Internet)). The web server
sends that URL and other information sent with it to it to
your .class file in that (e.g. '\tomcat\common\classes\')
directory on that server computer. Your .class file
interprets it possibly further, processes the corresponding
actions according to its source code (e.g. contacts databases
and gets processed information back), and possibly sends an
output back (e.g. as HTML source code text) to that web
server, which sends it back (e.g. via the network (e.g. the
Internet)) to the client computer, which sends it to that web
browser there. There you see that (HTML) output on your
computer screen (e.g. as some text and or pictures and or
other multimedia information)
3. -Your Java application program would be running on your (sometimes
on) 'Microsoft Windows XP' client computer or 'Microsoft Windows
2003' server computer
1. -E.g.
If not already present, possibly download and install the
'Sun Java JDK', create a 'HelloWorldApplication.java' file
containing a 'main' class, store in in any file directory on
that computer, compile it with javac.exe to a
HelloWorldApplication.class file, run this class file by
running java.exe with the parameter 'path to your
HelloWorldApplication'. Then you typically see some output on
your computer screen (usually outside the web browser, thus
in an MSDOS console or on the Microsoft Windows screen). E.g.
as some text and or pictures and or other multimedia
information).
===
Description:
A servlet is a facility which you usually use to write custom Internet
web servers, though other more general uses are equally possible.
[book: author = Niemeyer, Patrick / Knudsen, Jonathan - title =
Learning Java - publisher = O'Reilly - ISBN 1565922719]
===
Description:
---
Servlets are small programs that execute on the server side of a Web
connection.
---
Servlets are Java programs that execute in a Web container and are the
equivalent of CGI scripts.
---
Just as applets dynamically extend the functionality of a Web browser,
servlets dynamically extend the functionality of a Web server.
---
The Java Servlet development Kit (JSDK) contains the class libraries
that you will need to create servlets.
---
[book: source: Schildt, Herbert - Java 2 / The complete reference (3th
edition) - ISBN 0-07-211976-4 - p. 900 'Servlets']
===
Description:
---
Servlet are little Java programs and are in some way, like applets:
- they are not self-standing applications and do not have a main()
method
- they are not called by the user or programmer, but by another
application (the Web server)
- they have a life cycle that includes the init() and destroy() stages
---
Servlets are Java classes whose role is to connect a Web server (or
several Web servers) to other computational resources and entities.
---
A servlet is not a self-standing program, it springs into action when
1. a request addressed to it arrives at the server.
2. At that time, the servlet engine calls its custom designed class
loader.
3. The class loader checks to see whether
-the servlet class is already loaded and
-whether the loaded version has the same time stamp as the .class
file. (if finds the .class file in the specially designated
directory).
-If the servlet class is not loaded or is older than the disk file,
the engine loads the class into the special Java Virtual Machine
provided for that purpose and creates an instance of it.
---
Three entities are thus involved in managing the servlet:
1. The Web server
2. The servlet engine (perhaps bundled with the Web server)
3. The Java Virtual Machine (JVM) that runs the servlet
---
[book: source: Nakhimovsky, Alexander / Myers, Tom - professional Java
XML programming - ISBN 1-861002-850-8 - p. 14 'the client side']
===
Description:
---
A servlet is a class in Java that implements the Servlet interface. It
is an application running in memory, like your own little server.
Lightning fast and scalable.
---
It gets information via POST and GET, similar as in a HTML <FORM> and
does something.
---
Better than Perl (different maintainable), or C++. Takes more
resources, not cross platform.
---
Servlets are Java applications that reside on the server side of an
HTTP server.
---
Your Web server (if enabled for Java servlets) simply maps a request
onto a servlet, passing it the entire URL call.
---
The servlet then does what it is programmed to do (e.g. connecting to a
database using JDBC via an SQL query) and generates dynamic content
(e.g. the output of the database stored in Java variables, which it
prints out to an HTML page).
---
Because servlets are implemented in Java, they are platform independent
and architecture neutral.
---
As with normal Java objects, servlets require a valid Java Virtual
Machine to be present on the machine on which it runs.
---
In addition, the servlet requires a valid Java Web Server that is
capable of routing the requests properly.
---
[book: source: Sridharan, Prashant - advanced Java networking - ISBN 0-
13-749136-0 - p. 238]
===
Internet: see also:
---
Language: Computer: Java: Servlet: Link: Overview: Can you give an
overview of links?
http://www.faqts.com/knowledge_base/view.phtml/aid/40576/fid/778
----------------------------------------------------------------------