DOC PREVIEW
CMU ISM 95702 - Lecture

This preview shows page 1-2-3-4-24-25-26-50-51-52-53 out of 53 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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

CMU ISM 95702 - Lecture

Documents in this Course
Homework

Homework

12 pages

Lecture

Lecture

25 pages

Lecture

Lecture

21 pages

Lecture

Lecture

24 pages

Exam

Exam

11 pages

Homework

Homework

16 pages

Homework

Homework

38 pages

lecture

lecture

38 pages

review

review

7 pages

lecture

lecture

18 pages

review

review

8 pages

Chapter2

Chapter2

32 pages

Lecture 4

Lecture 4

47 pages

Lecture

Lecture

22 pages

Naming

Naming

26 pages

lecture

lecture

34 pages

lecture

lecture

42 pages

lecture

lecture

112 pages

Lecture

Lecture

33 pages

Axis

Axis

43 pages

lecture

lecture

32 pages

review

review

17 pages

Lecture

Lecture

80 pages

Lab

Lab

14 pages

Load more
Download Lecture
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?