Sitemap

Java Chapter -18

3 min readJul 19, 2022

--

Hi all Hope you all doing good, In our last article we have discussed about JAR which is used to deploy our application. In this article we are going to talk about Servlet which is another method to deploy our application. Let’s get into business.

(Servlet's what are they? How do they work?….) Servlets are Java programs that run on (and with) an HTTP web server. When a client uses a web browser to interact with a web page, a request is sent back to the web server. If the request needs the help of a Java servlet, the web server runs the servlet code.

Get farook fazni’s stories in your inbox

Join Medium for free to get updates from this writer.

(How do we Create a Servlet…….) There are several steps to make an servlet, and they are,

First we need to find out where our servlets need to be placed.

For this, we’ll assume that we already have a web server up and running, and it’s already configured to support servlets. The important thing in here is finding out exactly where we place our servlet class files in order to our server to ‘see’ them.

In next step we need to get the servlets.jar file and add it to our classpath

Servlets are not part of the standard Java libraries, which means we need the servlets classes packaged into the servlets.jar file. we can download the servlets classes from java.sun.com, or we can get them from our Java-enabled web server (Apache Tomcat, at the apache.org site). Without these classes, we can’t compile our servlets.

As the Third step we need to write a servlet class by extending HttpServlet

public class MyServletA extends HttpServlet { … }

And Then we need to write an HTML page that invokes your servlet

At the end we need make our servlet and HTML page available to our server

We can see the example code as below,

Press enter or click to view image in full size

And then we can create a simple HTML page with a link to our servlet,

Press enter or click to view image in full size

That’s all for this article, I hope you guys got something. Let’s meet in next article soon until then bye bye. OH wait…. Most of the images in this article are taken from the Head First Java book which is written by Kathy Sierra, Bert Bates. Don’t forget to read the book as well.

Reference: Head First Java, 2nd Edition [Book] (oreilly.com)

--

--

farook fazni
farook fazni

Written by farook fazni

Associate AI/ML Engineer at Virtusa | 2 years in AI solutions | Skilled in Azure Synapse, PySpark, SQL | Exploring Generative AI, Transformers, and LLMs.