Java Servlet
From Wikipedia, the free encyclopedia
A Servlet is a Java class which conforms to the Java Servlet API, a protocol by which a Java class may respond to http requests. Thus, a software developer may use a servlet to add dynamic content to a Web server using the Java platform. The generated content is commonly HTML, but may be other data such as XML. Servlets are the Java counterpart to non-Java dynamic Web content technologies such as CGI and ASP.NET. Servlets can maintain state across many server transactions by using HTTP cookies, session variables or URL rewriting.
The servlet API, contained in the Java package hierarchy javax.servlet, defines the expected interactions of a Web container and a servlet. A Web 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.
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 by JavaServer Pages (JSP) compiler, or alternately use template engines such as WebMacro or Apache Velocity to generate HTML. Often servlets are used in conjunction with JSPs in a pattern called "Model 2", which is a flavor of the model-view-controller pattern.
Contents |
[edit] History
The complete servlet specification was created by Sun Microsystems, with version 1.0 finalized in June 1997. Starting with version 2.3, the servlet specification was developed under the Java Community Process. JSR 53 defined both the Servlet 2.3 and JavaServer Page 1.2 specifications. JSR 154 specifies the Servlet 2.4 and 2.5 specifications. As of May 10, 2006, the current version of the servlet specification is 2.5.
In his blog on java.net, Sun veteran and GlassFish lead Jim Driscoll details the history of servlet technology. James Gosling first thought of servlets in the early days of Java, but the concept did not become a product until Sun shipped the Java Web Server product. This was before what is now the Java Platform, Enterprise Edition was made into a specification.
| Servlet API version | Released | Platform | Important Changes |
|---|---|---|---|
| Servlet 3.0 | December 2009 | JavaEE 6, JavaSE 6 | Pluggability, Ease of development, Async Servlet, Security, File Uploading |
| Servlet 2.5 | September 2005 | JavaEE 5, JavaSE 5 | Requires JavaSE 5, supports annotations |
| Servlet 2.4 | November 2003 | J2EE 1.4, J2SE 1.3 | web.xml uses XML Schema |
| Servlet 2.3 | August 2001 | J2EE 1.3, J2SE 1.2 | Addition of Filter |
| Servlet 2.2 | August 1999 | J2EE 1.2, J2SE 1.2 | Becomes part of J2EE, introduced independent web applications in .war files |
| Servlet 2.1 | November 1998 | Unspecified | First official specification, added RequestDispatcher, ServletContext |
| Servlet 2.0 | JDK 1.1 | Part of Java Servlet Development Kit 2.0 | |
| Servlet 1.0 | June 1997 |
[edit] Advantages
The advantages of using servlets is their fast performance and ease of use combined with more power over traditional CGI. Traditional CGI scripts written in Perl or C have a number of disadvantages when it comes to performance. When a HTTP request is made a new process is created for each call of the CGI script. This overhead of process creation can be very system intensive especially when the script does relatively fast operations (process creation will take more time than CGI script execution). Java servlets solve this problem by allowing each request to be handled by a separate Java thread within the Web server process, omitting separate process forking by the HTTP daemon. In addition, simultaneous CGI request causes the CGI script to be copied and loaded into memory as many times as there are requests. However with servlets there are same amount of threads as request but there will only be one copy of the servlet class created in memory.
[edit] Lifecycle of a servlet
The servlet lifecycle consists of the following steps:
- The servlet class is loaded by the container during start-up.
- The container calls the
init()method. This method initializes the servlet and must be called before the servlet can service any requests. In the entire life of a servlet, theinit()method is called only once. - After initialization, the servlet can service client requests. Each request is serviced in its own separate thread. The container calls the
service()method of the servlet for every request. Theservice()method determines the kind of request being made and dispatches it to an appropriate method to handle the request. The developer of the servlet must provide an implementation for these methods. If a request for a method that is not implemented by the servlet is made, the method of the parent class is called, typically resulting in an error being returned to the requester. - Finally, the container calls the
destroy()method that takes the servlet out of service. Thedestroy()method, likeinit(), is called only once in the lifecycle of a servlet.
Here is a simple servlet that just generates HTML. Note that HttpServlet is a subclass of GenericServlet, an implementation of the Servlet interface. The service() method dispatches requests to methods doGet(), doPost(), doPut(), doDelete(), etc., according to the HTTP request.
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " + "Transitional//EN\">\n" + "<html>\n" + "<head><title>Hello WWW</title></head>\n" + "<body>\n" + "<h1>Hello WWW</h1>\n" + "</body></html>"); } }
[edit] See also
[edit] External links
- Sun's servlet tutorial
- Beginning and intermediate servlet and JSP tutorials With source code
- Sun's servlet product description
- JSR
- New features added to Servlet 2.5 at JavaWorld
- Java Documentation of the Servlet 2.5 API
|
|||||||||||||||||||||||||
|
|||||||||||