Java Servlet Tutorial: Basic Introduction To Java Servlets
What is the role of a servlet?
What do you observe in an interactive website? Most of the users are unaware of the back-end processes and requests that are carried out during their visit. A web server accepts all the user requests and forward them for further processing. It involves accessing data and generating the desired user output.
The main role of a servlet is to handle incoming requests and generate results according to the user requests. A servlet can handle multiple requests and work in an efficient manner. There are several technologies that were used before the development of servlet. Some of these technologies include Common Gateway Interface (CGI), Netscape Server API (NSAPI), and Active Server Pages (ASP). You can understand the benefit of using servlets over these technologies in a servlet tutorial.
Why choose servlets?
1.Servlets are platform independent.
2.Servlets handle multiple requests by creating new threads instead of initiating separate processes for every request (as CGI).
3.High-end industry support for servlets including availability of container servers (Apache Tomcat) for major application servers.
Servlets are Java program modules that are used to extend web server's capabilities and process requests initiated by the user. Java Servlets are commonly used to store and process data submitted on a web page. Servlets are most commonly used with HTTP protocol for different types of server applications. Servlets follow a request-response communication model. Let us consider a servlet tutorial and understand the working of Java servlet.
How does a servlet work?
You need to start with the HttpServlet subclass for your first Java servlet. It allows you to access different request and response wrappers for handling user requests and generate responses. Java servlets wrap different low-level Java constructs in a convenient format. When a user makes a request through a URL, it is converted to a HttpServletRequest and sent to the URL address (as mentioned in the servlet container). The result is processed and sent back in an HttpServletResponse to display a raw HTTP response to the user. Generally, users make multiple requests and receive multiple responses during a Java session. All the requests and responses made in a session are wrapped inside an HttpSession object.
Containers are used to manage the runtime environments for Java Servlets. Some of the most common servlet containers include Tomcat, Glassfish, WebLogic, Jetty, and JBoss. You need to configure Jav containers and expose your servlets to the world because it is up to the container to manage and initialize servlets throughout the lifecycle of the program.
Lifecycle of Servlet
1.init ( ): It is the first stage and it initiates a particular servlet.
2.service ( ): It is used to service user requests.
3.destroy ( ): It is the final stage of a servlet and it ends the service of a servlet.
If you are getting started with the Java servlets tutorial, it is best to start with the basic "Hello World" servlet. It is best to understand the basic concepts of servlets before you can apply them to advanced web applications.