Blog is under construction. Blog is under construction.

Me on Flickr

Wednesday, 1 August 2012

Servlets - Advantages of Servlet


Advantages of Servlets
In any J2EE web applications servlets are an integral part. The server side component of a servlets gives a powerful mechanism for developing server side web applications. It provides an important role in the explosion of Internet, its reusability, performance and scalability. Web developers can create fast and efficient server side programs or applications by using servlets. By using servlets web developers can run these applications in any servlet enabled web servers.
The main advantages of using servlets over CGI are, the CGI programs are run outside the web server so a new process should be started before the execution of a CGI programs. At a time the CGI programs handle only one request. After the execution of a CGI program they return the result in the web server and exit. But in the case of servlets it can handle multiple requests simultaneously. Servlets generate dynamic content or create dynamic web pages that is easy to write and faster to run within a web servers. Servlets can access any J2SE and J2EE APIs and it can take the full advantage and capabilities of the java programming language. Servlets are component based, platform independent method for create in the web based applications, without the performance limitations of CGI programs.
The advantages of servlets are discussed below.
1)      Portability
2)      Powerful
3)      Efficiency
4)      Safety
5)      Integration
6)      Extensibility
7)      Inexpensive
8)      Secure
9)      Performance
Portability
Servlets are highly portable crosswise operating systems and server implementations because the servlets are written in java and follow well known standardized APIs. Servlets are writing once, run anywhere (WORA) program, because we can develop a servlet on Windows machine running the tomcat server or any other server and later we can deploy that servlet simply on any other operating system like Unix. Servlets are extremely portable so we can run in any platform. So we can call servlets are platform independent one. Servlets are written entirely in java.
Powerful
We can handle several things with the servlets which were difficult or sometimes impossible to do with CGI. For example the CGI programs can’t talk directly to the web server but the servlets can directly talk to the web server.  Servlets can share data among each other, they make the database connection pools easy to implement. By using the session tracking mechanism servlets can maintain the session which helps them to maintain information from request to request. Servlets can do many things which are difficult to implement in the CGI programs.
Efficiency
The servlets invocation is highly efficient as compared to CGI programs. The servlet remains in the server’s memory as a single object instance, when the servlet get loaded in the server. The servlets are highly scalable because multiple concurrent requests are handled by separate threads. That is we can handle N number of threads by using a single servlet class
Safety
As servlets are written in java, servlets inherit the strong type safety of java language. In java automatic garbage collection mechanism and a lack of pointers protect the servlets from memory management problems. In servlets we can easily handle the errors due to the exception handling mechanism. If any exception occurs then it will throw an exception by using the throw statement.
Integration
Servlets are tightly integrated with the server. Servlet can use the server to translate the file paths, perform logging, check authorization, and MIME type mapping etc.
Extensibility
The servlet API is designed in such a way that it can be easily extensible. As it stands today, the servlet API support Http Servlets, but in later date it can be extended for another type of servlets. Java Servlets are developed in java which is robust, well-designed and object oriented language which can be extended or polymorphed into new objects. So the java servlets take all these advantages and can be extended from existing class to provide the ideal solutions. So servlets are more extensible and reliable.
Inexpensive
There are number of free web servers available for personal use or for commercial purpose. Web servers are relatively expensive. So by using the free available web servers you can add servlet support to it.
Secure
Servlets are server side components, so it inherits the security provided by the web server. Servlets are also benefited with Java Security Manager.

Performance
Servlets are faster than CGI programs because each scripts in CGI produces a new process and these processes takes a lot of time for execution. But in case of servlets it creates only new thread. Due to interpreted nature of java, programs written in java are slow. But the java servlets runs very fast. These are due to the way servlets run on web server. For any program initialization takes significant amount of time. But in case of servlets initialization takes place first time it receives a request and remains in memory till times out or server shut downs. After servlet is loaded, to handle a new request it simply creates a new thread and runs service method of servlet. In comparison to traditional CGI scripts which creates a new process to serve the request.




Differences between CGI and Servlet:

  

Servlets - What is Servlet?


What is Java Servlet?
Servlet is a Java-based server-side web technology. As the name implies, it serves a client request and receives a response from the server.
Technically speaking, a Servlet is a Java class in Java EE that conforms to the Java Servlet API, a protocol by which a Java class may respond to requests. They are not tied to a specific client-server protocol, but are most often used with the HTTP protocol. Therefore, the word "Servlet" is often used in the meaning of "HTTP Servlet". Thus, a software developer may use a servlet to add dynamic content to a Web server using the Java platform.
Servlet is a server-side programming language that is used to create the dynamic web page. Servet technology is robust and scalable as it uses the java language. Before Servlet, CGI (Common Gateway Interface) scripting language was used as a server-side programming language. But there were many disadvantages of this technology. We have discussed these disadvantages below.
There are many interfaces and classes in the servlet API such as Servlet, GenericServlet, HttpServlet, ServletRequest, ServletResponse etc.
Servlet can be described in many ways, depending on the context.
·         Servlet is a technology i.e. used to create web application.
·         Servlet is an API that provides many interfaces and classes including documentations.
·         Servlet is an interface that must be implemented for creating any servlet.
·         Servlet is a class that extend the capabilities of the servers and respond to the incoming request. It can respond to any type of requests.
·         Servlet is a web component that is deployed on the server to create dynamic web page.




To deploy and run a Servlet, a Web container must be used. A Web container (also known as a Servlet container) is essentially the component of a Web server that interacts with the servlets. The Web container is responsible for managing the lifecycle of servlets, mapping a URL to a particular servlet and ensuring that the URL requester has the correct access rights.
The servlet API, contained in the Java package hierarchy javax.servlet, defines the expected interactions of the Web container and a servlet.
A Servlet is an object that receives a request and generates a response based on that request. The basic servlet package defines Java objects to represent servlet requests and responses, as well as objects to reflect the servlet's configuration parameters and execution environment. The package javax.servlet.http defines HTTP-specific subclasses of the generic servlet elements, including session management objects that track multiple requests and responses between the Web server and a client. Servlets may be packaged in a WAR file as a Web application.
Servlets can be generated automatically from JavaServer Pages (JSP) by the JavaServer Pages compiler. The difference between Servlets and JSP is that Servlets typically embed HTML inside Java code, while JSPs embed Java code in HTML. While the direct usage of Servlets to generate HTML (as shown in the example below) has become rare, the higher level MVC web framework in Java EE (JSF) still explicitly uses the Servlet technology for the low level request/response handling via the FacesServlet. A somewhat older usage is to use servlets in conjunction with JSPs in a pattern called "Model 2", which is a flavor of the model-view-controller pattern.

Related Posts Plugin for WordPress, Blogger...