Skip to main content

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.


Popular posts from this blog

Setting Background Image in JFrame - Swing

Here is sample tutorial, a simple trick that enables you to set background image for JFrame.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class BackgroundImageJFrame extends JFrame
JButton b1;
JLabel l1;
public BackgroundImageJFrame()
setTitle("Background Color for JFrame");
One way
setLayout(new BorderLayout());
JLabel background=new JLabel(new ImageIcon("C:\\Users\\Computer\\Downloads\\colorful design.png"));
background.setLayout(new FlowLayout());
l1=new JLabel("Here is a button");
b1=new JButton("I am a button");
// Another way
setLayout(new BorderLayout());
setContentPane(new JLabel(new ImageIcon("C:\\Users\\Computer\\Downloads\\colorful design.png")));
setLayout(new FlowLayout());
l1=new JLabel("Here is a button");
b1=new JButton("I am …

Add two numbers in JSP easily

Here is an example on adding up two numbers in JSP. This is very simple as you see below. This requires very small piece of code. All i have done here is created a HTML file containing the input fields which contain the numbers to add up.Now the values in the input field are of type String, so i have done type casting and converted them into int using Integer.parseInt(). See how easy it looks.

Creating Transparent JPanel in Swing

It is a quite common question, "how to create transparent panels in swing" and if you think that the solution is critical and involves a lot of hacks, you are wrong. Yes, because creating transparent panels in swing is as easy and you are already might be familiar with it.