quiz2.pdf
Document Details
Uploaded by WarmerMemphis
Tags
Full Transcript
CSC123 (INTRO TO PROGRAMMING II) Usman Aslam | Computer Science RECAP A client process seeks the service of a server process. The connects to the server, sends request and receives response java.net.Socket class acts as a client and is used to open network connections to remote servers Once connecte...
CSC123 (INTRO TO PROGRAMMING II) Usman Aslam | Computer Science RECAP A client process seeks the service of a server process. The connects to the server, sends request and receives response java.net.Socket class acts as a client and is used to open network connections to remote servers Once connected the client communicates with the server using a preagreed protocol e.g. HTTP HTTP protocol defines how the client should send a request and how the response is structured HTTP URL URL is short for Universal Resource Locator An HTTP URL has three parts: 1. Protocol: Name of the protocol being used followed by a colon and two forward slash characters e.g. http:// 2. Host: IP address or domain name e.g. www.google.com 3. Resource: The actual resource (text, images, videos) e.g. /videos/myvideo.mp4 Example: http://www.google.com/mypage/page.html COMMON HTTP HEADERS Header Type Description User-Agent Request Client sends this information to tell server what type of web browser it is Host Request Client sends this to server with the “host” part of the URL Content-Type Response Server returns this to client to explain the type of content being returned Set-Cookie Response Server returns this to client to store the contained information as a cookie Server Response Server returns this information to explain what type of server software it is running COMMON HTTP HEADERS Code Name Description 200 OK 301 Permanently moved The requested resource has moved to a new location (location is provided) 302 Temporarily moved The requested resource has moved to a new location (location is provided) 400 Bad Request Server does not understand client’s request, request is not according to protocol 401 Unauthorized The client needs to login and provide credentials 403 Forbidden The client has no access rights to the requested resource 404 Not Found The requested resource was not found on the server 500 Internal Server Error There was some error, and the server was not able to complete the request 503 Service Unavailable The server is not ready to service client’s request The server successfully processed request and has returned the output https://developer.mozilla.org/en-US/docs/Web/HTTP/Status SERVER PROCESS A Server process provides a service to clients. It is always “listening” on a pre-defined network port waiting for client connections. The server process: Attaches itself to a specific network port and starts listening Accepts client connection and establishes a two-way communication channel Receives client requests and processes it Sends the response to the client Closes connection when done (Connection closure can be initiated by the client or the server, depending on the protocol) java.net.ServerSocket class represents a server socket SERVER PROCESS ServerSocket server = new ServerSocket(1200); Socket client=server.accept(); // Blocks until client connection is made InputStream in=client.getInputStream(); OutputStream out=client.getOutputStream(); LAB EXERCISES Two lab exercises have been uploaded. One for a Java Client and the other one for a Java Server REST API REST stands for REpresentational State Transfer The API stands for Application Programming Interface if you write a reusable Java class with methods, you are essentially creating an API REST is an architectural style: That is used to build scalable and distributed systems That enables interoperability Where resources are hosted on remote servers and are accessed using Uniform Resource Locator (URL) addresses Where the client makes requests to access and manipulate resources using methods such as GET, POST, PUT, PATCH, DELETE, etc. REST API WHY USE REST? Scalability: RESTful APIs can handle large numbers of requests from a wide range of clients Simplicity: REST is easy to understand and implement due to its adherence to standard HTTP methods (GET, POST, PUT, DELETE) Interoperability: Works well with different platforms, languages, and systems, promoting reusability and collaboration Performance: Stateless nature allows for better caching, reducing server load and improving response times Maintainability: Separation of concerns between client and server allows for independent development and updates JSON JSON stands for JavaScript Object Notation It is a lightweight data-interchange format that is easy for humans to read and write, and easy for machines to parse and generate. JSON is based on a subset of the JavaScript programming language, but is language-independent and can be used with any programming language that can parse JSON data. JSON data is structured as key-value pairs, where keys are strings and values can be strings, numbers, booleans, arrays, or other JSON objects JSON data can be easily converted to and from native data types in most programming languages using built-in functions or third-party libraries. JSON syntax is simple and flexible, allowing for comments, whitespace, and varying levels of data complexity. JSON is widely used in web development, mobile app development, and other software applications that require data exchange or storage. SAMPLE JSON JSON ARRAY JSON Arrays are simple structures that hold simple list of items, just like a Java array or ArrayList JSON Arrays enclosed in square brackets: [ ] Example: [“Apples”,”Banana”,23,true] JSON OBJECT (MAP) JSON Objects are key/value structures that hold a simple list of items, just like a Java array or ArrayList JSON Arrays enclosed in curly braces: {} Example: {“Name”:”Jane”,”Email”:[email protected]} JSON DOCUMENTS JSON documents are structured using a combination of JSON Array and JSON Objects Example: Consider the requirement to store all students in class where each student has a Name, Email Address, and Phone Number as data fields Individual students will be stored in a JSON object All students will be stored in a JSON array Each entry in the JSON array will be a JSON object containing an individual student JSON DOCUMENTS [ {”Name”:”Student1”, “Email”:”[email protected]”, “Phone”:”Student1Phone”}, {”Name”:”Student2”, “Email”:”[email protected]”, “Phone”:”Student2Phone”} ] JSON LIBRARY To work with JSON objects in Java a third-party library will be required A library called Simple JSON can be downloaded from: http://www.java2s.com/Code/Jar/j/Downloadjsonsimple111jar.htm The library consists of a Java Archive File (.jar) called json-simple-1.1.1.jar The library was made by a third party that compiled Java classes and packaged them in a.jar file You will need to add the library to your IDE and the classpath to use it