![]() |
The Java term for CGI application is Servlet, as mentioned above that CGI application is different from plain |
| HTML document. To write and save data on the server you cannot rely on web server alone. |
![]() |
Web server have the ability to deliver static HTML pages only. But it cannot create real time pages that respond |
| to client request. What you need is a helper application. Which is called CGI or Servlet in Java world. |
![]() |
Servlet is a Java program that runs in a Web server, as opposed to an applet that runs in a client browser. |
| Typically, the servlet takes an HTTP request from a browser, generates dynamic content (such as by querying a | |
| database), and provides an HTTP response back to the browser. |
![]() |
Alternatively, it can be accessed directly from another application component or send its output to another |
| component. Most servlets generate HTML text, but a servlet might instead generate XML to encapsulate data | |
| (Wright and Smith 2005). |
![]() |
Servlet programming also offers advantages over earlier models of server-side Web application development, |
| including the following as stated by (Wright and Smith 2005): |
![]() |
Servlets outperform earlier technologies for generating dynamic HTML, such as server-side "includes" or | |
| CGI scripts. Once a servlet is loaded into memory, it can run on a single lightweight thread; CGI scripts must | ||
| be loaded in a different process for every request. |
![]() |
Servlet technology, in addition to improved scalability, offers the well-known Java advantages of object | |
| orientation, platform independence, security, and robustness. |
![]() |
Servlets are fully integrated with the Java language and its standard APIs, such as JDBC for Java database | |
| connectivity. |
![]() |
Servlets are fully integrated into the J2EE framework, which provides an extensive set of services that your | |
| Web application can use, such as Java Naming and Directory Interface (JNDI) for component naming and | ||
| lookup, Java Transaction API (JTA) for managing transactions, Java Authentication and Authorization | ||
| Service (JAAS) for security, Remote Method Invocation (RMI) for distributed applications, and Java Message | ||
| Service (JMS). |
![]() |
A servlet handles concurrent requests (through either a single servlet instance or multiple servlet instances, | |
| depending on the thread model), and servlets have a well-defined lifecycle. Also, servlets can optionally be | ||
| loaded when OC4J starts, so that any initialization is handled in advance instead of at the first user request. |
![]() |
The servlet request and response objects provide a convenient way to handle HTTP requests and send text | |
| and data back to the client. |
![]() |
Because servlets are written in the Java programming language, they are supported on any platform that | |
| has a Java virtual machine and a Web server that supports servlets. Servlets can be used on different | ||
| platforms without recompiling. You can package servlets together with associated files such as graphics, | ||
| sounds, and other data to make a complete Web application. This simplifies application development and | ||
| deployment. |
![]() |
An Example |
![]() |
Suppose that we want the current time of server to be shown up on web page, you cannot do this with static | |
| HTML page, you need something more dynamic, you need servlet: |

![]() |
In order to run this example you need to do other things (e.g. configure something called the deployment | |
| descriptor (DD)). This is not our focus now we just want to show you how simple servlet looks like. |
![]() |
In the previous figure, Lines from 4 to 7 are standard servlet declarations. We will talk about that in coming | |
| topics. Don't worry about them now. |
![]() |
Lines from 8 to 13 are the core, we use PrintWriterobject which we get from response object to write HTML | |
| tags to the response object, and through HTML tags we have put the current date of server. |
![]() |
It should display: |

![]() |
This is how you create a dynamic web page in a servlet. You have to print the whole thing to an output | |
| stream (it's really part of the HTTP response stream that you're printing to). But trying to format the HTML | ||
| inside a servlet out.println() is much sophisticated, this is one of the worst parts about servlets. If only there | ||
| was a way to put Java inside an HTML page instead of putting HTML inside a Java class, yes there is, are | ||
| you heard about JSP? (Bates, Sierra and Basham 2008). |