Introduction to Servlets

How Servlet Works

So just imagine we have a client machine there and we have a server right, so when you talk about this website world, a client sends a request to the server expecting a page right again. You can even ask for anything on Internet but let’s say you’re asking for a page now. This page can be a static page(it can be a tenant page). When it’s a static, it’s means which is already made and when you say dynamic, it means it will be built at one time.

So let’s say a client sends the request which goes your server you already have a file, so that, file goes to the client. We called a request on response finish, but let’s say if you are asking for a page which will be built as one time, so a client sends a request to this server, now the server says “hold ond I don’t have any page with this lengths”. So you have to make that page(have to build a page).

That request goes to a helper application on the server, now this helper application is also called as web container.

In this web container you will be having servlet. Servlet is basically a Java file which can take the request from the client on the Internet and it can process that request it can provide you a response in the format of HTML page. So this servlet will convert your request and it will process the information. It will send the HTML page.

Let me just go back once again: some client-side requests to the server on the server side since it is a dynamic request it will go to your web container like Apache Tomcat, GlassFish, JBoss or WebSphere. In fact all these other servers those are called as app servers but they also have a feature of web server.

When request goes to your Tomcat, Tomcat says “Hold on, I got a request now but that request is for abc.html. I don't have any pages like abc.html". So we have to build that page, so that request goes to a servlet. HOLD ON! Servelet will be having a Java name.

For example when you create Java classes let's say I want to add two numbers again a basic example is going to add two numbers. I will be having the servlet name as AddServlet that means when a request for abc.html, the page which should be getting called is your servlet. How can we do that? Inside your container, you have a special file which is called as a deployment descriptor.

So you have a file which is the deployment decriptor in which you mentioned for which request, which server should be called.

I mean in your Tomcat server you have maybe 50 to 60 servlets, so every servlet will be mentioned. I mean every request will map with one servlet. It may happen that you have multiple requests trying to access the same servlet. Everythin need to be configured in a file which is called as the deployment descriptor and the file name is web.xml.

REPEAT

  1. When clients of the request which goes to the server says “OK! This is the another page. Let’s send a request to the web container”
  2. Web container will check. “This request will be mapped by this servlet that can be done with the Helper web.xml file".

So you can seem to affect xml files we have two tags we have servlet tag and we have servlet mapping tag.

The servlet tag you have to mention the class name, and the servlet mapping tag you have to mention the url-pattern. This URL we need to call that particular servlet. But hold on, how do we get the servlet now?

When you want to create a servlet, we have to create a special class which extends HTTPServlet since we want to servlet features. This HTTPServlet will provide you all the features which you need.

All these feautres means it should be able to take the requested server, it should be processing the request, and it should be sending the response.

  1. Take the request
  2. Process the information
  3. Send the response

That response will be in HTML format. That HTML page is a dynamic page goes from your server to your client machine in the format of response object.

Let’s say we try to avoid XML. In the Servlet 3.0, we got a new feature which is called as annotation.

So we can use annotations on your servlet so you don’t have to use XML file. You can do your mapping using annotations.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store