Servlets Tutorial for Beginners

This tutorial will teach you servlets in Java.
In this tutorial you'll learn:
  • Desktop and web applications
  • Installing and configuring Tomcat
  • Http protocol and port numbers
  • Servlet basics
  • Generic and Http Servlets, form submissions
  • Annotations in servlets
  • Servlet communication
  • Cookies
  • Session Tracking

1. Desktop and web applications

You'll need to learn what are web apps before you start developing them.

2. Installing and configuring Tomcat

To work with servlets, you need a web server. We will use Tomcat here. Download Tomcat 7 
For every web application, we create a directory in Tomcat installation directory. So you need to know the typical folder structure for a tomcat web application.

3. HTTP Protocol and port numbers

You need to learn what is HTTP protocol, static and dynamic pages before you develop any web application.

4.  Servlet basics

You'll need to learn the basics of Java servlets that help you ace the web development with Java.

5. Generic and Http Servlets

Generic servlets are protocol-independent, it isn't just for http protocol.
Http servlets are strictly for http protocol.
There are two ways of sending data (aka making request or user input) from client to server. They are GET and POST.

GET transfers data via the address bar as query string. For example,

POST transfers data in background and hence not visible in the address bar. This is used for sending usernames and passwords as they must not be visible in the address bar.

6. Annotations in servlets

We can use annotations in servlets. Annotation is data about a class or a member variable or a method that will be useful at run time.

7. Servlet communication

Servlets need to communicate with each other to transfer data from one servlet to another.

8. Cookies

Cookies are key-value pairs that are stored in the web browser. As HTTP is a stateless protocol, there must be some way for the web application to keep track of the previous requests by the user. For example, in while shopping, our items are stored in the cart though we move to other pages, we will be displayed the previous items that we have added. This is because, our previous data is stored. Where? In our browser only!

9. Session Tracking

Session tracking is an alternative to cookies. Cookies are stored in client browser, sessions are stored in server. What if cookies are disabled in browser? What if they are manually deleted? For all this, session tracking is the ultimate solution.

Posted by at on

Tags: Servlets, Tutorials,