Jakarta Servlet: Difference between revisions

Content deleted Content added
→‎Introduction: Add link to javadoc
(5 intermediate revisions by 5 users not shown)
Line 44:
The package {{Javadoc:EE|package=javax.servlet.http|javax/servlet/http}} defines [[HTTP]]-specific subclasses of the <code>GenericServlet</code>. This package includes session management objects that track multiple requests and responses between the web server and a client.
 
Servlets can maintain [[state (computer science)|state]] in [[session (computer science)|session]] variables across many server transactions by using [[HTTP cookie]]s, or [[URL mapping]]. The application can keep track of session and cookies by using sessions and cookies.{{sfn | Murach | Urban | 2014 | loc=§2 Essential servlet and JSP skills | p=87}}. There are several ways forof creating a servlet and using URL mapping forwith a servlet. Before servlet 3.0 specification (Tomcat 7.0), configuring the web.xml to map a servlet to a URL was the only option. For applications using the servlet 3.0 specification or later, the <code>@WebServlet</code> annotation can be used to map any servlet to one or more URL patterns.
 
Servlets may be packaged in a [[WAR (file format)|WAR file]] as a [[web application]].{{sfn | Murach | Urban | 2014 | loc=§1 Get started right - Other skills for working with web applications| p=74}}
Line 52:
Servlets can be generated automatically from [[Jakarta Server Pages]] (JSP) by the [[Jakarta Server Pages compiler]]. The difference between servlets and JSP is that servlets typically embed HTML inside Java code, while JSPs embed Java code in HTML. In general, when using JSPs, embedding Java code in JSP is considered bad practice.{{sfn | Murach | Urban | 2014 | loc=§1 Get started right - The JSP for the second page | pp=46-47}} Instead, a better approach would be to move the back-end logic from the JSP to the Java code in the <code>Servlet</code>.{{sfn | Murach | Urban | 2014 | loc=§1 Get started right - The JSP for the second page | pp=46-47}} This ensures that the <code>Servlet</code> is only responsible for processing, and the JSP is only responsible for presenting the HTML,{{sfn | Murach | Urban | 2014 | loc=§1 Get started right - The JSP for the second page | pp=46-47}} allowing for a clear [[separation of concerns]] and conformance to the [[single-responsibility principle]].
 
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 Jakarta EE ([[Jakarta Server Faces|JSFFaces]]) 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]].
 
== History ==
The Java Servlet API was first publicly announced at the inaugural [[JavaOne]] conference in May 1996.<ref>{{cite news |last1=Freedman |first1=Matt |date=1996-06-26 |df=mdy |title=JavaOne conference report |url=https://www.javaworld.com/article/2077172/java-se/javaone-conference-report.html |website=[[JavaWorld]] |access-date=2018-07-25 |archive-date=2018-07-26 |archive-url=https://web.archive.org/web/20180726071907/https://www.javaworld.com/article/2077172/java-se/javaone-conference-report.html |url-status=dead }}</ref><ref>{{cite conference |last1=Diwanji |first1=Pavani |last2=Connelly |first2=Dave |last3=Wagle |first3=Prasad |date=1996-05-29 |df=mdy |chapter=Java Server and Servlets |title=Servers and Server Extensions |conference=[[JavaOne]] 1996 |chapter-url=http://java.sun.com:80/javaone/javaone96/pres/ServExt.pdf |archive-url=https://web.archive.org/web/20000816161328/http://java.sun.com/javaone/javaone96/pres/ServExt.pdf |archive-date=2000-08-16 |access-date=2020-02-01 |url-status=live }}</ref> About two months after the announcements at the conference, the first public implementation was made available on the JavaSoft website. This was the first alpha of the Java Web Server (JWS; then known by its codename ''Jeeves'')<ref>{{cite news |last1=Chang |first1=Phil Inje | date=1997-07-01 |df=mdy |title=Interview: The Java Web Server team gives you the skinny |url=https://www.javaworld.com/article/2076980/interview--the-java-web-server-team-gives-you-the-skinny.html |website=[[JavaWorld]] |access-date=2018-07-25 |archive-date=2018-07-26 |archive-url=https://web.archive.org/web/20180726071912/https://www.javaworld.com/article/2076980/interview--the-java-web-server-team-gives-you-the-skinny.html |url-status=dead }}</ref> which would eventually be shipped as a product on June 5, 1997.<ref>{{cite news |last1=Chang |first1=Phil Inje | date=1997-06-01 |df=mdy |title=Java Web Server ships! |url=https://www.javaworld.com/article/2076965/java-web-server-ships-.html |website=[[JavaWorld]] |access-date=2018-07-25 |archive-date=2018-07-26 |archive-url=https://web.archive.org/web/20180726071909/https://www.javaworld.com/article/2076965/java-web-server-ships-.html |url-status=dead }}</ref>
 
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 &#124; community.oracle.com|date=2005-12-10|publisher=Weblogs.java.net|access-date=2013-06-14|archive-date=2020-08-15|archive-url=https://web.archive.org/web/20200815055946/https://community.oracle.com/blogs/driscoll/2005/12/10/servlet-history|url-status=dead}}</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 December 1996 when Sun shipped JWS.<ref name="Hunter200003">{{cite conference |last1=Hunter |first1=Jason |date=March 2000 |chapter=Servlet Timeline |title=Beyond Java Servlet Programming |conference=O'Reilly Conference on Java |chapter-url=ftp://ftp.ora.com/pub/conference/java/Hunter_J/orabeyond.ppt |publisher=[[O'Reilly Media]] }}{{Dead link|date=April 2024 |bot=InternetArchiveBot |fix-attempted=yes }}</ref><ref>{{cite web |title=Java Web Server |url=http://jserv.javasoft.com:80/products/java-server/webserver/index.html |archive-url=https://web.archive.org/web/19980111053338/http://jserv.javasoft.com/products/java-server/webserver/index.html |archive-date=1998-01-11 |website=Javasoft |publisher=[[Sun Microsystems]] |access-date=2020-02-01 |url-status=live }}</ref><ref>{{cite web |title=Java Web Server(tm) |url=http://sun.com:80/software/jwebserver/index.html |archive-url=https://web.archive.org/web/20020206000714/http://sun.com/software/jwebserver/index.html |archive-date=2002-02-06 |publisher=[[Sun Microsystems]] |access-date=2020-02-01 |url-status=live }}</ref> This was before what is now the [[Jakarta EE]] was made into a specification.
 
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|access-date=12 November 2016|archive-date=26 July 2018|archive-url=https://web.archive.org/web/20180726071809/https://www.fosi.org/people/pavni-diwanji/|url-status=dead}}</ref><ref>{{cite patent |country=US |number=5928323 |status=patent |title=Apparatus and method for dynamically generating information with server-side software objects |pubdate=1999-07-27 |gdate=1999-07-27 |fdate=1997-03-28 |pridate=1996-05-30 |inventor1-last=Gosling |inventor1-first=James A. |inventorlink1=James Gosling |inventor2-last=Diwanji |inventor2-first=Pavni |inventorlink2= |inventor3-last=Connelly |inventor3-first=David W. |inventorlink3= |assign1=[[Sun Microsystems]] |class= |url=}}</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]].
Line 157:
}
response.getWriter().write("Incrementing the count to " + localCounter); // accessing a local variable
response.getWriter().flush(); // flush response
}
 
Line 170 ⟶ 171:
 
There are also other types of servlet containers such as those for SIP servlets, e.g., [[SailFin]].
 
== See also ==
* [[Apache JServ Protocol]] (AJP)
 
== Citations ==