WDD_lec_28 - Servlet Lifecycle Lec 28 Servlet Life Cycle...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Servlet Lifecycle Lec 28 Servlet Life Cycle Time Initialize Service Destroy 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) Life Cycle: Service On each request, the server spawns a new thread and calls service() Life Cycle: Service service() checks HTTP request type and calls doGet() or doPost() Override doGet() or doPost()to provide desired behaviour. EE557: ServerSide Development Life Cycle: Service 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 EE557: ServerSide Development Servlet Life Cycle Summary Java Servlets Form Data 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. EE557: ServerSide Development Html Form & Servlet HTML is Graphical User Interface (GUI) for a servlet Example : Reading two explicit parameters 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. index.html EE557: ServerSide Development Form Example (using GET) <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"> ........... EE557: ServerSide Development Form Example cont. (using GET) <BR> <INPUT TYPE="submit" value="Submit Form"> <INPUT TYPE="reset" value="Reset"> </FORM> </body> </html> EE557: ServerSide Development FormServlet.java (using GET) 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 { <input TYPE=""text NAME="firstname"> String fName = req.getParameter("firstname"); String sName = req.getParameter("surname"); PrintWriter out = res.getWriter(); out.println("Hello: " + fName + " " +sName "); out.close(); } } web.xml <?xml version="1.0" encoding="ISO-8859-1"?> <web-app> <servlet> <servlet-name>FormServlet</servlet-name> <servlet-class>MyServlet</servlet-class> </servlet> ACTION="http://localhost:8080/paramapp/formservlet" <servlet-mapping> <servlet-name>FormServlet</servlet-name> <url-pattern>/formservlet</url-pattern> </servlet-mapping> </web-app> Using NetBeans4.1 ...
View Full Document

Ask a homework question - tutors are online