In this tutorial, you will have a basic understanding of Servlet and why we need Servlet beside JSP.
Servlet is a server component which has capability to process request and construct response in protocol-independent fashion. Although Servlet can reponse to any kind of request but it is usually used to extend web applications hosted by a web server. When using in web application, Servlet provides a set of classess for dealing with HTTP request. Here is the class diagram of Java Servlet API
.gif)
There are two main packages: javax.servlet and javax.http.servlet. These packages are the center packages of Java servlet API. The javax.servlet provides interaces between applications and servlet container. For developing web applications HttpServlet is used.
Why do we use Java Servlet?
Java servlet was created before JSP to replace the CGI model to have a better way to develop high performance and scalable web applications. At that time the HTML was mixed with HTML inside a plain old Java class which was very difficult to maintain and read. The web designer had to to understand Java code to make the web design. Then JSP was invented to make the presentation of dynamic content easier. But why do we still need servlet? It is because:
- JSP is a servlet. At runtime, JSP page is compiled into a servlet to handle HTTP request and response.
- For the MVC model, servlet is used to develop the controller part (C) which contains a complex business logic.