Servlet API - Free Stuff Jamaica

fortunabrontideInternet et le développement Web

13 nov. 2013 (il y a 4 années et 10 mois)

85 vue(s)

SERVLETS



Servlets are designed to work within a
request/response processing model. In a
request/response model, a client sends a request
message to a server and the server responds by
sending back a reply message. We can say Servlets
are Server_side App
lets.


The Java Servlet API includes several Java
interfaces and fully defines the link between a
hosting server and servlets. The Servlet API is
defined as an extension to the standard JDK.
JDK extensions are packaged under
javax
--
the
root of the Java ext
ension library tree. The
Java Servlet API contains the following
packages:



Package
javax.servlet




Package
javax.servlet.http


Servlets are a powerful addition to the Java
environment. They are fast, safe, reliable, and 100%
pure Java. The server handles the network
connections, protocol negotiation, class loading, and
more; all
of this work does not need to be replicated!
And, because servlets are located at the middle tier,
they are positioned to add a lot of value and
flexibility to a system.

Servlets offer several advantages over other interfaces to
communicate with the server

such as CGI:


1.

Performance is significantly better. Servlets execute
within the address space of a Web server. Creating a
separate process to handle client request isn’t
necessary.


2.

Servlets are platform
-
independent, because they are
written in Java.


3.

T
he Java Security Manager on the server enforces a
set of restrictions to protect the resources on a server
machine.


4.

The full functionality of the Java class libraries is
available to a servlet. It can communicate with
applets, databases or other softwa
re via the sockets
and RMI mechanisms.


The life cycle of a Servlet:



The life between init() and destroy() methods of servlet is life
cycle of a servlet.


Three methods are central to the life cycle of a servlet:


1.init()

2.sevice()

3.destroy()



When
these methods are called? (or) How servlets get executed?


First, assume that a user enters a Uniform Resource Locator(URL)
to a Web browser. The browser then generates an HTTP request
for this URL and sends it to the appropriate server.


Second, this HT
TP request is received by the Web server. The
server maps this request to a particular servlet. The servlet is
dynamically retrieved and loaded into the address space of the
server.


Third, the server invokes the init() method of the servlet. This
metho
d is invoked only when the servlet is first loaded into
memory.


Fourth, the server invokes the servlet’s service() method, which is
called to process the HTTP request. The servlet will read the data
provided by the HTTP request, and may also formulate
an HTTP
response for the client. The servlet remains in the server’s address
space and is available to process any other HTTP requests received
from clients. The service() method is called for each HTTP
request.


Finally, the server may decide to unload
the servlet from its
memory. The server calls the destroy() 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 obje
cts can then be garbage
-
collected.






Installing Servlets

In order to test a servlet, two steps are required:

1.

Install the servlet in a hosting server

2.

Request a servlet's service via a client request



Using
servletrunner

For both JDK 1.1 and the Java
2 platform, you need to install the
Java Servlet Development Kit (JSDK). For a Windows machine,
the default location of Version 2 of the JSDK is c:
\
jsdk2.0. The
directory c:
\
jsdk2.0
\
bin contains servletrunner.exe. Set path to this
directory so that it
includes this directory. The directory
c:
\
jsdk2.0
\
lib contains jsdk.jar. This jar file contains the classes
and interfaces that are needed to build servlets. Update your
classpath environment variable so that it includes
c:
\
jsdk2.0
\
lib
\
jsdk.jar.




Usin
g Java Web Server

Set class path to
c:
\
javawebserver2.0
\
lib
\
servlet.jar;

To run JWS from the
c:
\
JavaWebServer1.1
\
bin

directory,
type in the
httpd

command.



Servlets are installed by moving them to the
c:
\
JavaWebServer1.1
\
servlets

directory.


To shut down
the JWS, press <Control>+C in the
command window. The server prints a message to
the console when it has finished shutting down.



Servlet API


The Java Servlet API defines the interface between
servlets and servers. This API is packaged as a
standard ext
ension to the JDK under
javax
:



Package
javax.servlet




Package
javax.servlet
.http



Package hierarchy:


1.


java.lang.Object





javax.servlet.GenericServlet




javax.servlet.http.HttpServlet


2.

java.io.InputStream


javax.servlet.ServletInputStream


3.

java.io.OutputStream


javax.servlet.ServletOutputStream


Generic Se
rvlets:


1.

It is a general Servlet.

2.

