Presentation is loading. Please wait.

Presentation is loading. Please wait.

Servlet Lifecycle Lec 28. Servlet Life Cycle  Initialize  Service  Destroy Time.

Similar presentations


Presentation on theme: "Servlet Lifecycle Lec 28. Servlet Life Cycle  Initialize  Service  Destroy Time."— Presentation transcript:

1 Servlet Lifecycle Lec 28

2 Servlet Life Cycle  Initialize  Service  Destroy Time

3 Life Cycle: Initialize  Executed once, when the servlet gets loaded for the first time  Not called for each client request  Put your initialization code here.(No constructor available)

4 Life Cycle: Service  On each request, the server spawns a new thread and calls service()

5 Life Cycle: Service  service() checks HTTP request type and calls doGet() or doPost()  Override doGet() or doPost() to provide desired behaviour.

6 EE557: Server-Side Development Life Cycle: Service

7 Life Cycle: Destroy  destroy() method is called only once  Call takes place when  Application is stopped  Servlet container shuts down  Allows resources to be freed freed

8 EE557: Server-Side Development Servlet Life Cycle Summary

9 Java Servlets Form Data

10 How client sends data?  When a user submits a browser request to a web server, it sends two categories of data:  Form Data: Data that the user explicitly typed into an HTML form.  For example: registration information.  HTTP Request Header Data: Data that is automatically appended to the HTTP Request from the client.  For example: cookies, browser type, browser IP address.

11 EE557: Server-Side Development Html Form & Servlet HTML is Graphical User Interface (GUI) for a servlet

12 Example : Reading two explicit parameters

13 Form Example  Our first example consists of one HTML page (index.html), and one servlet (FormServlet.java).  The HTML page contains two form parameters: firstname, surname  The Servlet extracts these specific parameters and echoes them back to the browser after appending Hello.

14 index.html

15 EE557: Server-Side Development Reading Two Parameters Please fill out this form: <FORM METHOD="GET" ACTION="http://localhost:8080/paramapp/formservlet" NAME="myform"> Firstname: Surname: ……….. Form Example (using GET)

16 EE557: Server-Side Development Form Example cont. (using GET)

17 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(); } FormServlet.java (using GET)

18 web.xml <web-app> FormServlet FormServlet MyServlet MyServlet FormServlet FormServlet /formservlet /formservlet </web-app> ACTION="http://localhost:8080/paramapp/formservlet"

19 Using NetBeans4.1


Download ppt "Servlet Lifecycle Lec 28. Servlet Life Cycle  Initialize  Service  Destroy Time."

Similar presentations


Ads by Google