Saturday, January 10, 2009

Java Servlet Tutorial 1

Java Servlet Tutorial 1

Servlets are Java programs that run on Web or application servers, acting as a middle

layer between requests coming from Web browsers or other HTTP clients and databases or applications on the HTTP server.

***************************************************************

A Servlet’s Job

Advantages

Efficient, Convenient, Powerful, Portable, Secure, Inexpensive

Lightweight threads instead of OS threads created

Single copy of code brought into memory for all threads versus per thread

Data (session state) can be stored across threads within servlet container

Java is portable and secure

Requires little expense once servlet container integrated with web server

***************************************************************

Servlet Lifecycle

A servlet is accessed through a URL.

When a request for a particular servlet is received, the Java Server invokes the servlet as a separate thread of execution.

Once started, it can receive environment information as well as client query information.

It processes the request and returns its data, formatted as HTML, to be included in the body of the HTTP response generated by the Java Server.

The servlet is then destroyed by the Java Server.

***************************************************************

Servlet Lifecycle
(Creation)

Single instance created

init() method called

You can override init() in your subclass of HttpServlet to do some initial code....

init() is NOT called again on further requests

***************************************************************

Servlet Lifecycle
(Service Method)

On each request, the server spawns a new thread and calls service()

service() checks HTTP request type and calls appropriate doXXXX (Get, Post, Put...)

don't override service (unless you really know what you're doing)

***************************************************************

Servlet Lifecycle
(doGet(), doPost())

Real meat of the web app is here

doPost() can call doGet(), or viceversa

***************************************************************

Servlet uses

can process data which was POSTed over HTTPS using an HTML FORM, passing data such as a purchase order (with credit card data).

servlets handle multiple requests concurrently: requests can be synchronized with each other to support collaborative applications such as on-line conferencing.

servlet can forward requests other servers.

balance load among several servers which mirror same content.


***************************************************************

No comments:

Post a Comment