DOC PREVIEW
CMU ISM 95702 - Java Servlets

This preview shows page 1-2-3-4-31-32-33-34-35-63-64-65-66 out of 66 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 66 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 66 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 66 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 66 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 66 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 66 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 66 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 66 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 66 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 66 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 66 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 66 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 66 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 66 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Java ServletsPart I : Server and Servlet BasicsNetworkServer.javaSlide 4Slide 5Slide 6Slide 7Compile, Run and VisitEchoServer.javaSlide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19PostForm.htmlPostForm.html BrowserEchoServer Response Using POSTGetForm.htmlGetForm.html BrowserEchoServer Response Using GETA Form With CheckboxesCheckBoxes BrowserCheckBox ResponseRadioBoxes HTMLRadioBoxes BrowserEchoServer ResponseReading Form Data With Servlets Under TomcatSlide 33Slide 34Slide 35Slide 36Slide 37Organizing The Filesbuild.propertiesweb.xmlRunning Ant on build.xmlHandling CheckBoxesPizza ToppingsServlet ResponsePizzaData ServletSlide 46Slide 47Part II Session Tracking and Servlet CollaborationSession Tracking with ServletsTraditional Session TrackingUser AuthorizationShared ObjectsVisitTracker.javaSlide 54Slide 55Slide 56Slide 57Slide 58CookiesUsing CookiesSlide 61Slide 62Slide 63The New Session Tracking APIThe Session Tracking APISlide 66OCT 1Java Servlets Part I Server and Servlet Basics Part II Session Tracking and Servlet CollaborationOCT 2Part I : Server and Servlet Basics•NetworkServer.java and EchoServer.java•PostForm.html•GetForm.html•More HTML form examplesOCT 3NetworkServer.java// NetworkServer.java Adapted from "Core Servlets// and Java Server Pages" // by Marty Hallimport java.net.*;import java.io.*;public class NetworkServer { private int port; private int maxConnections; No Tomcat server.Just this code.OCT 4 protected void setPort(int port) { this.port = port; } public int getPort() { return port; } protected void setMaxConnections(int max) { maxConnections = max; } public int getMaxConnections() { return maxConnections; } public NetworkServer(int port, int maxConnections) { setPort(port); setMaxConnections(maxConnections); }OCT 5// Wait for a connections until maxConnections.// On each connection call handleConnection() passing// the socket. If maxConnections == 0 loop forever public void listen() { int i = 0; try { ServerSocket listener = new ServerSocket(port); Socket server ; while((i++ < maxConnections) || (maxConnections == 0)) { server = listener.accept(); // wait for connection handleConnection(server); } } catch (IOException ioe) { System.out.println("IOException : " + ioe); ioe.printStackTrace(); } }OCT 6// Open readers and writers to socket.// Display client's host name to console.// Read a line from the client and display it on the console.// Send "Generic network server" to the client.// Override this method.protected void handleConnection(Socket server) throws IOException { BufferedReader in = new BufferedReader( new InputStreamReader( server.getInputStream() )); PrintWriter out = new PrintWriter( server.getOutputStream(),true); Flush bufferon printlnInputStream forreading bytesReaders and Writersto work with charactersOCT 7System.out.println("Generic network server: got connection from "+ server.getInetAddress().getHostName() + "\n" + "with first line '" + in.readLine() + "'"); out.println("Generic network server"); server.close(); } public static void main(String args[]) { NetworkServer test = new NetworkServer(6502, 5); test.listen(); }} To server’sconsole.To client.OCT 8Compile, Run and VisitC:\McCarthy\www\46-928\examples\networking>java NetworkServerGeneric network server: got connection from localhostwith first line 'GET / HTTP/1.0'ClientServerOCT 9EchoServer.java/* From Core Servlets, Marty HallAn HTTP Request header example NotesGET /path/file.html HTTP/1.0 The whitespace is required.Accept: text/html Accept header fieldsAccept: audio/x tell the server MIME typesUser-agent: MacWeb (Multipurpose Internet Mail Extension) that are handled by the browser.Still no TomcatHTTP defines dozens of possible headers.Request terminated by two returnsOCT 10EchoServer.javaAn HTTP Response header example HTTP 1.0 200 OKServer: NCSA/1.4.2MIME-version: 1.0Content-type: text/htmlContent-length: 107<html>::</html>Blank lineMIME typeThe client must interpretthis MIME encoded data.Response codeOCT 11HTTP 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 targetresource; it's the URL stripped of the protocol and the serverdomain name. When using the GET method, this field will alsocontain a series of name=value pairs separated by ‘&’. When usinga POST method, the entity body contains these pairs.The HTTP version identifies the protocol used by the client.*/OCT 12// Adapted from Core Servlets and JavaServerPages// by Marty Hall, chapter 16import java.net.*;import java.io.*;import java.util.StringTokenizer;public class EchoServer extends NetworkServer { protected int maxRequestLines = 50; // Post data is brought in // as a single string. protected String serverName = "EchoServer"; public static void main(String a[]) { int port = 6502; new EchoServer(port,0); // loop forever }OCT 13public EchoServer(int port, int maxConnections) { super(port,maxConnections); // call base class constructor listen(); // call base class listen() } // listen calls handleConnection() // Overrides base class handleConection and is called by listen() public void handleConnection(Socket server) throws IOException { // Assign readers and writers to the socket BufferedReader in = new BufferedReader( new InputStreamReader( server.getInputStream() )); PrintWriter out = new


View Full Document

CMU ISM 95702 - Java Servlets

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

53 pages

Lecture

Lecture

80 pages

Lab

Lab

14 pages

Load more
Download Java Servlets
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 Java Servlets 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 Java Servlets 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?