Examine individual changes
This page allows you to examine the variables generated by the Edit Filter for an individual change.
Variables generated for this change
Variable | Value |
---|---|
Whether or not the edit is marked as minor (no longer in use) (minor_edit ) | false |
Name of the user account (user_name ) | '79.167.170.51' |
Whether or not a user is editing through the mobile interface (user_mobile ) | false |
Page ID (page_id ) | 43826 |
Page namespace (page_namespace ) | 0 |
Page title without namespace (page_title ) | 'Java servlet' |
Full page title (page_prefixedtitle ) | 'Java servlet' |
Action (action ) | 'edit' |
Edit summary/reason (summary ) | '' |
Old content model (old_content_model ) | 'wikitext' |
New content model (new_content_model ) | 'wikitext' |
Old page wikitext, before the edit (old_wikitext ) | '{{refimprove|date=February 2014}}
{{Citation style|date=May 2016|details=Violates Wikipedia:External links: "Wikipedia articles may include links to web pages outside Wikipedia (external links), but they should not normally be used in the body of an article."}}
[[File:JSPLife.png|thumb|400px|Life of a JSP file]]
A '''Java servlet''' is a [[Java (programming language) |Java]] [[Computer program|program]] that extends the capabilities of a [[Server (computing)|server]]. Although servlets can respond to any types of requests, they most commonly implement applications hosted on [[Web server]]s.<ref>{{cite web
| accessdate = 2011-04-27
| ___location = http://www.webopedia.com/
| publisher = WEBOPEDIA| title = servlet
| quote = A small java program that runs on a server. The term usually refers to a Java applet which runs within a web server environment. This is analogous to a Java applet that runs within a web browser environment.
| url = http://www.webopedia.com/TERM/S/servlet.html}}</ref> Such Web servlets are the [[Java (software platform)|Java]] counterpart to other [[dynamic web page|dynamic Web content]] technologies such as [[PHP]] and [[ASP.NET]].
== Introduction ==
A Java servlet processes or stores a [[Java class]] in [[Java EE]] that conforms to the Java Servlet API,<ref>{{cite web|url=http://docs.oracle.com/javaee/7/api/javax/servlet/Servlet.html|title=Servlet (Java(TM) EE 7 Specification APIs)|work=oracle.com|accessdate=22 November 2016}}</ref> a standard for implementing Java classes that respond to requests. Servlets could in principle communicate over any [[client–server model|client–server]] protocol, but they are most often used with the [[Hypertext Transfer Protocol|HTTP protocol]]. Thus "servlet" is often used as shorthand for "HTTP servlet".<ref name="r1">{{cite web |url=http://www.novocode.com/doc/servlet-essentials/chapter1.html |title=Servlet Essentials - Chapter 1 |work=novocode.com |accessdate=2016-11-22}}</ref> Thus, a [[software developer]] may use a servlet to add [[dynamic web page|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 can maintain [[state (computer science)|state]] in [[session (computer science)|session]] variables across many server transactions by using [[HTTP cookie]]s, or [[URL rewriting]].
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 [[Application programming interface|API]], contained in the [[Java package]] hierarchy {{Javadoc:EE|package=javax.servlet|javax/servlet}}, defines the expected interactions of the web container and a servlet.<ref name=r1 />
A {{Javadoc:EE|javax/servlet|Servlet}} is an [[object (computer science)|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 {{Javadoc:EE|package=javax.servlet.http|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 format (Sun)|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 ([[JavaServer Faces|JSF]]) still explicitly uses the servlet technology for the low level request/response handling via the {{Javadoc:EE|javax/faces/webapp|FacesServlet}}. A somewhat older usage is to use servlets in conjunction with JSPs in a pattern called "[[JSP model 2 architecture|Model 2]]", which is a flavor of the [[model–view–controller]].
The current version of Servlet is 3.1.
== History ==
The Servlet1 specification was created by Pavni Diwanji<ref>{{cite web|title=Pavni Diwanji|url=https://www.fosi.org/people/pavni-diwanji/|website=Family Online Safety Institute|accessdate=12 November 2016}}</ref> while she worked at [[Sun Microsystems]], with version 1.0 finalized in June 1997. Starting with version 2.2, the specification was developed under the [[Java Community Process]]. As of June 9, 2015, the current version of the Servlet specification is 3.1.
In his blog on [[java.net]], Sun veteran and [[GlassFish]] lead Jim Driscoll details the history of servlet technology.<ref>{{cite web|url=https://community.oracle.com/blogs/driscoll/2005/12/10/servlet-history|title=Servlet History | community.oracle.com|last=|first=|date=2005-12-10|website=|publisher=Weblogs.java.net|archive-url=|archive-date=|dead-url=|accessdate=2013-06-14}}</ref> [[James Gosling]] first thought of servlets in the early days of [[Java (programming language)|Java]], but the concept did not become a product until Sun shipped the {{clarify-span|''Java Web Server''|date=September 2012}} product. This was before what is now the [[Java Platform, Enterprise Edition]] was made into a specification.
{| class="wikitable"
|+ Servlet API history
! Servlet API version !! Released !! JSR Number !! Platform !! Important Changes
|-
| Servlet 4.0 || [https://jcp.org/en/jsr/detail?id=369 Sep 2017] || 369 || Java EE 8 || [[HTTP/2]]
|-
| Servlet 3.1 || [https://jcp.org/en/jsr/detail?id=340 May 2013] || 340 || Java EE 7 || Non-blocking I/O, HTTP protocol upgrade mechanism ([[WebSocket]])<ref>{{cite web|url=https://blogs.oracle.com/arungupta/entry/what_s_new_in_servlet|title=What's new in Servlet 3.1 ? - Java EE 7 moving forward (Arun Gupta, Miles to go ...)|work=oracle.com|accessdate=22 November 2016}}</ref>
|-
| Servlet 3.0 || [http://www.javaworld.com/javaworld/jw-02-2009/jw-02-servlet3.html December 2009] || 315 || Java EE 6, Java SE 6 || Pluggability, Ease of development, Async Servlet, Security, File Uploading
|-
| Servlet 2.5 || [http://www.javaworld.com/javaworld/jw-01-2006/jw-0102-servlet.html September 2005] || 154 || Java EE 5, Java SE 5 || Requires Java SE 5, supports annotation
|-
| Servlet 2.4 || [http://www.javaworld.com/jw-03-2003/jw-0328-servlet.html November 2003] || 154 || J2EE 1.4, J2SE 1.3 || web.xml uses XML Schema
|-
| Servlet 2.3 || [http://www.javaworld.com/jw-01-2001/jw-0126-servletapi.html August 2001]|| 53 || J2EE 1.3, J2SE 1.2 || Addition of <code>Filter</code>
|-
| Servlet 2.2 || [http://www.javaworld.com/jw-10-1999/jw-10-servletapi.html August 1999]|| 902, 903 || J2EE 1.2, J2SE 1.2 || Becomes part of J2EE, introduced independent web applications in .war files
|-
| Servlet 2.1 || [http://www.javaworld.com/jw-12-1998/jw-12-servletapi.html November 1998] || NA || Unspecified || First official specification, added <code>RequestDispatcher</code>, <code>ServletContext</code>
|-
| Servlet 2.0 || || NA || JDK 1.1 || Part of Java Servlet Development Kit 2.0
|-
| Servlet 1.0 || June 1997|| NA || ||
|}
== Compared with other web application models ==
{{one source|section|date=December 2013}}
The advantages of using servlets are their fast performance and ease of use combined with more power over traditional [[Common Gateway Interface|CGI]] (Common Gateway Interface). Traditional CGI scripts written in Java have a number of performance disadvantages:
* When an HTTP request is made, a new process is created each time the CGI script is called. The overhead associated with process creation can dominate the workload especially when the script does relatively fast operations. Thus, process creation will take more time for CGI script execution. In contrast, for servlets, each request is handled by a separate Java thread ''within'' the web server process, thereby avoiding the overhead associated with forking processes within the [[HTTP daemon]].
* Simultaneous CGI requests will load the CGI script to be copied into memory once per request. With servlets, there is only one copy that persists across requests and is shared between threads.
* Only a single instance answers all requests concurrently. This reduces memory usage and eases the management of persistent data.
* A servlet can be run by a servlet container in a restrictive environment, called a [[Sandbox (computer security)|sandbox]]. This is similar to an [[applet]] that runs in the sandbox of the web browser. This enables restricted use of potentially harmful servlets.<ref name=r1/> CGI programs can of course also sandbox themselves, since they are simply OS processes.
Technologies like [[FastCGI]] and its derivatives (including [[Simple Common Gateway Interface|SCGI]], [[Apache JServ Protocol|AJP]]) do not exhibit the performance disadvantages of CGI, incurred by the constant process spawning. They are, however, roughly as simple as CGI. They are therefore also in contrast with servlets which are substantially more complex.
== Life cycle of a servlet ==
Three methods are central to the life cycle of a servlet. These are <code>init()</code>, <code>service()</code>, and <code>destroy()</code>.
They are implemented by every servlet and are invoked at specific times by the server.
* During initialization stage of the servlet [[Object lifetime|life cycle]], the web container initializes the servlet instance by calling the [http://docs.oracle.com/javaee/7/api/javax/servlet/Servlet.html#init <code>init()</code>] method, passing an object implementing the [http://docs.oracle.com/javaee/7/api/javax/servlet/ServletConfig.html <code>javax.servlet.ServletConfig</code>] interface. This configuration object allows the servlet to access [[Attribute–value pair|name-value]] initialization parameters from the web application.
* After initialization, the servlet instance can service client requests. Each [[HTTP request#Request message|request]] is serviced in its own separate thread. The web container calls the <code>service()</code> method of the servlet for every request. The <code>service()</code> 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 is made for a method that is not implemented by the servlet, the method of the parent class is called, typically resulting in an error being returned to the requester.
* Finally, the web container calls the <code>destroy()</code> method that takes the servlet out of service. The <code>destroy()</code> method, like <code>init()</code>, is called only once in the lifecycle of a servlet.
The following is a typical user scenario of these methods.
# Assume that a user requests to visit a [[Uniform resource locator|URL]].
#* The browser then generates an HTTP request for this URL.
#* This request is then sent to the appropriate server.
# The HTTP request is received by the web server and forwarded to the servlet container.
#* The container maps this request to a particular servlet.
#* The servlet is dynamically retrieved and loaded into the address space of the container.
# The container invokes the <code>init()</code> method of the servlet.
#* This method is invoked only when the servlet is first loaded into memory.
#* It is possible to pass initialization parameters to the servlet so that it may configure itself.
# The container invokes the <code>service()</code> method of the servlet.
#* This method is called to process the HTTP request.
#* The servlet may read data that has been provided in the HTTP request.
#* The servlet may also formulate an HTTP response for the client.
# The servlet remains in the container's address space and is available to process any other HTTP requests received from clients.
#* The <code>service()</code> method is called for each HTTP request.
# The container may, at some point, decide to unload the servlet from its memory.
#* The algorithms by which this decision is made are specific to each container.
# The container calls the servlet's <code>destroy()</code> method to relinquish any resources such as file handles that are allocated for the servlet; important data may be saved to a persistent store.
# The memory allocated for the servlet and its objects can then be garbage collected.
== Example ==
The following example servlet prints how many times its <code>service()</code> method was called.
Note that <code>HttpServlet</code> is a subclass of <code>GenericServlet</code>, an implementation of the <code>Servlet</code> interface.
The <code>service()</code> method of <code>HttpServlet</code> class dispatches requests to the methods <code>doGet()</code>, <code>doPost()</code>, <code>doPut()</code>, <code>doDelete()</code>, and so on; according to the HTTP request. In the example below <code>service()</code> is overridden and does not distinguish which HTTP request method it serves.
<syntaxhighlight lang="java">
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletLifeCycleExample extends HttpServlet {
private int count;
@Override
public void init(final ServletConfig config) throws ServletException {
super.init(config);
getServletContext().log("init() called");
count = 0;
}
@Override
protected void service(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
getServletContext().log("service() called");
count++;
response.getWriter().write("Incrementing the count to " + count);
}
@Override
public void destroy() {
getServletContext().log("destroy() called");
}
}
</syntaxhighlight>
== Container servers ==
The specification for Servlet technology has been implemented in many products. See a list of implementations on the [[Web container]] page.
== References ==
{{Reflist}}
== External links ==
<!-- {{No more links}}
Please be cautious adding more external links.
Wikipedia is not a collection of links and should not be used for advertising.
Excessive or inappropriate links will be removed.
See [[Wikipedia:External links]] and [[Wikipedia:Spam]] for details.
If there are already suitable links, propose additions or replacements on
the article's talk page, or submit your link to the relevant category at
the Open Directory Project (dmoz.org) and link there using {{Dmoz}}.
-->
* [https://www.jcp.org/en/jsr/detail?id=369 JSR 369] - Java servlet 4.0 documentation
* [https://www.jcp.org/en/jsr/detail?id=340 JSR 340] - Java servlet 3.1 documentation
* [https://www.jcp.org/en/jsr/detail?id=315 JSR 315] - Java servlet 3.0 documentation
* [https://www.jcp.org/en/jsr/detail?id=154 JSR 154] - Java servlet 2.4 documentation
* [https://www.jcp.org/en/jsr/detail?id=53 JSR 53] - Java servlet 2.3 documentation
{{Java EE 7}}
{{Web interfaces}}
{{Prone to spam|date=June 2013}}
{{Z148}}
[[Category:Articles with example Java code]]
[[Category:Java enterprise platform|Servlet]]
[[Category:Java platform]]
[[Category:Java specification requests|Servlet]]' |
New page wikitext, after the edit (new_wikitext ) | '{{refimprove|date=February 2014}}
{{Citation style|date=May 2016|details=Violates Wikipedia:External links: "Wikipedia articles may include links to web pages outside Wikipedia (external links), but they should not normally be used in the body of an article."}}
[[File:JSPLife.png|thumb|400px|Life of a JSP file]]
sadsadasdas
A '''Java servlet''' is a [[Java (programming language) |Java]] [[Computer program|program]] that extends the capabilities of a [[Server (computing)|server]]. Although servlets can respond to any types of requests, they most commonly implement applications hosted on [[Web server]]s.<ref>{{cite web
| accessdate = 2011-04-27
| ___location = http://www.webopedia.com/
| publisher = WEBOPEDIA| title = servlet
| quote = A small java program that runs on a server. The term usually refers to a Java applet which runs within a web server environment. This is analogous to a Java applet that runs within a web browser environment.
| url = http://www.webopedia.com/TERM/S/servlet.html}}</ref> Such Web servlets are the [[Java (software platform)|Java]] counterpart to other [[dynamic web page|dynamic Web content]] technologies such as [[PHP]] and [[ASP.NET]].
== Introduction ==
A Java servlet processes or stores a [[Java class]] in [[Java EE]] that conforms to the Java Servlet API,<ref>{{cite web|url=http://docs.oracle.com/javaee/7/api/javax/servlet/Servlet.html|title=Servlet (Java(TM) EE 7 Specification APIs)|work=oracle.com|accessdate=22 November 2016}}</ref> a standard for implementing Java classes that respond to requests. Servlets could in principle communicate over any [[client–server model|client–server]] protocol, but they are most often used with the [[Hypertext Transfer Protocol|HTTP protocol]]. Thus "servlet" is often used as shorthand for "HTTP servlet".<ref name="r1">{{cite web |url=http://www.novocode.com/doc/servlet-essentials/chapter1.html |title=Servlet Essentials - Chapter 1 |work=novocode.com |accessdate=2016-11-22}}</ref> Thus, a [[software developer]] may use a servlet to add [[dynamic web page|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 can maintain [[state (computer science)|state]] in [[session (computer science)|session]] variables across many server transactions by using [[HTTP cookie]]s, or [[URL rewriting]].
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 [[Application programming interface|API]], contained in the [[Java package]] hierarchy {{Javadoc:EE|package=javax.servlet|javax/servlet}}, defines the expected interactions of the web container and a servlet.<ref name=r1 />
A {{Javadoc:EE|javax/servlet|Servlet}} is an [[object (computer science)|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 {{Javadoc:EE|package=javax.servlet.http|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 format (Sun)|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 ([[JavaServer Faces|JSF]]) still explicitly uses the servlet technology for the low level request/response handling via the {{Javadoc:EE|javax/faces/webapp|FacesServlet}}. A somewhat older usage is to use servlets in conjunction with JSPs in a pattern called "[[JSP model 2 architecture|Model 2]]", which is a flavor of the [[model–view–controller]].
The current version of Servlet is 3.1.
== History ==
The Servlet1 specification was created by Pavni Diwanji<ref>{{cite web|title=Pavni Diwanji|url=https://www.fosi.org/people/pavni-diwanji/|website=Family Online Safety Institute|accessdate=12 November 2016}}</ref> while she worked at [[Sun Microsystems]], with version 1.0 finalized in June 1997. Starting with version 2.2, the specification was developed under the [[Java Community Process]]. As of June 9, 2015, the current version of the Servlet specification is 3.1.
In his blog on [[java.net]], Sun veteran and [[GlassFish]] lead Jim Driscoll details the history of servlet technology.<ref>{{cite web|url=https://community.oracle.com/blogs/driscoll/2005/12/10/servlet-history|title=Servlet History | community.oracle.com|last=|first=|date=2005-12-10|website=|publisher=Weblogs.java.net|archive-url=|archive-date=|dead-url=|accessdate=2013-06-14}}</ref> [[James Gosling]] first thought of servlets in the early days of [[Java (programming language)|Java]], but the concept did not become a product until Sun shipped the {{clarify-span|''Java Web Server''|date=September 2012}} product. This was before what is now the [[Java Platform, Enterprise Edition]] was made into a specification.
{| class="wikitable"
|+ Servlet API history
! Servlet API version !! Released !! JSR Number !! Platform !! Important Changes
|-
| Servlet 4.0 || [https://jcp.org/en/jsr/detail?id=369 Sep 2017] || 369 || Java EE 8 || [[HTTP/2]]
|-
| Servlet 3.1 || [https://jcp.org/en/jsr/detail?id=340 May 2013] || 340 || Java EE 7 || Non-blocking I/O, HTTP protocol upgrade mechanism ([[WebSocket]])<ref>{{cite web|url=https://blogs.oracle.com/arungupta/entry/what_s_new_in_servlet|title=What's new in Servlet 3.1 ? - Java EE 7 moving forward (Arun Gupta, Miles to go ...)|work=oracle.com|accessdate=22 November 2016}}</ref>
|-
| Servlet 3.0 || [http://www.javaworld.com/javaworld/jw-02-2009/jw-02-servlet3.html December 2009] || 315 || Java EE 6, Java SE 6 || Pluggability, Ease of development, Async Servlet, Security, File Uploading
|-
| Servlet 2.5 || [http://www.javaworld.com/javaworld/jw-01-2006/jw-0102-servlet.html September 2005] || 154 || Java EE 5, Java SE 5 || Requires Java SE 5, supports annotation
|-
| Servlet 2.4 || [http://www.javaworld.com/jw-03-2003/jw-0328-servlet.html November 2003] || 154 || J2EE 1.4, J2SE 1.3 || web.xml uses XML Schema
|-
| Servlet 2.3 || [http://www.javaworld.com/jw-01-2001/jw-0126-servletapi.html August 2001]|| 53 || J2EE 1.3, J2SE 1.2 || Addition of <code>Filter</code>
|-
| Servlet 2.2 || [http://www.javaworld.com/jw-10-1999/jw-10-servletapi.html August 1999]|| 902, 903 || J2EE 1.2, J2SE 1.2 || Becomes part of J2EE, introduced independent web applications in .war files
|-
| Servlet 2.1 || [http://www.javaworld.com/jw-12-1998/jw-12-servletapi.html November 1998] || NA || Unspecified || First official specification, added <code>RequestDispatcher</code>, <code>ServletContext</code>
|-
| Servlet 2.0 || || NA || JDK 1.1 || Part of Java Servlet Development Kit 2.0
|-
| Servlet 1.0 || June 1997|| NA || ||
|}
== Compared with other web application models ==
{{one source|section|date=December 2013}}
The advantages of using servlets are their fast performance and ease of use combined with more power over traditional [[Common Gateway Interface|CGI]] (Common Gateway Interface). Traditional CGI scripts written in Java have a number of performance disadvantages:
* When an HTTP request is made, a new process is created each time the CGI script is called. The overhead associated with process creation can dominate the workload especially when the script does relatively fast operations. Thus, process creation will take more time for CGI script execution. In contrast, for servlets, each request is handled by a separate Java thread ''within'' the web server process, thereby avoiding the overhead associated with forking processes within the [[HTTP daemon]].
* Simultaneous CGI requests will load the CGI script to be copied into memory once per request. With servlets, there is only one copy that persists across requests and is shared between threads.
* Only a single instance answers all requests concurrently. This reduces memory usage and eases the management of persistent data.
* A servlet can be run by a servlet container in a restrictive environment, called a [[Sandbox (computer security)|sandbox]]. This is similar to an [[applet]] that runs in the sandbox of the web browser. This enables restricted use of potentially harmful servlets.<ref name=r1/> CGI programs can of course also sandbox themselves, since they are simply OS processes.
Technologies like [[FastCGI]] and its derivatives (including [[Simple Common Gateway Interface|SCGI]], [[Apache JServ Protocol|AJP]]) do not exhibit the performance disadvantages of CGI, incurred by the constant process spawning. They are, however, roughly as simple as CGI. They are therefore also in contrast with servlets which are substantially more complex.
== Life cycle of a servlet ==
Three methods are central to the life cycle of a servlet. These are <code>init()</code>, <code>service()</code>, and <code>destroy()</code>.
They are implemented by every servlet and are invoked at specific times by the server.
* During initialization stage of the servlet [[Object lifetime|life cycle]], the web container initializes the servlet instance by calling the [http://docs.oracle.com/javaee/7/api/javax/servlet/Servlet.html#init <code>init()</code>] method, passing an object implementing the [http://docs.oracle.com/javaee/7/api/javax/servlet/ServletConfig.html <code>javax.servlet.ServletConfig</code>] interface. This configuration object allows the servlet to access [[Attribute–value pair|name-value]] initialization parameters from the web application.
* After initialization, the servlet instance can service client requests. Each [[HTTP request#Request message|request]] is serviced in its own separate thread. The web container calls the <code>service()</code> method of the servlet for every request. The <code>service()</code> 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 is made for a method that is not implemented by the servlet, the method of the parent class is called, typically resulting in an error being returned to the requester.
* Finally, the web container calls the <code>destroy()</code> method that takes the servlet out of service. The <code>destroy()</code> method, like <code>init()</code>, is called only once in the lifecycle of a servlet.
The following is a typical user scenario of these methods.
# Assume that a user requests to visit a [[Uniform resource locator|URL]].
#* The browser then generates an HTTP request for this URL.
#* This request is then sent to the appropriate server.
# The HTTP request is received by the web server and forwarded to the servlet container.
#* The container maps this request to a particular servlet.
#* The servlet is dynamically retrieved and loaded into the address space of the container.
# The container invokes the <code>init()</code> method of the servlet.
#* This method is invoked only when the servlet is first loaded into memory.
#* It is possible to pass initialization parameters to the servlet so that it may configure itself.
# The container invokes the <code>service()</code> method of the servlet.
#* This method is called to process the HTTP request.
#* The servlet may read data that has been provided in the HTTP request.
#* The servlet may also formulate an HTTP response for the client.
# The servlet remains in the container's address space and is available to process any other HTTP requests received from clients.
#* The <code>service()</code> method is called for each HTTP request.
# The container may, at some point, decide to unload the servlet from its memory.
#* The algorithms by which this decision is made are specific to each container.
# The container calls the servlet's <code>destroy()</code> method to relinquish any resources such as file handles that are allocated for the servlet; important data may be saved to a persistent store.
# The memory allocated for the servlet and its objects can then be garbage collected.
== Example ==
The following example servlet prints how many times its <code>service()</code> method was called.
Note that <code>HttpServlet</code> is a subclass of <code>GenericServlet</code>, an implementation of the <code>Servlet</code> interface.
The <code>service()</code> method of <code>HttpServlet</code> class dispatches requests to the methods <code>doGet()</code>, <code>doPost()</code>, <code>doPut()</code>, <code>doDelete()</code>, and so on; according to the HTTP request. In the example below <code>service()</code> is overridden and does not distinguish which HTTP request method it serves.
<syntaxhighlight lang="java">
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletLifeCycleExample extends HttpServlet {
private int count;
@Override
public void init(final ServletConfig config) throws ServletException {
super.init(config);
getServletContext().log("init() called");
count = 0;
}
@Override
protected void service(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
getServletContext().log("service() called");
count++;
response.getWriter().write("Incrementing the count to " + count);
}
@Override
public void destroy() {
getServletContext().log("destroy() called");
}
}
</syntaxhighlight>
== Container servers ==
The specification for Servlet technology has been implemented in many products. See a list of implementations on the [[Web container]] page.
== References ==
{{Reflist}}
== External links ==
<!-- {{No more links}}
Please be cautious adding more external links.
Wikipedia is not a collection of links and should not be used for advertising.
Excessive or inappropriate links will be removed.
See [[Wikipedia:External links]] and [[Wikipedia:Spam]] for details.
If there are already suitable links, propose additions or replacements on
the article's talk page, or submit your link to the relevant category at
the Open Directory Project (dmoz.org) and link there using {{Dmoz}}.
-->
* [https://www.jcp.org/en/jsr/detail?id=369 JSR 369] - Java servlet 4.0 documentation
* [https://www.jcp.org/en/jsr/detail?id=340 JSR 340] - Java servlet 3.1 documentation
* [https://www.jcp.org/en/jsr/detail?id=315 JSR 315] - Java servlet 3.0 documentation
* [https://www.jcp.org/en/jsr/detail?id=154 JSR 154] - Java servlet 2.4 documentation
* [https://www.jcp.org/en/jsr/detail?id=53 JSR 53] - Java servlet 2.3 documentation
{{Java EE 7}}
{{Web interfaces}}
{{Prone to spam|date=June 2013}}
{{Z148}}
[[Category:Articles with example Java code]]
[[Category:Java enterprise platform|Servlet]]
[[Category:Java platform]]
[[Category:Java specification requests|Servlet]]' |
Whether or not the change was made through a Tor exit node (tor_exit_node ) | 0 |
Unix timestamp of change (timestamp ) | 1515598395 |