Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Jakarta Servlet
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Introduction == A '''Jakarta Servlet''' is a [[Java class]]{{sfn | Murach | Urban | 2014 | loc=§2 Essential servlet and JSP skills - Perspective - Summary | pp=170-171}} in [[Jakarta EE]] that conforms to the Jakarta Servlet API,<ref>{{cite web|url=https://docs.oracle.com/javaee/7/api/javax/servlet/Servlet.html |title=Servlet (Java(TM) EE 7 Specification APIs)|work=oracle.com|access-date=2018-07-25}}</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 [[Hypertext Transfer Protocol|HTTP]]. In principle, any servlets can extend the {{Javadoc:EE|javax/servlet|GenericServlet}} class; however, realistically speaking, all servlets extend the {{Javadoc:EE|javax/servlet/http|HttpServlet}} class.{{sfn | Murach | Urban | 2014 | loc=§2 Essential servlet and JSP skills - How to create and map a servlet | pp=128-129}} 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 |archive-url=https://web.archive.org/web/20171218173656/http://www.novocode.com/doc/servlet-essentials/chapter1.html |archive-date=2017-12-18}}</ref> Thus, a servlet can be used to add [[dynamic web page|dynamic content]] to a [[web server]] using the [[Java platform]].{{sfn | Murach | Urban | 2014 | loc=§1 Get started right - The servlet for back-end processing | pp=40-42}} The generated content is commonly [[HTML]], but may be other data such as [[XML]] and more commonly, [[JSON]]. The Jakarta Servlet API has, to some extent, been superseded{{Citation needed|reason=Citation is need for the claim where a technology is said to be superseded by other technologies|date=June 2023}} by two standard Java technologies for web services: * the [[Jakarta RESTful Web Services]] (JAX-RS 2.0) useful for AJAX, JSON and REST services, and * the [[Jakarta XML Web Services]] (JAX-WS) useful for [[SOAP]] [[Web Service]]s. 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 <code>Servlet</code> 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 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 /> 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]].{{sfn | Murach | Urban | 2014 | loc=§2 Essential servlet and JSP skills | p=87}} There are several ways of creating a servlet and using URL mapping with 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}} A [[web container]] is required for deploying and running a servlet. A web container (also known as a servlet container) is essentially the component of a web server that interacts with the servlets.{{sfn | Murach | Urban | 2014 | loc=§2 Essential servlet and JSP skills - Perspective - Summary | pp=170-171}} 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. 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 Faces|Faces]]) 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]].
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Jakarta Servlet
(section)
Add topic