Building Web Apps in Java:
Beginning & Intermediate Servlet & JSP Tutorials

Interested in training from the author of these tutorials? See the upcoming public J2EE training courses in Maryland (co-sponsored by Johns Hopkins Engineering for Professionals, or contact for info on customized courses at your location.

Following is an extensive series of tutorials on servlets and JSP, aimed at developers that already know Java but who have little or no experience with servlets and JSP. Since each section includes exercises and exercise solutions, this can also be viewed as a self-paced JSP and servlet training course. All the slides, source code, exercises, and exercise solutions are free for unrestricted use. Click on a section below to expand its content. The relatively few parts on IDE development and deployment use Eclipse, but of course none of the actual code is Eclipse-specific. Also, although servlets and JSP are very widely used, new projects should seriously consider the JSF 2 framework as a higher-level alternative to servlets and JSP.

Overview of the Servlet and JSP Tutorial Series

The beginning and intermediate-level tutorials on this page were originally based on the second edition of Marty's book Core Servlets and JavaServer Pages, but have been extensively upgraded since then, including coverage of servlets 3.0 and JSP 2.2 (e.g., for Tomcat 7). The materials have been tested by Marty in live training courses in Canada, Australia, Puerto Rico, Japan, Cambodia, Mexico, India, Norway, the Philippines, and dozens of US venues. Click on a topic below to get the detailed tutorial, download the source code, or get the exercises on the topic. Practice is the key to learning, so if you serious about learning the technology, you should do at least some of the exercises in each topic before moving on to the next topic. These tutorials assume that you already know Java; they definitely move too fast for those without prior Java experience. If you don't already know the Java language, please see the Java programming tutorial series.

Also see the advanced servlet and JSP tutorials for topics like filters, Web app security, listeners, custom tag libraries, JDBC, and use of the Spring Framework in Web applications. In addition, the training materials home page has tutorials on many other Java-related topics, including Ajax (with jQuery), one of the recommended next steps after learning servlets and JSP. Also, as stated previously, for new projects, JSF 2 should be seriously considered as a higher-level alternative to servlets and JSP. A few JSF2 topics like session tracking and cookie handling use the underlying servlet API, but for the most part, it is not necessary to learn servlets and JSP first: you can dive straight into JSF 2. See the JSF 2 tutorial series for details.

To arrange a customized course at your organization on servlets and JSP, JSF 2, Ajax, Android programming, or another Java EE topic, contact These courses can be customized to use any combination of the materials on the J2EE tutorials site, and new materials can be added for specific client needs. To learn more details about the instructor, the curriculum for the live training courses, or the public course schedule, please see

If you find these free tutorials helpful, we would appreciate it if you would link to us. Send corrections or feedback on any tutorial to You can also add Marty on LinkedIn.

Servlets & JSP: Overview and Setup

If you don't already know how to use a Java-enabled server (such as Tomcat) with an IDE (such as Eclipse), please see the Tomcat and Eclipse setup tutorial. Covers both Tomcat 6 (for servlets 2.5 and 2.5) and Tomcat 7 (for servlets 3.0). Tomcat 7 recommended.

Servlet Basics

Again, if you don't already know how to use a Java-enabled server (such as Tomcat) with an IDE (such as Eclipse), please see the Tomcat and Eclipse setup tutorial.

Handling the Client Request: Form Data

Handling the Client Request: HTTP Request Headers

Generating the Server Response: HTTP Status Codes

Generating the Server Response: HTTP Response Headers

Handling Cookies

  • Tutorial section in PDF (best for printing and saving).
  • Tutorial section on SlideShare (preferred by some for online viewing).
  • Topics covered:
    • Understanding the benefits and drawbacks of cookies
    • Sending outgoing cookies
    • Receiving incoming cookies
    • Tracking repeat visitors
    • Specifying cookie attributes
    • Differentiating between session cookies and persistent cookies
    • Simplifying cookie usage with utility classes
    • Modifying cookie values
    • Remembering user preferences
  • Source code of examples in this section. This is the exact code shown in the tutorial, and it runs only on Tomcat 7 or another server that supports servlets 3.0. Bundled as an Eclipse project.
  • Slightly modified source code for examples in this section. This code has a few small changes so that it runs on Tomcat 6 or any other servlet 2.4 or 2.5 container.
  • Exercises to reinforce the concepts in this section.
  • Exercise solutions. Uses @WebServlet, so runs only on Tomcat 7 or other servlet 3.0 containers. However, code could be easily adapted to servlet 2.4 or 2.5 containers by deleting @WebServlet (and the associated import statement) and adding a web.xml file with servlet mappings.
  • Online apps. Downloading the source code (above) and using it for your own apps is probably what most developers want to do. But if you just want to see the apps in action, try the following links.

Session Tracking

JSP Introduction & Overview

Invoking Java Code with JSP Scripting Elements

Controlling the Structure of Generated Servlets: The JSP page Directive

Including Files and Applets in JSP Pages

Using JavaBeans Components in JSP Documents

Note: in modern app servers that support the JSP expression language, there is little need to use beans using these somewhat old-fashioned tags. So, you might want to skip this section and move on to the next section on MVC, which uses beans in a more modern way.

The Model View Controller (MVC) Architecture: Integrating Servlets and JSP

This is the most important section in this entire tutorial. And doing the exercises in this section is the most important practice you can do.

The JSP 2 Expression Language: Simplifying MVC and Access to Java Code

Creating Custom JSP Tag Libraries: The Basics

Creating Custom JSP Tag Libraries: Advanced Topics

Advanced Topics

The tutorials in this section cover basic and intermediate servlet and JSP programming techniques. See the advanced servlet and JSP tutorials section for information on JSTL, filters, listeners, web.xml details, declarative security, programmatic security, custom tag libraries, JDBC, using the Spring Framework in Web applications, and more. For those supporting very, very old servers, you can also access the version 1.0 servlet tutorial and JSP tutorial.

Also, for advanced applications, you should strongly consider the use of JSF 2 as an alternative to servlets and JSP. See the JSF 2 tutorial for more info.

PowerPoint Files for University Faculty

The PDF files in this tutorial contain the complete text of the original PowerPoint files, so if your goal is learning this technology, just stick with this tutorial. However, as a service to instructors teaching full-semester courses at accredited universities, will release the original PowerPoint files for free. Please see the instructor materials page for details.

More Information


JSF (JavaServer Faces)

Servlets & JSP
Ajax, GWT, & JavaScript

Spring, Hibernate, & JPA