Java Servlet

From HaFrWiki
Revision as of 16:28, 22 August 2016 by Hjmf (talk | contribs) (Programming tools)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Java Servlets are the main entries for developing Java Web applications. But what is a servlet exactly. A short introduction created on several sources on the Internet [1].


What is a Java servlet?
Essentially, a servlet is a Java applet that's designed to run on a server, rather than on a web browser [2]. They're commonly use to extend the capabilities of a server.

For instance, a servlet might act as an interface to run a database query. It might hold and manage additional information alongside a stateless HTTP protocol. Or it might convert input from an HTML form into a format more useful to the server. In their function, servlets can be thought of as the Java counterparts to other technologies like PHP and ASP.NET.


The main model for a servlet is request-response.

  • Applications or users interact with the servlet by sending a request, perhaps for a certain piece of data.
  • The servlet then takes that request and translates it into a series of operations on the server itself.
  • The results from the server are translated back into a response that the user can readily understand.

In this sense then, a servlet is just a translator between the user interface and the server behind it. Technically speaking, an application is only a servlet if it implements the Java Servlet API, which defines the overall structure for the servlet family of classes.

This is contained within the Javax.servlet package hierarchy. Within that API, there are a number of more specific types of servlets that can be implemented. The default is the GenericServlet class, which has only the base methods. If you plan to use your servlet with HTTP protocols though, the HTTPServlet will provide additional methods like doGet and doPost that make writing your servlet much easier. The life cycle of a servlet has three main phases, all of which are controlled by the web container in which the servlet is deployed.

  1. In the first phase, the web container uses the Init method to load the servlet class, create an instance of it, and then initialize that instance.
  2. Once the servlet has been initialized, the container can send requests to the servlet using the Service method.
    • A single servlet can respond to any number of requests from its client, but each request must be serviced in its own thread.
  3. Once all desired requests have been serviced, the web container will call the Destroy method, thereby removing the servlet.

In sum, a generic servlet life cycle will consist of creation, a number of requests to be serviced, and then deletion.


What is JavaServer Faces (JSF) technology?
Whereas a servlet controls the back-end sequence of requests to and from a server, Faces define the user interface. In a sense, they act as the public face of the application. As you might expect, they're commonly used alongside servlets to help the end user efficiently understand and manipulate the server.
A typical workflow with a JavaServer Faces might look like this.

  1. First, we create a webpage.
  2. Then we associate that webpage with components via a set of tags.
  3. Then associate these components on that webpage to server-side data through a set of managed beans
  4. Then we can create a web deployment descriptor.

In addition, you can use application configuration resource files to configure custom components, beans, and page navigation rules. As with Java servlets, you don't necessarily have to reinvent the wheel every time you design a new application interface. The JavaServer Faces technology includes a programming model and libraries of some of the most common tags that you might use.

The prebuilt tags also have tag handlers to help implement them. As a result, common tasks like dropping components onto a webpage can be done very quickly and easily. On the other hand, if your needs don't exactly match any of the prebuilt tags, they can be customized and extended in a number of different ways.

The life cycle of a JavaServer Faces web application has two main phases.

  1. The first phase is called execute.
    • During this phase, the application view is built or refreshed.
    • Applying the parameters of the request.
    • The values for all of the components are recalculated based on the request.
    • Those values are applied to the managed beans and then the actual code of the application is run based on those values.
    • In essence, the execute phase consists of translating whatever input was provided into an understandable format and then running the application based on that input.
  2. The second phase of the web application is called the render phase.
    • Once the application has been run based on the provided inputs, the render phase simply consists of generating the new user interface based on those inputs.

The output is usually generated in HTML or XHTML, which is then displayed in the client's web browser. Basically, the render phase is just a reverse of the execute phase. It takes an output in machine-readable format and translates it into human-readable format. Altogether, a JavaServer Faces web application will take

  • human input,
  • translate it into a series of server requests,
  • run the requests,
  • and then translate the output back into something a human could understand.

Server programming

The canonical method of teaching programming is to assume that all parts of the program, including the data with which it interacts, are stored on the same machine. This makes it easy to understand data storage and retrieval. It also handily avoids issues with multiple access and other servers accessing things from over the internet. However, in the real world, many programs depend in some way on other machines.

Perhaps they're getting data from an external server, perhaps they're trying to write to or modify another machine in some way. Either way, programming on a server brings up two potential difficulties:

  • Latency [3]
    • Communication speed limited by the speed of light
    • Workload balancing
  • Rewrite conflicts.
    • Multiple clients can simultaneously access one servlet.
    • Dealing with concurrent read/write requests

These are complex issues and there's no one universal solution. Instead, the best solution will depend on your specification and those of your intended clients.

Programming tools

What do we need (Alphabetical sequence):

  • Apache Tomcat servlet container
  • IDE, Integrated Development Environments
  • Java EE, Java for Enterprise Environment Developers

Apache Tomcat



  •, Downloads eclipse (i.e. Luna, Mars, Neon d.d. 29 Aug 2015, for all platforms.
    Free and very good. The preferred one.
  •, Downloads IntelliJ
    Very good but may not be free
  •, Releases NetBeans.
    More equipped for PHP

Simple Editors

  • BBEdit, Mac Only
  • jEdit, all platforms native Java.
  • NotePad++, Windows
  • Sublime Text

See also


  •, Downloads i.e. the JEE environment (d.d. aug 2015 : Luna, Mars or Neon).




  1., What is a servlet.
  2. Look at the names Servlet and Applet, which references to the Server and the Application (Client).
  3. Latency: Luiheid (Dutch). Latency is the amount of time a message takes to traverse a system. In a computer network, it is an expression of how much time it takes for a packet of data to get from one designated point to another. It is sometimes measured as the time required for a packet to be returned to its sender. Basically, latency exists due to limitations imposed by the speed of light. Whereas the time it takes signals to travel within a machine is measured in nanoseconds, it takes a minimum of about 75 milliseconds for signals to travel half way around the world.