Common Java Servlet Questions

DZone 's Guide to

Common Java Servlet Questions

Get your Java servlet questions answered...

· Java Zone ·
Free Resource

What Is a Servlet?

A Java servlet is a Server component where serv means server and let means component. A servlet is a Java file that takes requests from a client, processes the request, and generates an HTML page to the client.

Image title

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 back-end. 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.

Image title

Why Doesn't Servlet Have a Main()? How Does It 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, the server hands requests to a web container where the servlet is deployed.

Image title

Why Do We Use Web Containers?

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 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.

How Can We Translate JSP?

In a servlet, we write Java code inside HTML but JSP allows us to write Java code in HTML. JSP allows easy development of web pages and allows web designer and web developer to work independently. All JSP pages are translated into servlet and web container is responsible for translating JSP into servlet.

Image title

What Is the Servlet Lifecycle?

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

Allows a 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 server sockets, listen on a port and performing all underlying services.

Provide Security

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

How Do Web Containers Build a 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 the particular servlet to a user request(URL pattern). Let examine a simple example.


When user request foe servlet we can map request to servlet with help of servlet and servlet mapping tag.  <servlet-name>  is used to map <servlet> with <servlet-mapping>. In this 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 the URL by which the client can call a servlet. We can use wildcards in <url-paatern>.

I hope these concepts are clear to you.

Happy learning!

java, jsp, questions, servlet, web components, web containers

Published at DZone with permission of Hitanshi Mehta . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}