Servlets - Pooky/java-enterprise-examples GitHub Wiki

Servlets are simple java technology which allow handle, process or modify request and response with HTTP protocol.

Simply put, when your computer or any other want to load remote website, it will make request through network to server. On this remote server usually run some service or container, which act as Web Server. This Web Server does basically really simple thing. It listen on specific port, process incoming requests and usually return response. This request could be file, image, folder or something else.

Very nice description is also here: http://eriklievaart.com/blog/cgi2.html

In many times we want Web Server to generate some dynamic content. Like file with current date, greetings to logged person, shop order and others. To achieve this we need to use some programing language to tell Web Server what to do.

We have several options:

Server Language
Apache PHP, Perl, Python, Ruby, Rails
MS Server Asp.net
Tomcat 7 Java (Servlet, JSP)
NginX Only static files

In this case, Tomcat 7 is not really WebServer (or HTTP server) but Servlet and JSP Container which is fully complaint with Java Servlet specification. There is many versions, which have different features, each version of Tomcat supports different one.

Servlet example:

package my.app;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(
	asyncSupported = false, 
	name = "MainServlet",
	urlPatterns = {"/MainController"}
)
public class MainServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.getWriter().write("Hello world!");
	}

}

Links

Servlet history

VERSION DATE JAVA EE / JDK FEATURES / CHANGES
Servlet 4.0 September 2017 JavaEE 8 HTTP/2
Servlet 3.1 May 2013 JavaEE 7 Non-blocking I/O, HTTP protocol upgrade mechanism
Servlet 3.0 December 2009 JavaEE 6, JavaSE 6 Pluggability, Ease of development, Async Servlet, Security, File Uploading
Servlet 2.5 September 2005 JavaEE 5, JavaSE 5 Requires JavaSE 5, supports annotation
Servlet 2.4 November 2003 J2EE 1.4, J2SE 1.3 web.xml uses XML Schema
Servlet 2.3 August 2001 J2EE 1.3, J2SE 1.2 Addition of Filter
Servlet 2.2 August 1999 J2EE 1.2, J2SE 1.2 Becomes part of J2EE, introduced independent web applications in .war files
Servlet 2.1 November 1998 Unspecified First official specification, added RequestDispatcher, ServletContext
Servlet 2.0 JDK 1.1 Part of Java Servlet Development Kit 2.0
Servlet 1.0 June 1997