Java Servlet

drivercutInternet and Web Development

Nov 13, 2013 (3 years and 7 months ago)

73 views

Ja
va

Servlet

Servlet adalah sebuah API

(application
Progr. Interface) dl
m pemrograman java yang
mempermudah
developer untuk menambahkan
konten
-
konten dinamis dalam sebuah web
server yang mengimplementasikan platform
java.
Servlet sendiri dibuat un
tuk menandingi
teknologi
-
teknologi konten web yang dinamis
lainnya seperti PHP, CGI, ASP.NET.

Servlet API terdapat didalam
jav
ax.servlet

package
.

Dalam sebuah Web Server, Servlet
berinteraksi dengan sebuah Web Container,
dimana Web Container ini bertanggung jawab
penuh terhadap daur hidup, pemetaan URL
terhadap Servlet tertentu lainnya.

Servlet sendiri sebenarnya adalah sebuah

object
(dari class interface) yang menerima
request
,
kemudian menggenerate
response

berdasarkan
request yang diterima tadi. Sedangkan untuk
spesifik HTTP, disediakan sub
-
class dari
Servlet, yaitu HttpServlet yang didalamnya
terdapat juga obyek untuk manaj
emen session.
Sedangkan daur hidup dari sebuah Servlet sendiri
ada 4
state

yaitu:

1.

Pada saat
startup
, class
-
class Servlet di
letakkan/
load
ed

dalam Web Container..

2.

Web Container kemudian memanggil method
init(). Method ini yang bertugas
menginisialisasi Serv
let dan harus dipanggil
sebelum Servlet melayani
requests
. Method
init() ini hanya dipanggil sekali. Setelah
method init() dipanggil, barulah Servlet
bisa melayani
requests.

3.

Seteiap
request

yang masuk dilayani dan
ditangani oleh
thread

yang berbeda. Web
Co
ntainer memanggil method service() untuk
setiap reqeust yang masuk. Method ini
menetukan jenis
request

yang masuk, kemudian
menentukan method mana yang akan menghandle
request

tersebut.

4.

State terakhir adalah ketika Web
Container memanggil method destroy().

Method
ini menyerupai method init(), hanya
dipanggil sekali, dan menandakan bahwa sudah
tidak ada lagi layanan dari Servlet.

Servle
tContext hanya ada satu di setiap
aplikasi, dan dapat digunakan oleh semua Servlet
yang terlibat di aplikasi tersebut. Di si
si
lain, masing
-
masing Servlet tadi memiliki
ServletConfig sendiri
-
sendiri. ServletConfig ini
menyediakan inisialisasi parameter untuk Servlet
tersebut.


Package javax.servlet

This chapter describes the javax.servlet package.

See:


Description


Interface Summary

Filter

A filter is an object
that performs filtering tasks on either the request
to a resource (a servlet or static content), or on the response from a
resource, or both.


Filters perform filtering in the
doFilter

method.

FilterChain

A FilterChain is an object provided by the servlet container to the
developer giving a view into the invocation chain of a filtered request
for a resource.

FilterConfig

A filter configuration object used by a servlet container to pass
information to a filter during initialization.

RequestDispatcher

Defines an object that receives requests from the client and sends
them to any resource (such as a servlet, HTML file, or JSP file) on the
server.

Servlet

Defines methods that all servlets must implement.

ServletConfig

A servlet configuration object used by a servlet container
to pass
information to a servlet during initialization.

ServletContext

Defines a set of methods that a servlet uses to communicate with its
servlet container, for exam
ple, to get the MIME type of a file, dispatch
requests, or write to a log file.

ServletContextAttributeListener

Implementations of this interface
receive notifications of changes to
the attribute list on the servlet context of a web application.

ServletContextListener

Implementations of this interface re
ceive notifications about changes
to the servlet context of the web application they are part of.

ServletRequest

Defines an object to provide client request informatio
n to a servlet.

