Java is a robust, general-purpose, high-level programming language and a powerful software platform. It is also object-oriented, distributed, portable and multi-threaded. Java follows the ‘Write – once – run – anywhere’ approach. All Java programs must run on the Java platform that has two components, the Java Virtual Machine (JVM) and the Java Application Programming Interface (API).
With Java API, many types of Java programs can be developed. These include
Java stand-alone applications: The Java stand-alone applications are the programs written in Java to carry out certain tasks. These applications run directly by the Java interpreter. A standalone application should be delivered and installed on each computer before it is run. It can have either a Command-Line Interface (CLI) or Graphical User Interface (GUI). You have already used a number of stand-alone applications such as text editors, word processors and games applications.
We’ll be covering the following topics in this tutorial:
Java Applets
An applet is a small Java program that is embedded in an HTML page and is executed by using a Java-enabled web browser(E.g. Netscape and Hot Java) or a tool such as an applet viewer. An applet is embedded in a HTML page using the <applet> or <object> tag and hosted on a web server. When a user accesses an HTML page that contains an embedded applet, the applet code is downloaded to the web browser (client) along with the requested webpage and is eventually executed there under the control of the Java Virtual Machine (JVM) installed in the web browser.
Since Java’s bytecode is platform independent, Java applets can be executed by browsers for many platforms such as Windows, Unix, Linux, and MacOS, etc. Applets generally used to provide some interactive features to web applications that cannot be provided by HTML such as:
•Displaying dynamic web pages of a web application. For example, A web page displaying data relating to examination results, reservation status of an Airline application, share prices, etc.
• Playing sound files.
• Displaying documents.
• Playing animations.
Java Servlets
Java servlets provide web developers with a simple, consistent mechanism for extending the functionality of a web server and provide dynamic behavior for web applications. Servlets are Java classes that function like CGI programs. They accept a request from a client (usually a web browser), process that request and return a response to the client. All servlets are loaded and executed by a servlet container that can run by itself or as a component of a web server. The servlet container is also referred to as a servlet engine in the early days of servlet technology.
When your web server (like Apache) gets a request for a servlet from the client, the server hands over the request not to the servlet itself, but to the servlet container in which servlet deployed. The servlet container then directs the request to the appropriate servlet. The servlet does its processing which may include interacting with the database or other server-side components such as servlets or JSPs (Java Server Pages). After the servlet processes the request, the response (generally in the form of HTML Document) returned to the servlet container which in turn sends the response back to the client via the web server. It should note that a servlet is loaded by the servlet container, the first time the servlet requested. Afterward, the servlet stays in memory waiting for other requests. Each request dispatched by the clients may be served simultaneously inside the same process, and typically the servlet is unloaded only when the servlet container is shut down.
Servlets generally used for
• Processing and storing data submitted using HTML form by the user such as purchase order or credit card data.
• Providing dynamic contents (e.g., returning the results of a database query to the client).
• Allowing collaboration between people. A servlet can handle multiple requests concurrently, i.e., they can synchronize requests to support systems such as online conferencing.
• Forwarding requests to other servers and servlets. It allows them to be used to balance the load among several servers that mirror the same contents.
• Managing state information on the top of the stateless HTTP, e.g., for an online shopping cart system which manages shopping carts from many concurrent customers and maps every request to the right customer.