What is Java Servlet?
Solution 1:
A servlet is simply a class which responds to a particular type of network request - most commonly an HTTP request. Basically servlets are usually used to implement web applications - but there are also various frameworks which operate on top of servlets (e.g. Struts) to give a higher-level abstraction than the "here's an HTTP request, write to this HTTP response" level which servlets provide.
Servlets run in a servlet container which handles the networking side (e.g. parsing an HTTP request, connection handling etc). One of the best-known open source servlet containers is Tomcat.
Solution 2:
A servlet at its very core is a java class; which can handle HTTP requests. Typically the internal nitty-gritty of reading a HTTP request and response over the wire is taken care of by the containers like Tomcat. This is done so that as a server side developer you can focus on what to do with the HTTP request and responses and not bother about dealing with code that deals with networking etc. The container will take care of things like wrapping the whole thing in a HTTP response object and send it over to the client (say a browser).
Now the next logical question to ask is who decides what is a container supposed to do? And the answer is; In Java world at least It is guided (note I did not use the word controlled) by specifications. For example Servlet specifications (See resource 2) dictates what a servlet must be able to do. So if you can write an implementation for the specification, congratulations you just created a container (Technically containers like Tomcat also implement other specifications and do tricky stuff like custom class loaders etc but you get the idea).
Assuming you have a container, your servlets are now java classes whose lifecycle will be maintained by the container but their reaction to incoming HTTP requests will be decided by you. You do that by writing what-you-want-to-do in the pre-defined methods like init(), doGet(), doPost() etc. Look at Resource 3.
Here is a fun exercise for you. Create a simple servlet like in Resource 3 and write a few System.out.println() statements in it's constructor method (Yes you can have a constructor of a servlet), init(), doGet(), doPost() methods and run the servlet in tomcat. See the console logs and tomcat logs.
Hope this helps, happy learning.
Resources
Look how the HTTP servlet looks here(Tomcat example).
Servlet Specification.
Simple Servlet example.
Start reading the book online/PDF It also provides you download of the whole book. May be this will help. if you are just starting servlets may be it's a good idea to read the material along with the servlet API. it's a slower process of learning, but is way more helpful in getting the basics clear.
Solution 3:
In addition to the above, and just to point out the bleedin' obvious...
To many this is hyper obvious, but to someone used to writing apps which are just run and then end: a servlet spends most of its time hanging around doing nothing... waiting to be sent something, a request, and then responding to it. For this reason a servlet has a lifetime: it is initalised and then waits around, responding to anything thrown at it, and is then destroyed. Which implies that it has to be created (and later destroyed) by something else (a framework), that it runs in its own thread or process, and that it does nothing unless asked to. And also that, by some means or other, a mechanism must be implemented whereby this "entity" can "listen" for requests.
I suggest that reading about threads, processes and sockets will throw some light on this: it's quite different to the way a basic "hello world" app functions.
It could be argued that the term "server" or "servlet" is a bit of an overkill. A more rational and simpler name might be "responder". The reason for the choice of the term "server" is historical: the first such arrangements were "file servers", where multiple user/client terminals would ask for a specific file from a central machine, and this file would then be "served up" like a book or a plate of fish and chips.
Solution 4:
What is a Servlet?
- A servlet is simply a class which responds to a particular type of network request - most commonly an HTTP request.
- Basically servlets are usually used to implement web applications - but there are also various frameworks which operate on top of servlets (e.g. Struts) to give a higher-level abstraction than the "here's an HTTP request, write to this HTTP response" level which servlets provide.
Servlets run in a servlet container which handles the networking side (e.g. parsing an HTTP request, connection handling etc). One of the best-known open source servlet containers is Tomcat.
In a request/response paradigm, a web server can serve only static pages to the client
- To serve dynamic pages, a we require Servlets.
- Servlet is nothing but a Java program
- This Java program doesn’t have a main method. It only has some callback methods.
- How does the web server communicate to the servlet? Via container or Servlet engine.
- Servlet lives and dies within a web container.
- Web container is responsible for invoking methods in a servlets. It knows what callback methods the Servlet has.
Flow of Request
- Client sends HTTP request to Web server
- Web server forwards that HTTP request to web container.
- Since Servlet can not understand HTTP, its a Java program, it only understands objects, so web container converts that request into valid request object
- Web container spins a thread for each request
- All the business logic goes inside doGet() or doPost() callback methods inside the servlets
- Servlet builds a Java response object and sends it to the container. It converts that to HTTP response again to send it to the client
How does the Container know which Servlet client has requested for?
- There’s a file called web.xml
- This is the master file for a web container
-
You have information about servlet in this file-
-
servlets
- Servlet-name
- Servlet-class
-
servlet-mappings- the path like /Login or /Notifications is mapped here in
- Servlet-name
- url-pattern
- and so on
-
servlets
Every servlet in the web app should have an entry into this file
- So this lookup happens like- url-pattern -> servlet-name -> servlet-class
How to "install" Servlets? * Well, the servlet objects are inherited from the library- javax.servlet.* . Tomcat and Spring can be used to utilize these objects to fit the use case.
Ref- Watch this on 1.5x- https://www.youtube.com/watch?v=tkFRGdUgCsE . This has an awesome explanation.
Solution 5:
Servlet is server side technology which is used to create dynamic web page in web application. Actually servlet is an api which consist of group of classes and interfaces, which has some functionality. When we use Servlet API we can use predefined functionality of servlet classes and interfaces.
Lifecycle of Servlet:
Web container maintains the lifecycle of servlet instance.
1 . Servlet class loaded
2 . Servlet instance created
3 . init() method is invoked
4 . service() method invoked
5 . destroy() method invoked
When request raise by client(browser) then web-container checks whether the servlet is running or not if yes then it invoke the service() method and give the response to browser..
When servlet is not running then web-container follow the following steps..
1. classloader load the servlet class
2. Instantiates the servlet
3. Initializes the servlet
4.invoke the service() method
after serving the request web-container wait for specific time, in this time if request comes then it call only service() method otherwise it call destroy() method..