Java Servlet Intro

indexadjustmentInternet και Εφαρμογές Web

13 Νοε 2013 (πριν από 3 χρόνια και 9 μήνες)

86 εμφανίσεις

Java Servlet Intro

Basic HTTP Servlet

$Catalina_Home/webapps/ROOT/WEB
-
INF/classes/TestingServlet

$Catalina_Home/webapps/ROOT/WEB
-
INF/web.xml



JSP & Servlet APIs



The Servlet Interface

javax.servlet

Interface Servlet


(Every servlet must implement this Interface, directly or indirectly.)


Defines methods that all servlets must implement.

A servlet is a small Java program that runs within a Web server. Servlets receive and respond to
requests from Web clients, usually across HTTP, the HyperText Transfer Protocol.

To implement this interface, you can write a generic servlet that extends
javax.servlet.GenericServlet

or an HTTP servlet that extends
javax.servlet.http.HttpServlet
.

This interface defines methods to initialize a servlet, to service requests, and to remove a servlet from
the server. These are known as
life
-
cycle methods

and are called in the following sequence:

1.
The servlet is constructed, then initialized with the
init

method.

2.
Any calls from clients to the
service

method are handled.

3.
The servlet is taken out of service, then destroyed with the
destroy

method, then garbage
collected and finalized.



In addition to the life
-
cycle methods, this interface provides the
getServletConfig

method,
which the servlet can use to get any startup information, and the
getServletInfo

method,
which allows the servlet to return basic information about itself, such as author, version, and
copyright.

See Also:


GenericServlet
,
HttpServlet


Methods for Servlet Interface



void destroy()



Called by the servlet container to indicate to a servlet that the servlet is
being taken out of service.


ServletConfig getServletConfig()


Returns a ServletConfig object, which contains initialization and startup
parameters for this servlet.


java.lang.String getServletInfo()


Returns information about the servlet, such as author, version, and
copyright.


void init(ServletConfig config)


Called by the servlet container to indicate to a servlet that the servlet is
being placed into service.


void service(ServletRequest req, ServletResponse res)


Called by the servlet container to allow the servlet to respond to a request.


Servlet’s Life Cycle


init()

init() method


called exactly once, when servlet instantiated


must complete successfully before receive any requests


can override to write your own initialization code (load db,
init vars, etc)


public void init(ServletConfig config) throws
ServletException


container passes config object (values from web.xml for app)


servlet container cannot place servlet into service if throws
exception or does not return in server specified time

Servlet’s Life Cycle


service()

service() method


called by servlet container after the init method to allow
response to a request


public void service(ServletRequest request,
ServletResponse response) throws
ServletException,
java.io.IOException


container passes the request and response objects. Request
contains client’s request and response contains the
servlet’s response to that request.

Servlet’s Life Cycle


destroy()

destroy() method


called by container before removing a servlet instance
from service.


public void destroy()


normally done when container shutdown or needs to free
memory.


gives servlet an opportunity to clean up any resources
being held


Basic HTTP Servlet

$Catalina_Home/webapps/ROOT/WEB
-
INF/classes/TestingServlet

$Catalina_Home/webapps/ROOT/WEB
-
INF/web.xml



Application Directory Structure

install_dir


webapps



ROOT







Demos




Testing.html




Testing.jsp




WEB_INF





web.xml





classes






TestingServlet.java






TestingServlet.class






PrimitiveServlet.java






PrimitiveServlet.class






ConfigDemoServlet.java






ConfigDemoServlet.class









web.xml



Servlet Life Cycle



ServletConfig



Where from?? Ans: web.xml



ServletContext

(environment where servlet runs)



Sharing Info Between Servlets

(Any object bound into a context is available to other servlets in the same web app.)



Sharing (cont.)



AttributeSetter

+

ContextDemo



Attributes passed
between servlets in
same web app.


Request from Client



Request

DemoServlet





ServletRequest

ServletResponse

PrintWriter

Results of ResponseDemoServlet





GenericServlet
Wrapper Class

javax.servlet.http


HttpServlet extends the GenericServlet


Much more convenient to use


Richer set of members


Eg., support for cookies and session management


HttpServlet class has doXxx methods


doGet & doPost


Interfaces equivalent to ServletRequest and
ServletResponse (HttpServletRequest &
HttpServletResponse)

doGet &
doPost





HttpServletRequest
Interface