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 the 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.
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.
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 the servlet, we write java code inside Html but JSP, it allows us to write java code into Html. JSP allows 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.
Servlet lifecycle
When a user requests a servlet, a web container will check whether any instance of the servlet is available, or not. If not, then web container will instantiate a new instance of the servlet. If instance is already created, then web container will create a new thread of instance. If web container doesn't get a request for servlet, over a long period (specified in web container), web container will destroy the instance, of the servlet.
Allows you to focus on business logic
You can consider a web container as your application assistant, who will perform all necessary extra work on your behalf. Web container allows you to focus on business logic, by building server sockets, listen on a port, and perform all underlying services.
Provide security
You can consider a web container as a guardian of the servlet. Web container controls accessibility permission, such as a user accessing files, or networks.
How does the web container build page?
When we deploy servlet in web container, it is simply converted into an XML document, which is known as deployment descriptor (web.xml). Itwill allow us to map a particular servlet, to a user request (URL pattern). Let's examine a simple example.
- < servlet > < servlet - name > FilterFirstServlet < /servlet-name><servlet-class>Filter.FilterFirstServlet</servlet - class > < /servlet><servlet-mapping><servlet-name>FilterFirstServlet</servlet - name > < url - pattern > /FilterFirstServlet</url - pattern > < /servlet-mapping>
When a 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 the client can call servlet. We can use wildcards in <url-pattern>.
Happy learning!!!