Servlets are protocol and platform independent server-side software components,written in Java. They run inside a Java enabled server or ap...
Servlets are protocol and platform independent server-side software components,written in Java. They run inside a Java enabled server or application server. Servlets are loaded and executed within the Java Virtual Machine (JVM) of the Web server or application server, in much the same way that applets are loaded and executed within the JVM of the Web client. Since servlets run inside the servers, however, they do not need a graphical user interface (GUI). In this sense, servlets are also faceless objects. Servlets more closely resemble Common Gateway Interface (CGI) scripts or programs than applets in terms of functionality. As in CGI programs, servlets can respond to user events from an HTML request, and then dynamically construct an HTML response that is sent back to the client. Servlet process flow Servlets implement a common request/response paradigm for the handling of the messaging between the client and the server. The Java Servlet API defines a standard interface for the handling of these request and response messages between the client and server.
- The client sends a request to the server.
- The server sends the request information to the servlet.
- The servlet builds a response and passes it to the server. That response is dynamically built, and the content of the response usually depends on the client’s request. External resources may also be used.
- The server sends the response back to the client.
Servlets are powerful tools for implementing complex business application logic. Written in Java, servlets have access to the full set of Java API’s, such as JDBC for accessing enterprise databases. As mentioned above, servlets are similar to CGI in that they can produce dynamic Web content.
Servlets, however, have the following advantages over traditional CGI programs:
❑ Portability and platform independence: Servlets are written in Java, making them portable across platforms and across different Web servers, because the Java Servlet API defines a standard interface between a servlet and a Web server.
❑ Persistence and performance: A servlet is loaded once by a Web server, and invoked for each client request. This means that the servlet can maintain system resources, like a database connection, between requests. Servlets don’t incur the overhead of instantiating a new servlet with each request. CGI processes typically must be loaded with each invocation.
❑ Java based: Because servlets are written in Java, they inherit all the benefits of the Java language, including a strong typed system, object-orientation, and modularity, to name a few.