lec37 - 1.00 Lecture 37 Java and the Web Internet and...

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: 1.00 Lecture 37 Java and the Web Internet and TCP/IP • Internet is “just” a set of loosely interconnected networks – A set of local area networks connected via wide area networks – Network segments interconnect via routers: • Dedicated computers that manage packets of data – TCP/IP is the universal data protocol on the network – Actual format, content is left to higher-level protocols, like the Web • TCP/IP connections – Client is typically a data consumer that sends short requests • On Web, client is a browser – Server is typically a data provider that sends long responses • Listen for requests and transmit desired data, static or dynamic • On Web, servers talk a protocol called HTTP on port 80 – TCP/IP connection is active only long enough to exchange data • Avoid overhead of many communication channels, but lose state 1 Protocol Layering Server computer or router Browser PC (html) Layer 7: Application Layer 4: Transport Layer 7: Application HTTP Connections Packets Layer 3: Network Layer 2: Data Link Layer 1: Physical Web pgs Frames Bits Layer 4: Transport TCP Layer 3: Network IP Layer 2: Data Link Ethernet Layer 1: Physical 10BaseT Protocol layering Layers TCP header: source port, destination port, seq nbr, checksum, time to live, out of band signal Data 7: HTTP, HTML TCP Hdr Data 4: TCP IP Hdr TCP Hdr Data 3: IP Ethernet Hdr IP Hdr TCP Hdr Data IP header: protocol (TCP), IP source addr, IP dest addr 14 20 20 Enet Trlr 2: LAN 4 Ethernet frame Bits 1: 10BaseT 2 Exercise: TCP/IP Connection // Download and run TcpipTest. What is its output? // Download and run TcpipTest. What is its output? At what layer (1, 2, 3, 4, or 7) is this program operating? // At what layer (1, 2, 3, 4, or 7) is this program operating? java.net.*; import java.net.*; java.io.*; import java.io.*; class public class TcpipTest { static void main(String args) public static void main(String args) { Socket is port try { // Socket is tcp/ip connection: ip address, port s= new Socket("time­a.nist.gov", 13); Socket s= new Socket("time­a.nist.gov", 13); new InputStreamReader is= new InputStreamReader(s.getInputStream()); InputStreamReader(s.getInputStream()); new BufferedReader(is); Same as file! BufferedReader b= new BufferedReader(is); // Same as file! ""; String currentLine = ""; ((currentLine (( b.readLine()) != null) while ((currentLine = b.readLine()) != null) System.out.println(currentLine); System.out.println(currentLine); b.close(); catch (IOException } catch (IOException e) { System.out.println(e); System.out.println(e); } } Socket programs usually use threads, due to delays, losses… } // Socket programs usually use threads, due to delays, losses… Exercise 2: IP Addresses // Download and run AddressTest to see IP addresses // Download and run see IP addresses java.net.*; import java.net.*; javax.swing.*; import javax.swing.*; class public class AddressTest { static void main(String args) public static void main(String args) { try { InetAddress.getLocalHost(); InetAddress local= InetAddress.getLocalHost(); "+ local); System.out.println("Local host: "+ local); input= String input= JOptionPane.showInputDialog host (e.g., web.mit.edu): "); ("Enter host (e.g., web.mit.edu): "); InetAddress.getByName(input); InetAddress other= InetAddress.getByName(input); "+ other); System.out.println("Other host: "+ other); catch (Exception e) } catch (Exception e) { System.out.println(e); System.out.println(e); } } } 3 Exercise 3: Chat Program PC 1 PC 2 ChatServer ChatServer ServerSocket ServerSocket ChatClient ChatClient Socket Socket ReadThread ReadThread Exercise 3: Chat Program • Download ChatClient, ChatServer and ReadThread – We’ll read them together on the next several slides • While we read we will: – Complete ChatServer (two short methods) – Complete ChatClient (one short method) – No changes in ReadThread 4 ChatServer, p.1 import import import import import import java.awt.*; java.awt.*; java.awt.event.*; java.awt.event.*; javax.swing.*; javax.swing.*; java.net.*; java.net.*; java.io.*; java.io.*; class public class ChatServer extends JFrame { serverPanel; Panel for UI elements private JPanel serverPanel; // Panel for UI elements serverLabel; Label for server name private JLabel serverLabel; // Label for server name serverName; Input server to contact private JTextField serverName; // Input server to contact startButton; Button to request session private JButton startButton; // Button to request a session quitButton; Button to end application private JButton quitButton; // Button to end application static 5002; private static int CHATPORT = 5002; static 16; private static int NCHATS = 16; Port for chat service // Port # for chat service max. of requests // max. # of requests Port 5002 is assigned to Radio Free Ethernet; we should really reall // Port 5002 is assigned to Radio Free Ethernet; we should really use an unassigned port! // use an unassigned port! ChatServer, p.2 public ChatServer() public ChatServer() { getContentPane(); Container c = getContentPane(); setBounds(200,200,500,200); new JPanel(); serverPanel = new JPanel(); new "); serverLabel = new JLabel("Server = "); new here", 40); serverName = new JTextField("Enter server here", 40); new startButton = new JButton("Request Connection"); new JButton("Quit"); quitButton = new JButton("Quit"); serverPanel.add(serverLabel); serverPanel.add(serverLabel); serverPanel.add(serverName); serverPanel.add(serverName); serverPanel.add(startButton); serverPanel.add(startButton); c.setLayout(new BorderLayout() ); BorderLayout() ); serverPanel); c.add("Center", serverPanel); quitButton); c.add("North", quitButton); 5 ChatServer, p.3 startButton.addActionListener(new ActionListener() startButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { void serverName.getText(); String svr = serverName.getText(); try { // Your code here: Your code here: Get the Internet address of AddressTest) // Get the Internet address of svr (see AddressTest) Create new Socket w/ address of server and chat port // Create new Socket w/ address of server and chat port (see TcpipTest) // (see TcpipTest) Create new ChatClient, with Socket object as its // Create new ChatClient, with Socket object as its arg } Catch IOException: print "Unable to connect to "+svr "+ // Catch IOException: print "Unable to connect to "+svr } }); ActionListener() quitButton.addActionListener(new ActionListener() { void public void actionPerformed(ActionEvent e) { System.exit(0); } }); setVisible(true); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); setDefaultCloseOperation(EXIT_ON_CLOSE); startListening(); startListening(); End } // End ChatServer constructor Solution: ChatServer startButton startButton.addActionListener(new ActionListener() startButton.addActionListener(new ActionListener() { void public void actionPerformed(ActionEvent e) { serverName.getText(); String svr = serverName.getText(); try { Socket(InetAddress.getByName(svr), new ChatClient(new Socket(InetAddress.getByName(svr), ChatServer.CHATPORT) ); ChatServer.CHATPORT) ); } catch(IOException e1) { connect to svr); System.out.println("Unable to connect to " + svr); } } }); 6 ChatServer, p.4 // Create listen for session requests // Create ServerSocket to listen for session requests This is invoked as last line in // This is invoked as last line in ChatServer constructor void startListening() protected void startListening() { null; ServerSocket soc = null; try { =new ServerSocket(ChatServer.CHATPORT,ChatServer.NCHATS); soc =new ServerSocket(ChatServer.CHATPORT,ChatServer.NCHATS); } catch(IOException e) { System.exit(1); } // This loop waits for a connection request This loop waits for connection request while(true) { try { Socket chatSocket = soc.accept(); soc.accept(); Your code here: // Your code here: Create new this socket as its argument // Create new ChatClient with this socket as its argument This client will be running on the server PC // This client will be running on the server PC } catch(IOException e) { System.exit(1); } } } static void main(String args) public static void main(String args) { new ChatServer startFrame = new ChatServer(); } } Solution: ChatServer startListening protected void startListening() protected void startListening() { null; ServerSocket soc = null; try { new ServerSocket(ChatServer1.CHATPORT, soc = new ServerSocket(ChatServer1.CHATPORT, ChatServer1.NCHATS); } catch(IOException e) { System.exit(1); } This loop waits for connection request // This loop waits for a connection request while(true) { try { soc.accept(); Socket chatSocket = soc.accept(); ChatClient1(chatSocket); New new ChatClient1(chatSocket); // New } catch(IOException e) { System.exit(1); } } } 7 ChatClient, p.1 import import import import import import java.awt.*; java.awt.*; java.awt.event.*; java.awt.event.*; javax.swing.*; javax.swing.*; java.io.*; java.io.*; java.net.*; java.net.*; class public class ChatClient extends JFrame { chFrame; private JFrame chFrame; sendArea, receiveArea; private JTextArea sendArea, receiveArea; endButton; private JButton endButton; private private private private private Socket chatSocket; Socket chatSocket; final addr; final InetAddress addr; DataOutputStream output; DataInputStream input; rThread; ReadThread rThread; final static public final static static char private static char static char private static char static char private static char static char private static char // // // // // Send and receive text // Send and receive text Session socket Session socket Chat server internet address Chat server internet address Output stream for session Output stream for session Input stream for session Input stream for session Thread to receive characters Thread to receive characters char END_SESSION_CHAR '\ ctrl­ ctrl char END_SESSION_CHAR = '\u0004'; // ctrl­D NEWLINE (char) 13; NEWLINE = (char) 13; // newline CR (char) 10; carriage return CR = (char) 10; // carriage return BACKSPACE (char) 8; backspace BACKSPACE = (char) 8; // backspace TAB '\ tab TAB = '\t'; // tab ChatClient, p.2 public cSoc) throws public ChatClient(Socket cSoc) throws IOException { cSoc; chatSocket = cSoc; addr= cSoc.getInetAddress(); addr= cSoc.getInetAddress(); "+addr "+addr); setTitle("Chat session "+addr); getContentPane(); Container c = getContentPane(); setBounds(100,200,500,525); c.setLayout(null); c.setLayout(null); new JTextArea(30, 80); sendArea = new JTextArea(30, 80); sendArea.setBounds(25,225,400,175); new JTextArea(30,80); receiveArea = new JTextArea(30,80); receiveArea.setBounds(25,25,400,175); receiveArea.setEditable(false); receiveArea.setEditable(false); new endButton = new JButton("End Session"); endButton.setBounds(150,425,150,50); c.add(sendArea); c.add(sendArea); c.add(receiveArea); c.add(receiveArea); c.add(endButton); c.add(endButton); session "+addr "+addr); System.out.println("New chat session "+addr); 8 ChatClient, p.3 endButton.addActionListener(new ActionListener() endButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { void try { output.writeChar(ChatClient.END_SESSION_CHAR); output.writeChar(ChatClient.END_SESSION_CHAR); } catch(IOException ev) { ev) in chat session "+addr "+addr); System.out.println("IO error in chat session "+addr); } endSession(); endSession(); } ); } ); KeyAdapter() sendArea.addKeyListener(new KeyAdapter() { void public void keyTyped(KeyEvent e) { sendCharacter(e.getKeyChar() ); sendCharacter(e.getKeyChar() ); } ); } ); new DataInputStream(chatSocket.getInputStream() ); input = new DataInputStream(chatSocket.getInputStream() ); new ReadThread(this, input); Give input to thread to rThread = new ReadThread(this, input); // Give input to thread to rThread.start(); read and handle rThread.start(); // read and handle new DataOutputStream(chatSocket.getOutputStream() ); output = new DataOutputStream(chatSocket.getOutputStream() ); c.validate(); setVisible(true); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); End constructor setDefaultCloseOperation(EXIT_ON_CLOSE); } // End constructor ChatClient, p.4 } public void public void sendCharacter(char c) { try { (isAllowedCharacter(c isAllowedCharacter(c)) if (isAllowedCharacter(c)) output.writeChar(c); output.writeChar(c); == ChatClient.END_SESSION_CHAR) if(c == ChatClient.END_SESSION_CHAR) endSession(); endSession(); catch (IOException } catch (IOException e) { chat "+addr "+addr); System.out.println("IOException in chat "+addr); } } void public void waitForMessage(char c) { (c == ChatClient.END_SESSION_CHAR) if (c == ChatClient.END_SESSION_CHAR) endSession(); endSession(); else { char ch= c; final char ch= c; Your code here, same as last lecture // Your code here, same as last lecture // Use SwingUtilities.invokeLater() method to insert an Use SwingUtilities.invokeLater() method to insert an event on the Swing event list. Its argument is // event on the Swing event list. Its argument is new anonymous object that implements // a new anonymous object that implements Runnable The object's public void run() method will call the // The object's public void run() method will call the updateReceivedMessage() method with char slide) // updateReceivedMessage() method with char ch (next slide) } 9 Solution: ChatClient public void public void waitForMessage(char c) { == ChatClient1.END_SESSION_CHAR) if(c == ChatClient1.END_SESSION_CHAR) endSession(); endSession(); else { char ch= c; final char ch= c; Runnable() SwingUtilities.invokeLater(new Runnable() { void run() public void run() { ChatClient1.this.updateReceivedMessage(ch); } }); } } ChatClient, p.5 public void public void updateReceivedMessage(char c) { (c == == ChatClient.CR) if (c == ChatClient.NEWLINE || c == ChatClient.CR) receiveArea.append("\ receiveArea.append("\n"); if (c == ChatClient.BACKSPACE) else if (c == ChatClient.BACKSPACE) { receiveArea.getText(); String s = receiveArea.getText(); (s.length() 0) if (s.length() > 0) receiveArea.setText(s.substring(0,s.length()­ ); receiveArea.setText(s.substring(0,s.length()­1) ); else } else { new char[1]; char newChar = new char[1]; c; newChar[0] = c; String(newChar)); receiveArea.append(new String(newChar)); } } public void endSession() { void endSession() try { output.close(); input.close(); chatSocket.close(); chatSocket.close(); "+addr "+addr); System.out.println("Connection closed "+addr); } catch(IOException e) { close connection "+addr "+addr); System.out.println("Unable to close connection "+addr); } isAllowedCharacter() code omitted dispose(); } // isAllowedCharacter() code omitted 10 ReadThread import java.io.*; import java.io.*; class Thread public class ReadThread extends Thread { session to forward characters to private ChatClient chat; // session to forward characters to private DataInputStream input; // stream to read from stream to read from public ReadThread(ChatClient c, DataInputStream s) { super(); s; input = s; c; chat = c; } void run() public void run() { c='A'; // initialize != END_SESSION_CHAR char c='A'; // initialize c != END_SESSION_CHAR != ChatClient.END_SESSION_CHAR) while(c != ChatClient.END_SESSION_CHAR) { try { input.readChar(); Blocks until character is read c = input.readChar(); // Blocks until character is read chat.waitForMessage(c); chat.waitForMessage(c); } catch(IOException e) { break; } } } } Exercise 3 Solutions: Chat Program • We completed ChatServer: – In startButton’s action listener: • Created a new Socket with the server IP address and the chat port number • Created a new ChatClient on that socket. This ChatClient will be on the client PC. – In method startListening(): • Created a new ChatClient on the socket that the server hears. This ChatClient will be on the server PC. • We completed ChatClient: – In method waitForMessage(), which is called by the thread that reads characters from the other ChatClient: • It gets a character, ch • It calls SwingUtilities.invokeLater(), which take an anonymous object that implements Runnable as its argument • The anonymous object’s public void run() method calls the updateReceivedMessage() method with character c • Refer to an outerClass instance by outerClass.this • No changes in ReadThread – It reads characters from the other client, which blocks – By having the reader in a separate thread, the entire client doesn’t block 11 Exercise Conclusion • Run ChatServer – If you enter your own computer name as the server, you will get two ChatClients on your PC • You can happily type at yourself – If you enter someone else’s computer name who is running ChatServer, you will get a ChatClient on your PC and he or she will get a Chat Client on his or her PC HTTP protocol • Four phases: – Open tcp/ip connection: Based on URL – Http request: Browser opens connection to server and sends: • • • • Request method, (and request data at bottom if POST or PUT request) URL, HTTP version number Header information (informational, optional), terminated with blank line – Http response: Server processes request and sends: • HTTP protocol version and status code • Header information, terminated by blank line • Text (data) – Close tcp/ip connection 12 HTTP request examples Typical browser request: telnet web.mit.edu 80, then type: GET /about-mit.html HTTP/1.1 Host: web.mit.edu (required) Accept: text/html, text/plain, image/jpeg, image/gif, */* (optional) (blank line) Typical server response: HTTP/1.1 200 OK Server: Apache/1.3.3 Ben-SSL/1.28 (Unix) Content-Type: text/html Content-Length: 8300 (blank line) <HTML> <HEAD><TITLE>About MIT</TITLE></HEAD> <BODY>The mission of MIT…</BODY> </HTML> HTTP Client Server Browser Request Page HTML Page (static or dynamic) HTTP Decoding HTTP Encoding AddHTTP Header Disk • These transactions are stateless. The connection is closed after each page and re-established: Server can’t connect successive requests from the client 13 Hypertext markup language (html) example <HTML> <HEAD> <TITLE> Welcome to the Aircraft Parts System </TITLE> </HEAD> <BODY> <H1> Welcome to the Aircraft Parts System </H1> This system handles orders for aircraft and balloon parts. We handle aircraft parts, and parts for all balloon manufacturers. We comply with the latest US regulations. <P> The use of this system is subject to <A HREF= MITRule.html> MIT rules and regulations. </A> </BODY> </HTML> (Aircraft1.html) HTML • • • • Tags (e.g. <TAG>) never display but direct the browser Often in pairs (e.g. <TAG> and </TAG>) to delimit section Some tags have attributes (e.g. <A HREF=abc.html> </A>) HTML document begins with <HTML>, ends with </HTML> – Two sections within document: HEAD and BODY – Head has identifying information not displayed – Body is displayed, with formatting: • • • • Paragraph <P> Header levels 1 through 6 <H1> through <H6> Line break or carriage return <BR> Anchor <A>, placed around text or graphics; used for hyperlinks 14 HTTP and HTML • HTTP – Is only direct form of interaction between browser and server – Was an extremely perceptive extension of email, ftp protocols by Tim Berners-Lee to enable Web browsers – Request-response paradigm – Connection made for each request/response pair – Most popular protocol on Internet • HTML – – – – Text description language, based on tags High level description rather than specific formatting HTML is static, which is not sufficient for Web applications Many extensions and changes (scripting, Java) for dynamic content HTML forms • Used as front ends to server programs – Java Server Pages, servlets, .NET framework (Microsoft) • Forms are user interface components to collect data from user and transmit it to the server application program – Forms are placed on Web pages that can also have other elements – Java applets can be used but rarely are – All of these run on the browser and are user interface components 15 How HTML forms transmit data • Forms allow a series of components to be placed on the page – Each component has a name and a value – Entire form is associated with the URL of a server side program that will process the input data – Form data is sent when user presses ‘Submit’ button (component) – Data is sent to URL as string of form: • Name1=Value1&Name2=Value2&…NameN=ValueN – If data is sent with HTTP GET command, it is appended to end of GET string after a ?: • GET /Index.html?Name1=Value1… – If data is sent with HTTP PUT command, it is sent after the blank line as a string – Server programs (Java Server Pages, servlet) have methods to extract the data from the string and use it in the program HTML 4.0 tags for forms Tag Type <FORM> <INPUT TYPE= text password file checkbox radio image hidden submit reset <SELECT> <OPTION> <TEXTAREA> Definition Start a form Single line of text entry Single line password entry File to upload, with ‘Browse’ button Checkbox Radio button (option box) Image acting as button Track user, store predefined input, etc. Submit button for form Button to restore default values List box or combo box Item in scrolling list or popup menu Start multiple-line text entry field 16 Browser-server interaction Browser Side • Step 1: Browser requests GET /MyApp/MyPage page without any parameters • Step 2: Server fetches page, and returns empty input form • Step 3: User fills out input form and presses ‘submit’ xxxxxx button xxxxxx • Step 4: Browser packages form into query string and GET /MyApp/MyProg?name=Pat sends to server • Step 5: Program synthesizes a response document and returns it Server Side Web svr Web svr MyProg MyProg Java on servers • Java servlets. Implement the following steps: – Read data sent by browser • Usually entered by user, but could be from applet or JavaScript – Look up other information from the HTTP request • Browser capabilities, cookies, host, etc. from HTTP headers – Generate results • Access database, execute program (possibly via CORBA/COM), etc. – Format the results as an HTML or other document • Servlets can generate GIF, gzip, many MIME types – Set HTTP response parameters in headers – Send document back to browser • Java Virtual Machine (JVM) runs the servlets on the server • These evolved from applets (limited use) into servlets (extremely useful, secure, standard) 17 Servlet Servlet example java.io.*; import java.io.*; javax.servlet.*; import javax.servlet.*; import javax.servlet.http.*; javax.servlet.http.*; class Hello100 extends public class Hello100 extends HttpServlet { void public void doGet(HttpServletRequest request, HttpServletResponse response) ServletException, throws ServletException, IOException { response.setContentType("text/html"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); response.getWriter(); String docType = HTML PUBLIC HTML 4.0 "<!DOCTYPE HTML PUBLIC \"­//W3C//DTD HTML 4.0 " + "Transitional//EN\">\n"; "Transitional//EN\">\ out.println(docType + "<HTML>\ "<HTML>\n" + 1.00</TITLE></HEAD>\ 1.00</TITLE></HEAD> "<HEAD><TITLE>Hello 1.00</TITLE></HEAD>\n" + "<BODY>\ "<BODY>\n" + 1.00</H1>\ 1.00</H1> "<H1>Hello 1.00</H1>\n" + "</BODY></HTML>");}} Java Server Pages (JSP) • Allow us to mix static HTML with dynamically generated content from servlets – Many Web pages are primarily static – Changeable parts are in only a few locations on the page • A servlet requires us to program the entire page • JSP allows the Web designer to write static HTML and leave stubs for dynamic content – Java programmers can then write the stubs • ASP.NET is very similar (Microsoft) 18 JSP example <!DOCTYPE HTML PUBLIC "­ HTML 4.0 Transitional//EN"> <!DOCTYPE HTML PUBLIC "­//W3C//DTD HTML 4.0 Transitional//EN"> <HEAD> <HTML> <HEAD> Example</TITLE> <TITLE>JSP Example</TITLE> REL=STYLESHEET <LINK REL=STYLESHEET HREF="JSP­Styles.css" HREF="JSP­Styles.css" TYPE="text/css css"> TYPE="text/css"> </HEAD> <BODY> Expressions</H2> <H2>JSP Expressions</H2> <UL> time: <%= java.util.Date() <LI>Current time: <%= new java.util.Date() %> hostname: <%= request.getRemoteHost() <LI>Your hostname: <%= request.getRemoteHost() %> <LI>Your session ID: <%= session.getId() %> session ID: <%= session.getId() <CODE>testParam <CODE>testParam</CODE> form parameter: <LI>The <CODE>testParam</CODE> form parameter: request.getParameter("testParam") <%= request.getParameter("testParam") %> </UL> </BODY> </HTML> JSP Example 2 <!DOCTYPE HTML PUBLIC "­ HTML 4.0 Transitional//EN"> <!DOCTYPE HTML PUBLIC "­//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <TITLE>Color Test</TITLE> </HEAD> <HEAD> <TITLE>Color Test</TITLE> </HEAD> <% request.getParameter("bgColor"); String bgColor = request.getParameter("bgColor"); hasColor; boolean hasColor; (bgColor null) if (bgColor != null) { true; hasColor = true; } else { false; hasColor = false; "WHITE";} bgColor = "WHITE";} %> BGCOLOR="<%= <BODY BGCOLOR="<%= bgColor %>"> ALIGN="CENTER">Color Testing</H2> <H2 ALIGN="CENTER">Color Testing</H2> <% (hasColor hasColor) if (hasColor) { used"); out.println(bgColor + “ used"); } else { (WHITE) used“; out.println(“Default color (WHITE) used“; } %> </BODY> </HTML> 19 ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online