Switch branches/tags
Nothing to show
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Examples for Java Servlets

After we have learned about sockets and HTML, we are able to develop nice web pages and we can also send data over the internet using TCP. It may occur to us that a web server is actually just sending HTML and stuff over the net, using sockets. The web browser then receives the data and displays it. Of course, they cannot just use TCP for communication, as the web browser must somehow tell the web server which page or which image (hosted on that server) it wants and the web server needs to say whether the request was successful or not and so on. For this, the hypertext transfer protocol exist. This protocol is sent via TCP and it is text-based. With the stuff we have learned, we can fully implement it. Actually, we already have done some partial, rudimentary implementations of it in the sockets examples set.

Still, doing this by hand is not just cumbersome, we will probably make many mistakes. What we would like to have is an API for HTTP as comfortable as sockets are for TCP and UDP. Java Servlets are this kind of thing. A servlet is a sub-class of a special Java class which implements handler methods for different HTTP interactions ("HTTP methods"). These methods are called by a servlet container, the actual implementation of the server, which then uses sockets, thread pools, and also takes care of the character encoding for us. We can therefore fully concentrate on the application logic and don't need to worry about the protocol interaction itself.

We can create forms with HTML whose contents are sent to the web server (or servlet, in our case). This means that we can send information from a browser to our servlets. This information is provided by the servlet container to our servlet. Our servlet may process, store, and use this information for its purposes. It may dynamically create contents as response. This means that we can now build dynamic web applications. We can build applications that run in a web browser and communicate with our server components and which use Javascript in the browser to create the feeling of "local applications" for the user.

There are many different implementations for java servlet containers. Here we use GlassFish and Jetty. Other notable containers are Tomcat and JBoss.

Long story short, here you can find my Java Servlets examples.

  1. deployable examples
  2. HTTP Proxy Servlet


This work has purely educational purposes. Besides everything mentioned below, for anything in this repository and projects, I impose one additional licensing condition: The code must never be used for anything which might violate the laws of Germany, China, or the USA. This also holds for any other file or resource provided here.

The examples in this repository are licensed under the GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007, with the exception of everything in the directories /javaServerPages/standAloneJSPsWithJetty and /javaServlets/proxy, which re licensed under the Apache License v2.0 and are partially derived from project embedded-jetty-jsp with copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.