Common Java Servlet Questions

Here is a list of the most common questions about Java Servlet.

What is servlet?

Java Servlet is a Server component where 'serv' means server and 'let' means component. A servlet is a java file, which takes requests from a client, processes requests and generates an html page to client.



Why do we need a servlet?

Servlet is a basic fundamental unit for creating dynamic data driven Web applications in Java. Now the question arises, why do we even need dynamic web applications? In today's world, content is user driven and displays based on user settings. Websites also store data in the backend. Static websites have limited functionality. Almost every website is data driven these days, unless its a company profile. Some of the common examples of these websites are Amazon, Facebook, and Ebay. 
Introduction To Servlet

How come servlet doesn't have a main()? How does work?

If you've created a basic program in Java then you must know that every Java program has a main() method, which is the starting point of the program.  So, how come servlets don't have a main()? That is because servlets are served using via Web containers. When a client requests a servlet, server hands requests to a Web container where the servlet is deployed.
Introduction To Servlet

Why do we use Web container?

Why do we need Web containers? Isn't using a Web container extra overhead? Not really. Web containers are a way to deploy Web components. A container runs on its own and provides all the resources and support a Servlet needs to run. Web containers also have several other benefits including deployment and maintenance.

Translate JSP

In servlet we write java code inside html but JSP allow us to write java code into html. JSP allow easy development of web pages and allows web designers and web developers to work independently. All JSP pages are translated into servlets, and web container is responsible for translating JSP into servlet.
Introduction To Servlet

Servlet lifecycle

When a user requests servlet, web container will check whether any instance of servlet is available or not, if not then web container will instantiate new instance of servlet. If instance is already created then web container will create new thread of instance. If web container doesn't get request for servlet over a long period (specified in web container) web container will destroy instance of servlet.

Allows you to focus on business logic

You can consider web container as your application assistant who will perform all necessary extra work on behalf of you. Web container allows you to focus on business logic by building serversockets, listen on port and perform all underlying services.

Provide security

You can consider web container as a guardian of the servlet. Web container controls accessibility permission, such as user accessing file or network.

How does web container build page?

When we deploy servlet in web container it is simply converted into XML document which is known as deployment descriptor (web.xml) which will allow us to map a particular servlet to user request (URL pattern). Let's examine a simple example. 
  1. <servlet>  
  2.    <servlet-name>FilterFirstServlet</servlet-name>  
  3.    <servlet-class>Filter.FilterFirstServlet</servlet-class>  
  4. </servlet>  
  5. <servlet-mapping>  
  6.    <servlet-name>FilterFirstServlet</servlet-name>  
  7.    <url-pattern>/FilterFirstServlet</url-pattern>  
  8. </servlet-mapping>  
When user requests servlet we can map request to servlet with the help of servlet and servlet mapping tag.
<servlet-name> is used to map <servlet> with <servlet-mapping>. In the example we have <servlet-name> as FilterFirstServlet (in both <servlet> and <servlet-mapping>) which will map <servlet> and <servlet-mapping>.
In <servlet-class> we specify a fully-qualified class name.
In <url-paatern> we specify url by which client can call servlet. We can use wildcards in <url-pattern>.
Happy learning!!!