Lab 2 Primer - PowerPoint PPT Presentation

About This Presentation
Title:

Lab 2 Primer

Description:

Splits a String into an array of strings based on a delimiter. New class: StringTokenizer(String, delimiter) Breaks a String into 'tokens' based on delimiters ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 14
Provided by: cscd6
Category:
Tags: delimiters | lab | primer

less

Transcript and Presenter's Notes

Title: Lab 2 Primer


1
Lab 2 Primer
  • Assignment 3 Structure
  • File I/O
  • More parsing and HTTP Formatting

2
Assignment Reviewso far
  • We explored sockets
  • We looked at how to connect to them, how to
    communicate (I/O) through them, how to parse the
    data through them, and briefly how data is
    interpreted through them
  • We explored the idea of clients and servers
  • We looked at connecting to a server through
    telnet, making a client to connect via sockets,
    protocol schemes (how they understand each
    other), extracting information from sent
    messages, and multiple simultaneous user support
    (threads and why we need them)

3
Now
  • We will now extend the knowledge weve gained
    with more practice at the same principles
  • We will add file I/O to the mix
  • We will continue with string parsing
  • We will add to our use of the HTTP protocol

4
Assignment 3 Structure
  • We need to add
  • File I/O to serve html files
  • Parsing to get key-value pairs from either the
    query string, body or both
  • Parsing to get requested resource from request
  • Proper HTTP responses (200 OK, 404 File Not
    Found, etc.)
  • Content-Length and Content-Type headers

5
Parsing the Query String and Requested File
  • Use same structure as getContentLength() from
    Assignment 2 to find whole path/filequery
  • Use .indexOf( , 5) to find the first space
    after GET or POST /
  • Use indexOf(?) to find the query string (if
    any)
  • If there is no match, indexOf() returns -1
  • Use .substring to get file request and query
    string

6
Parsing the Key-Value pairs
  • We need to make 2 arrays one for keys and one
    for values
  • A key-value pair should be indexed with the same
    value

Index i0 i1 i2 i3
key 1
value 1
key 2
value 2
key 3
value 3
key 4
value 4
7
Parsing the Key-Value pairs
  • New method .split()
  • Splits a String into an array of strings based on
    a delimiter
  • New class StringTokenizer(String, delimiter)
  • Breaks a String into tokens based on delimiters
  • Can iterate through tokens in string

8
Parsing the Key-Value pairs
  • String temp "GET /index.html/?Thisisequalsstr
    inguserfoo"
  • String keystemp.split(")
  • int length temp2.length
  • String values new Stringlength
  • StringTokenizer st new StringTokenizer(temp,
    )
  • int i
  • for(i0 iltlength i)
  • keysi st.nextToken()
  • valuesi st.nextToken()

9
File Input/Output
  • Java is designed for simple I/O
  • ALL I/O works the same keyboard, monitor, files,
    sockets, etc.
  • All I/O can be cast to higher level objects
  • An input/output stream can be cast to a
    reader/writer or a buffered I/O class
  • If you understand socket I/O, file I/O will be
    very simple
  • There are 2 file I/O classes we will use
  • File fin new File(/html)
  • FileReader fileIn new FileReader(filename)

10
File I/O Classes
  • File
  • Opens a local file
  • Can be used to quickly find length of a text file
  • .length()
  • File fin new File (/path/to/file)
  • System.out.print(fin.length())

11
File I/O Classes
  • FileReader
  • Opens the file specified in filename throws
    FileNotFoundException if the file cannot be
    opened
  • Use with BufferedReader
  • BufferedReader fileIn new BufferedReader(new
    FileReader(File))

12
File I/O Basics
  • Much like a socket, keyboard or monitor, file I/O
    can use the read(), readLine(), print(),
    println() methods to get data in and out of a
    file
  • When reading from a file, the end can be
    determined
  • When the input with readLine() is null
  • When the input with read() is 1
  • A file pointer keeps track of where in the file
    we are currently located (see example on next
    page)
  • If we want to write a line where the file pointer
    currently is located, we will overwrite the line
    following the pointer. If the pointer is at the
    end of the file, we will write a new line

13
Final Thoughts
  • Feel free to either refer to the sample solution,
    or to use it as the basis for the next
    assignment. Thats what it is there for.
  • If you have questions, ask! There is nothing to
    gain from silence (except a worse mark)!
  • Try this early! You have plenty of time to finish
    this. Do not wait until the last minute!
Write a Comment
User Comments (0)
About PowerShow.com