The specific protocl servlets are extended from the Generic
Servlets. Therefore the http Servlets are extended from
Generic Servlets.






javax.servlet: General Servlet Support

Servlet

An interface that defines communication
between a web server and a servlet. This
interface defines the
i
nit()
,
service()
, and
destroy()

methods (and a few ot
hers).

ServletConfig

An interface that describes the
configuration parameters for a servlet.
This is passed to the servlet when the
web server calls its
init()

method. Note
that the servlet should save the
reference to the
Ser
vletConfig

object, and
define a
getServletConfig()

method to return
it when asked. This interface defines how
to get the initialization parameter
s for
the servlet and the context under which
the servlet is running.

ServletContext

An interface that describes how a servlet
can get information about the ser
ver in
which it is running. It can be retrieved
via the
getServletContext()

method of the
ServletConfig

object.

ServletRequest

An interface that describes how to get
information about a client request.

ServletResponse

An interface that describes how to pass
information back to the client.

GenericServlet

A base servlet implementation. It takes
care of saving the
ServletConfig

object
reference, and provides several methods
that delegate their functionality to the
ServletConfig

object. It also provides a
dummy implementat
ion for
init()

and
destroy()
.

ServletInputStream

A subclass of
InputSt
ream

used for reading
the data part of a client's request. It
adds a
readLine()

method for convenience.

ServletOutputStream

An
OutputStream

to which responses for the
client are written.

ServletException

Should be thrown when a servlet
problem is encountered.

Unavai
lableException

Should be thrown when the servlet is
unavailable for some reason.


javax.servlet.http: Support for HTTP Servlets

HttpServletRequest

A
subclass of
ServletRequest

that defines
several methods that parse HTTP request
headers.

HttpServletResponse

A subclass of
ServletResponse

that provides
access and interpretation of HTTP status
codes and header info
rmation.

HttpServlet

A subclass of
GenericServlet

that provides

automatic separation of HTTP request by
method type. For example, an HTTP
GET

request will be processed by the
service()

method and passed to a
doGet()

method.

HttpUtils

A class that provides assis
tance for
parsing HTTP
GET

and
POST

requests.




Important Methods of HttpServlet class:



doPost(HttpServletRequest req, HttpServletResponse res)


doGet(HttpServletRequest res,HttpServletResponse res)



A servlet's
doGet()

method should



Read request data, such as input
parameters



Set response headers (length, type, and
encoding)



Write the response data


A servlet's
doPost()

method should be overridden when you
need to process an HTML form posting or to handle a large
amount of data being sent by a client.






How to execute Servlets usi
ng servletrunner?



1.To start the start servlet runner utility give the following
command in he command prompt:


servletrunner <path>


Where path is the path where java class files resides.


2.Compile the code and place the class file in the above
sp
ecified path.


3.open the browser and give the following url:


http://localhost:8080/servlet/<java

class file name>



How to execute Servlets using
JavaWebServer?



1.Edit a java servlet file
and save it





eg: c:
\
java
\
servlets
\
cyberbooks.java


2.Edit autoexec.bat file and set the classpath as(if you have
not set the classpath (before)


set
classpath=%classpath%;c:
\
javawebserver2.0
\
lib
\
servlet.jar


3.Compile and save the class file in the s
ervlets directory of
Javawebserver2.0


eg: c:
\
java
\
servlets
\
javac cyberbooks.java

d
c
\
javawebserver2.0
\
servlets


4.Edit html file to call this servlet class file and place it in
public_html directory of JavaWebsServer2.0.(if you are
calling thro
ugh html file).


5.Start the server from c:
\
javawebserver2.0
\
bin by giving
httpd


6.Open the browser and give the following url to call the
servlet:



if through html file:
http://localhost:8080/<html

file na
me>


if calling the servlet directly:
http://localhost:8080/servlet/<java

class file name>









Reading Initialization Parameters:


You can provide initialisation parameters to a servlet. These c
an
be used to open files, create database connections, or perform
other acitons. This information can be accessed in two ways:


1.The init() method declared by the Servlet interface receives a
ServletConfig object as the argument. The object provides
me
thods that enable you to read the initialisation parameters.


2.The getServletConfig() method declared by the Servlet interface
returns a ServletConfig object.


How to call initialisation Parameters using JWS?


Admin/manage/servlets/add/in add new servlets

window type
servlet name and its corresponding class file
name/add/properties/add/type the name of the parameters and its
corresponding values and click add and repeat for all
parameters/load.