Servlet in Java

A servlet is an interface whose implementation extends the functionality of a server. A servlet communicates with clients through a request-response principle. While servlets can serve any request, they are commonly used to extend web servers.

Most of the classes and interfaces needed to create servlets are contained in the javax.servlet and javax.servlet.http packages.

Basic Servlet Methods:

  • public void init(ServletConfig config) throws ServletException is thrown as soon as the servlet is loaded into memory
  • public ServletConfig getServletConfig() returns a reference to an object that provides access to servlet configuration information
  • public String getServletInfo() returns a string containing information about the servlet, for example: author and version of the servlet
  • public void service(ServletRequest request, ServletResponse response) throws ServletException, java.io.IOException is thrown to process each request
  • public void destroy() is executed before unloading the servlet from memory

Advantages of servlet technology over CGI (Common Gateway Interface)

  • Servlets provide better query performance and more efficient memory use by taking advantage of multithreading (a new thread is created for each request, which is faster than allocating memory for a new object for each request, as it happens in CGI).
  • Servlets are both platform and system independent. Thus, a web application written using servlets can be launched in any servlet container that implements this standard and on any operating system.
  • Using servlets increases the reliability of the program because the servlet container takes care of the servlet lifecycle (and hence memory leaks), security, and garbage collection on its own.
  • Servlets are relatively easy to learn and maintain, so the developer only needs to care about the business logic of the application, not the internal implementation of web technologies.

Read also:


Comments

Popular posts from this blog

Methods for reading XML in Java

XML, well-formed XML and valid XML

ArrayList and LinkedList in Java, memory usage and speed