Servlet Notes
Servlet Notes
Servlet is a java program that runs inside JVM on the web server. It is used
for developing dynamic web applications.
What is dynamic web application?
A web application can be described as collection of web pages (e.g. a
website) and when we call it dynamic,
It simply means that the web pages are not same for all the users, web pages
would be generated on server side based on the request made by client (user’s
browser).
Difference between static and dynamic web page
Static page as name suggests remains same for all users
However a dynamic web page changes based on the request from client
(user’s browser).
Example: consider a web application that shows you two input fields & an
add button and when you enter two numbers and click add, it shows you another
web page that has the result of addition of two numbers, this web application is
dynamic in nature as the second web page that shows you the result changes based
on the user input, it is not static for all users.
CGI (Common Gateway Interface)
CGI has several limitations such as performance, scalability, reusability etc.
that a servlet doesn’t have.
Limitations of CGI
Server has to create a new CGI process for every client request.
Example: If 100 users are accessing the web application, then the server has
to create 100 CGI processes to handle the request made by them. Since a server has
limited resources, creating new process every time for a new request is not a viable
option, this imposed the limitation on server, due to that the server cannot handle
more than a specified number of users at the same time.
How Servlet is better than CGI
CGI programs are handled by a new process every time a new request has
been made.
Unlike CGI, the servlet programs are handled by separate threads that can
run concurrently more efficiently.
CGI program can be written in any programming language that makes it mostly
platform dependent as not all programming languages are platform independent.
Servlet only uses Java as programming language that makes it platform
independent and portable.
Another benefit of using java is that the servlet can take advantage of the
object oriented programming features of java.
Features of Servlet
1. Portable:
As I mentioned above that Servlet uses Java as a programming language,
Since java is platform independent, the same holds true for servlets.
Example: you can create a servlet on Windows operating system that users
Glass Fish as web server and later run it on any other operating system like Unix,
Linux with Apache tomcat web server,
This feature makes servlet portable and this is the main advantage servlet has
over CGI.
2. Efficient and scalable:
Once a servlet is deployed and loaded on a web server, it can instantly start
fulfilling request of clients.
The web server invokes servlet using a lightweight thread so multiple client
requests can be filling by servlet at the same time using the multithreading feature
of Java.
Compared to CGI where the server has to initiate a new process for every client
request, the servlet is truly efficient and scalable.
3. Robust:
By inheriting the top features of Java (such as Garbage collection, Exception
handling, Java Security Manager etc.)
the servlet is less prone to memory management issues and memory leaks.
This makes development of web application in servlets secure and less error prone.
Servlet API
We need to use Servlet API to create servlets.
There are two packages that you must remember while using API,
1. javax.servlet package that contains the classes to support generic servlet
(protocol-independent servlet)
2. javax.servlet.http package that contains classes to support http servlet.
java.lang.Object
|_extended by javax.servlet.GenericServlet
|_extended by javax.servlet.http.HttpServlet
Generic Servlet
If you are creating a Generic Servlet then you must
extend javax.servlet.GenericServlet class.
GenericServlet class has an abstract service() method. Which means the
subclass of GenericServlet should always override the service() method.
That’s it. It would ask you to restart the Eclipse after installation, choose
restart. You would now be able to see Apache when you try to add the server in
Eclipse.
Continue to Apache configuration setup -----------------→
You will be presented with a window as shown in the image below. Click
browse and select the folder that you have extracted from the zip file in Step 2.
Click finish.
That’s it you have successfully configured the tomcat server in Eclipse, you
can now run the JSP in Eclipse.
Creating Servlet in Eclipse IDE
Step 1: Create a Project:
Create a Servlet application in Eclipse.
Open Eclipse and then
Click File ❯ New ❯ Click Dynamic Web Project.
Step 4: Select the following three options under “Web, XML, Java EE and OSGI
Enterprise Development”
• Eclipse Java EE Developer Tools
• Eclipse Java Web Developer Tools
• Eclipse Web Developer Tools
Step 5: Click next and you would see that the software are installing. Wait for
some time and then a popup would ask your permission to restart the Eclipse.
Restart it and you would find the dynamic web project option under project list.
MyServletDemo.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>BeginnersBook Servlet Demo</title>
</head>
<body>
<a href="welcome">Click to call Servlet</a>
</body>
</html>
<servlet>
<servlet-name>MyHttpServletDemo</servlet-name>
<servlet-class>MyServletDemo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyHttpServletDemo</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
</web-app>
Final Project Structure
Output: