servlet lifecycle lec 28. servlet life cycle initialize service destroy time
TRANSCRIPT
Servlet LifecycleServlet Lifecycle
Lec 28Lec 28
Servlet Life CycleServlet Life Cycle
InitializeInitialize ServiceService DestroyDestroy
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)
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()
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.
EE557: Server-Side Development
Life Cycle: ServiceLife Cycle: Service
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
EE557: Server-Side Development
Servlet Life Cycle SummaryServlet Life Cycle Summary
Java ServletsJava Servlets
Form DataForm Data
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..
EE557: Server-Side Development
Html Form & ServletHtml Form & Servlet
• HTML is Graphical User Interface (GUI) for a servlet
Example : Reading Example : Reading two explicit two explicit parametersparameters
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.
index.htmlindex.html
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)
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)
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)
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"
Using NetBeans4.1Using NetBeans4.1