95-702 Distributed Systems 1!Master of Information System Management 95-702 Distributed Systems Lecture 2: Server-Side Programming: An Introduction to Servlets95-702 Distributed Systems 2!Master of Information System Management 2!Master of Information System Management What is a Servlet? • Created by Sun back in 1997 • A Java class that extends HttpServlet • Responds to HTTP requests • The response is usually XHTML or some other XML language • May maintain state across several interactions (may use cookies or URL rewriting or hidden form fields) • Live within a web container • May be generated by a JSP compiler95-702 Distributed Systems 3!Master of Information System Management Servlet Lifecycle • The container loads the servlet class. • The servlet’s init() method is called exactly once. • Upon each request, the container calls the servlet’s service() method. • The service() method selects the appropriate method to call and calls it. • Finally, before the container shuts down, it calls the servlet’s destroy() method. 3!Master of Information System Management95-702 Distributed Systems 4!Master of Information System Management What is an HTTP request? /* From Core Servlets, Marty Hall An HTTP Request header example GET /path/file.html HTTP/1.0 The whitespace is required. Accept: text/html Accept header fields Accept: audio/x tell the server MIME types User-agent: MacWeb (Multipurpose Internet Mail Extension) that are handled by the browser. HTTP defines dozens of possible headers. A blank line followed by name value pairs or an XML document95-702 Distributed Systems 5!Master of Information System Management What is an HTTP Response? An HTTP Response header example HTTP 1.0 200 OK Server: NCSA/1.4.2 MIME-version: 1.0 Content-type: text/html Content-length: 107 <html> : : </html> Blank line MIME type The client must interpret this MIME encoded data. Response code95-702 Distributed Systems 6!Master of Information System Management Request Reply Pattern Request Request Channel Reply channel reply Requestor Replier The pattern applies in the asynchronous and synchronous cases. HTTP is synchronous request reply. From “Enterprise Integration Patterns”.95-702 Distributed Systems 7!Master of Information System Management HTTP General Form <method> <resource identifier> <HTTP Version> <crlf> [<Header> : <value>] <crlf> : : : [<Header> : <value>] <crlf> a blank line [entity body] The resource identifier field specifies the name of the target resource; it's the URL stripped of the protocol and the server domain name. When using the GET method, this field will also contain a series of name=value pairs separated by ‘&’. When using a POST method, the entity body contains these pairs. The HTTP version identifies the protocol used by the client.95-702 Distributed Systems 8!Master of Information System Management Reading Form Data With Servlets Under a Web Server (Glassfish) // QueryData.java -- Handle the voting form in radio.html import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class QueryData extends HttpServlet { public void doPost(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException { doGet(req, response); }95-702 Distributed Systems 9!Master of Information System Management public void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException { String newPresident = req.getParameter("president"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); String docType = "<!DOCTYPE HTML PUBLIC \"//W3C//DTD” + “HTML 4.0 "; docType += "Transitional//EN\">\n";95-702 Distributed Systems 10!Master of Information System Management out.println(docType + "<HTML>\n" + "<HEAD><TITLE>Presidential Servlet" + "</TITLE>” + “</HEAD>\n" + "<BODY>\n" + "<H1>The new president is "+ newPresident + "</H1>\n" + "</BODY></HTML>"); } }95-702 Distributed Systems 11!Master of Information System Management <!-- index.jsp --> <html> <head> <title>Radio Buttons</title> </head> <body BGCOLOR="WHITE"> <form action="http://localhost:8080/WeekTwoServlets/QueryData"> <dl> <dt> Please Vote </dt> <dd><Input type = "Radio" name = "president" value= "Bush"> <b>George W. Bush</b> <dd><Input type = "Radio" name = "president" value = "Gore"> Al Gore <dd><Input type = "Radio" name = "president" value = "Buchanan"> Pat Buchanan <dd><Input type = "Radio" name = "president" value = "Nader"> Ralph Nader <p> <input type = "submit"> </dl> </form> </body> </html> servlet Web server’s port Project path95-702 Distributed Systems 12!Master of Information System Management Radio HTML in the browser95-702 Distributed Systems 13!Master of Information System Management The Servlet’s Response95-702 Distributed Systems 14!Master of Information System Management NetBeans Project List Netbeans provides a development environment. The software is deployed to Glassfish.95-702 Distributed Systems 15!Master of Information System Management NetBeans Generated web.xml Note how the servlet’s name is associated with a URL pattern. “QueryData” is a user defined identifier for use only within this file. <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>QueryData</servlet-name> <servlet-class>QueryData</servlet-class> </servlet> <servlet-mapping> <servlet-name>QueryData</servlet-name>
View Full Document