servlet lifecycle lec 28. servlet life cycle initialize service destroy time

19
Servlet Lifecycle Servlet Lifecycle Lec 28 Lec 28

Upload: melina-oneal

Post on 12-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Servlet Lifecycle Lec 28. Servlet Life Cycle  Initialize  Service  Destroy Time

Servlet LifecycleServlet Lifecycle

Lec 28Lec 28

Page 2: Servlet Lifecycle Lec 28. Servlet Life Cycle  Initialize  Service  Destroy Time

Servlet Life CycleServlet Life Cycle

InitializeInitialize ServiceService DestroyDestroy

Time

Page 3: Servlet Lifecycle Lec 28. Servlet Life Cycle  Initialize  Service  Destroy Time

Life Cycle: InitializeLife Cycle: Initialize

Executed once, when the servlet gets loaded Executed once, when the servlet gets loaded for the first timefor the first time

Not called for each client Not called for each client

requestrequest

Put your initialization code Put your initialization code

here.(No constructor available)here.(No constructor available)

Page 4: Servlet Lifecycle Lec 28. Servlet Life Cycle  Initialize  Service  Destroy Time

Life Cycle: ServiceLife Cycle: Service

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

Page 5: Servlet Lifecycle Lec 28. Servlet Life Cycle  Initialize  Service  Destroy Time

Life Cycle: ServiceLife Cycle: Service

service()service() checks HTTP request type and checks HTTP request type and calls calls doGet()doGet() or or doPost()doPost()

OverrideOverride doGet()doGet() oror doPost()doPost()to provide to provide desired behaviour.desired behaviour.

Page 6: Servlet Lifecycle Lec 28. Servlet Life Cycle  Initialize  Service  Destroy Time

EE557: Server-Side Development

Life Cycle: ServiceLife Cycle: Service

Page 7: Servlet Lifecycle Lec 28. Servlet Life Cycle  Initialize  Service  Destroy Time

Life Cycle: DestroyLife Cycle: Destroy

destroy()destroy() method is called only once method is called only once Call takes place whenCall takes place when

Application is stoppedApplication is stopped Servlet container shuts downServlet container shuts down

Allows resources to be Allows resources to be

freedfreed

Page 8: Servlet Lifecycle Lec 28. Servlet Life Cycle  Initialize  Service  Destroy Time

EE557: Server-Side Development

Servlet Life Cycle SummaryServlet Life Cycle Summary

Page 9: Servlet Lifecycle Lec 28. Servlet Life Cycle  Initialize  Service  Destroy Time

Java ServletsJava Servlets

Form DataForm Data

Page 10: Servlet Lifecycle Lec 28. Servlet Life Cycle  Initialize  Service  Destroy Time

How client sends data?How client sends data?

When a user submits a browser request to a When a user submits a browser request to a web server, it sends two categories of data:web server, it sends two categories of data: Form DataForm Data: Data that the user explicitly typed into : Data that the user explicitly typed into

an HTML form.an HTML form. For example: registration information.For example: registration information.

HTTP Request Header DataHTTP Request Header Data: Data that is : Data that is automatically appended to the HTTP Request automatically appended to the HTTP Request from the client.from the client. For example: cookies, browser type, browser IP For example: cookies, browser type, browser IP

addressaddress..

Page 11: Servlet Lifecycle Lec 28. Servlet Life Cycle  Initialize  Service  Destroy Time

EE557: Server-Side Development

Html Form & ServletHtml Form & Servlet

• HTML is Graphical User Interface (GUI) for a servlet

Page 12: Servlet Lifecycle Lec 28. Servlet Life Cycle  Initialize  Service  Destroy Time

Example : Reading Example : Reading two explicit two explicit parametersparameters

Page 13: Servlet Lifecycle Lec 28. Servlet Life Cycle  Initialize  Service  Destroy Time

Form ExampleForm Example

Our first example consists of one HTML Our first example consists of one HTML page (index.html), and one servlet page (index.html), and one servlet (FormServlet.java).(FormServlet.java).

The HTML page contains two form The HTML page contains two form parameters: parameters: firstnamefirstname, , surnamesurname

The Servlet extracts these specific The Servlet extracts these specific parameters and echoes them back to the parameters and echoes them back to the browser after appending Hello.browser after appending Hello.

Page 14: Servlet Lifecycle Lec 28. Servlet Life Cycle  Initialize  Service  Destroy Time

index.htmlindex.html

Page 15: Servlet Lifecycle Lec 28. Servlet Life Cycle  Initialize  Service  Destroy Time

EE557: Server-Side Development

<html> <head> <title> Reading Two Parameters </title> </head>

<body>

<H2>Please fill out this form:</H2>

<FORM METHOD="GET" ACTION="http://localhost:8080/paramapp/formservlet" NAME="myform"> <BR> Firstname: <INPUT TYPE = “text” NAME="firstname"> <BR> Surname: <INPUT TYPE = “text” NAME="surname"> ………..

Form Example Form Example (using GET)(using GET)

Page 16: Servlet Lifecycle Lec 28. Servlet Life Cycle  Initialize  Service  Destroy Time

EE557: Server-Side Development

<BR>

<INPUT TYPE="submit" value="Submit Form"> <INPUT TYPE="reset" value="Reset">

</FORM>

</body> </html>

Form Example cont.Form Example cont. (using GET) (using GET)

Page 17: Servlet Lifecycle Lec 28. Servlet Life Cycle  Initialize  Service  Destroy Time

EE557: Server-Side Development

import java.io.*; import javax.servlet.*; import javax.servlet.http.*;

public class MyServlet extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException

{ String fName = req.getParameter(“firstname”); String sName = req.getParameter(“surname”);

PrintWriter out = res.getWriter();

out.println("Hello: " + fName + “ “ +sName "); out.close();

} }

<input TYPE=“”text NAME=“firstname”>

FormServlet.java FormServlet.java (using GET)(using GET)

Page 18: Servlet Lifecycle Lec 28. Servlet Life Cycle  Initialize  Service  Destroy Time

web.xmlweb.xml

<?xml version="1.0" encoding="ISO-8859-1"?><?xml version="1.0" encoding="ISO-8859-1"?>

<web-app><web-app>

<servlet><servlet> <servlet-name><servlet-name>FormServletFormServlet</servlet-name></servlet-name> <servlet-class><servlet-class>MyServletMyServlet</servlet-class></servlet-class> </servlet></servlet>

<servlet-mapping><servlet-mapping> <servlet-name><servlet-name>FormServletFormServlet</servlet-name></servlet-name> <url-pattern><url-pattern>/formservlet/formservlet</url-pattern></url-pattern> </servlet-mapping></servlet-mapping>

</web-app></web-app>

ACTION="http://localhost:8080/paramapp/formservlet"

Page 19: Servlet Lifecycle Lec 28. Servlet Life Cycle  Initialize  Service  Destroy Time

Using NetBeans4.1Using NetBeans4.1