ServletRequestAttributeListener

A ServletRequestAttributeListener can be implemented by the
developer interested in being notified of
request attribute changes.

ServletRequestListener

A ServletRequestListener can be implemented by the developer
interested in being notified of requests coming
in and out of scope in a
web component.

ServletResponse

Defines an object to assist a servlet in sending a response to the client.

SingleThreadModel

Deprecated.

As of Java Servlet API 2.4, with no direct replacement.




Class Summary

GenericServlet

Defines a generic, protocol
-
independent servlet.

Ser
vletContextAttributeEvent

This is the event class for notifications about changes to the attributes
of the servlet context of a web application.

ServletContextEvent

This is the event class for notifications about changes to the servlet
context of a web application.

ServletInputStream

Provides an input stream for reading binary data from a client request,
including an efficient
readLine

method for reading data one line at a
time.

ServletOutputS
tream

Provides an output stream for sending binary data to the client.

ServletRequestAttributeEvent

This is the event class for notifications of changes
to the attributes of
ServletRequest in an application.

ServletRequestEvent

Events of this kind indicate lifecycle events for a ServletRequest.

ServletRequestWrapper

Provides a convenient implementation of the ServletRequest interface
that can be subclassed by developers wishing to adapt the request to a
S
ervlet.

ServletResponseWrapper

Provides a convenient implementation of the ServletResponse interface
that can be subclassed by developers wishing to adapt the
response
from a Servlet.




Exception Summary

ServletException

Defines a general exception a servlet can throw when it encounters difficulty.

UnavailableException

Defines an exception that a servlet or filter throws to indicate that it is
permanently or temporarily unavailable.




Package javax.servlet

Description

This chapter describes the javax.servlet package. The chapter includes content that is generated
automatically from javadoc embedded in the actual Java classes and interfaces. This allows the
creation of a single, authoritative, specification

document.

Generic Servlet Interfaces and Classes

The
javax.servlet package

contains a number of classes and interfaces that describe and define
the contracts between a servlet class and the runtime environment provided for an instance of
such a class by
a conforming servlet container.

The
Servlet

interface is the central abstraction of the servlet API. All servlets implement this
interface either directly, or more commonly, by extending a class that implements the interface.
The two classes in the servle
t API that implement the
Servlet

interface are
GenericServlet

and
HttpServlet

. For most purposes, developers will extend
HttpServlet

to implement their servlets
while implementing web applications employing the HTTP protocol.

The basic
Servlet

interface d
efines a
service

method for handling client requests. This method is
called for each request that the servlet container routes to an instance of a servlet.

The javax.servlet package





Life Cycle Servlet:


The Life Cycle of a Servlet

Each servlet has the same life cycle:



A server loads and initializes the servlet






The servlet handles zero or more client requests






The server removes the servlet

(some servers do this step only when they shut down)

Initializing a Servlet

When a server loads a servlet, the server runs the servlet's
init

method. Initialization completes
before client requests are handled and before the servlet is destroyed.

Even tho
ugh most servlets are run in multi
-
threaded servers, servlets have no concurrency issues
during servlet initialization. The server calls the
init

method once, when the server loads the
servlet, and will not call the init method again unless the server is r
eloading the servlet. The
server can not reload a servlet until after the server has destroyed the servlet by calling the
destroy

method.





Interacting with Clients

After initialization, the servlet is able to handle client requests. This part of the se
rvlet life cycle
was handled in the
previous lesson
.





Destroying a Serv
let

Servlets run until the server are destroys them, for example, at the request of a system
administrator. When a server destroys a servlet, the server runs the servlet's
destroy

method.
The method is run once; the server will not run that servlet again u
ntil after the server reloads
and reinitializes the servlet.

When the
destroy

method runs, another thread might be running a service request. The
Handling Servi
ce Threads at Servlet Termination

lesson shows you how to provide a clean
shutdown when there could be long
-
running threads still running service requests.