The popularity of java has various reasons but the primary motivation was
Platform Independence
WebTechnology (Internet)
Taking web technology into consideration is where J2EE (Server Side Code) stands as Enterprise Edition of java(a standard for developing Multitier Enterprise applications)which promotes application component reusability , the factor which helps in reducing development time and costs significantly.
Java Servlets are a key Component of Server side Java development. A servlet is a small, pluggable extension to a server that enhances the service functionality. Servlets allows Developers to extend and customize in java-enabled servers such as web server, a mail server, an application server or any Customer server with the hitherto unknown degree of portability, flexibility and ease.
A Servlet is a generic server extension, a java class that can be loaded dynamically to expand the functionality of a server. Servlets are commonly used with web server, where they can replace CGI scripts. A servlet is similar to proprietary server extension, except that it runs inside a Java Virtual Machine (JVM) on the server. So it is safe and portable.
Servlets operate solely with in the domain of the server unlike applets they don’t require the support of java in web browser. This means that servlets are also efficient and scalable. Because, servlets run within the webserver it can do things that are not possible using CGI Scripts.
Java servlets, as define by java Software divisions of Sun Microsystems (formally known as Java Soft), are the first Standard extensions to java. This means servlets are officially blessed by sun and are part of java language, but they are not part of core java API.
The Servlet has various advantageous features such as the following
Portable
Highly portable across Operating Systems and across Server Implementations because they interface to particular server architecture through a set of standard interface called Servlet Engine (i.e. platform independent).
Power
Servlets (server side java) can work with the full power of core API (J2SE) implementations such as Multithreading, N/W, RMI , Database Connectivity ,CORBA ,etc.
Efficient and Scalable
An intuitive Threading Model brings in the efficiency to handle each request using a New light weight thread a way which helps in handling multiple concurrent requests each by a separate thread so obtaining the scalability required.
Persistent and Endurance
Once a servlet is loaded it generally remains in the server’s memory as a single object instance. Thereafter the server invokes the servlet (by this instance) to handle a request using a simple light weight method invocation.
Servlets are naturally enduring objects because they automatically maintain their state and can hold external resources (such as d/b connections) when they remain in server’s memory as single object instance.
Safe and Secure
It is safe due to Robustness which includes Exception Handling and Memory Management techniques (Garbage Collection) hence no memory leaks or memory management problems like dangling pointers, invalid pointer reference etc because there is no such concept called pointers here.
Secure since it runs inside JVM (Java Virtual Machine) which resides inside the server and the server further protects itself from servlets (crashing it) through the use of Java Security Manager.
Elegant
Servlet code is clean, Object Oriented, Modular and amazingly simple. One reason for its simplicity is the Servlet API which includes methods and classes to handle many of the routine chores of servlet development. Even advanced operations like Cookie handling AND session Tracking are abstracted to convenient classes.
Integration
Servlets and tightly integrated with the servers hence can easily cooperate with the servers for performing various operations such as authorization check,translate file paths etc.
Flexibility
Any page can be included in to a static file by using either Server side Includes or Java Bean.
Architecture Neutral
Can be designed to be compiled for a specific target.
Servlet usage
To make Servlet usage easy Sun has made publicly available a set of classes that provide basic servlet report. The javax.servlets and javax.servlets.http packages constitute this servlet API. In addition to servlet classes you need servlet engine.
Servlet engine (Servlet Container)
Servlet interface to a particular server architecture through a set of standard interface called
Servlet Engine(to run servlets we just can use any web server ,since servlets can be run using a standalone servlet engine the web server doesn’t need to support servlet).
This container translates requests from whatever protocol is being used in to objects that servlets understand
Also gives the servlet an object that can be used to send a response
The container is also responsible for managing the Life Cycle of the servlet.
There are three flavors of Servlet Engines
Stand-Alone Servlet Engines
Add-on Servlet Engines
Embeddable Servlet Engines
A stand-alone engine is the server that includes build-in support for servlets. Such an engine has the advantage that every thing works right out of the box. Some famous servers are Java Web server, Jigsaw server, Netscape Enterprise server, JSWDK (java server web development kit).
The Add-on servlet engine functions has plug-ins to the existing server, it adds servlet support to the server that was not originally designed keeping servlets in mind. Some famous servlets are Java Apache Server, Live Software’s JRun server, and IBM Websphere.
An embedded engine is generally a light weight server deployment platform that can be embedded in another application. Some of the famous webservers are Java server, Jef PosKanzer’s Acme server, and Paralogic’s webcore.
Life Cycle of a Servlet
The Servlet container creates an instance of the Servlet
The container calls the created instance’s Init() method.
If the container has a request for the servlet ,the instance’s service() method is called.
Before destroying the Instance , the container calls the destroy() method.
The instance is destroyed and marked for garbage collection.
There is nothing to stop a servlet container from performing the entire servlet lifecycle each time a servlet is requested.
JSP (Java Server Pages)
They are a direct extension of Java servlets and provide a way to separate content generation from content presentation. JSP gives us a way to glue together HTML, Java code and components by simply creating a special webpage that will be dynamically compiled in to a servlet the first time it is called.
When a request is made for the JSP document , the server recognizes the *.jsp extension and realizes that special handling is required and processed in the following format.
Client requests a JSP page
JSPEngine compiles the JSP into a servlet
The generated servlet is compiled and loaded.
The compiled servlet services the request and sends back the response to the client.
General Internet Terms
Web Browser
A web browser can be thought of as a Universal user Interface responsible for
Presenting web content
Issuing requests to the web server and
Handling any results generated by the request.
Web server
Webserver is a program running on the server that listens for incoming requests and services those requests as they come in depending up on the type of request. It might search for the requested page or at times execute a program on the server architecture.
Servlet Engine(Servlet Container)
Servlet interface to a particular server architecture through a set of standard interface called Servlet Engine (to run servlets we just can use any web server ,since servlets can be run using a standalone servlet engine the web server doesn’t need to support servlets).
|