Skip to content

cbfacademy/java-exercises-web-applications

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Web Applications

[Language][1]

The goal of these programming exercises is to practise:

  • working with a web server
  • working with a web client
  • creating simple HTML pages

🌐 HTTP

Exercise 1

Complete a Java class that constructs HttpRequest objects with specific properties.

Task: Implement the build() method in the HttpRequestBuilder class to return a configured HttpRequest object. Use the HttpRequest.newBuilder() method to create a Builder instance and build a request with the following properties:

  • Method: GET
  • HTTP Version: HTTP_1_1
  • User-Agent: "Mozilla/5.0 (Java Exercise Client)"
  • Accept: "text/html,application/json,/;q=0.8"
  • Timeout: 30 seconds

Example usage:

HttpRequest request = HttpRequestBuilder.build("https://www.example.com/api/data");

System.out.println("Method: " + request.method());
System.out.println("URI: " + request.uri());
System.out.println("Headers: " + request.headers().map());

Exercise 2

Complete a Java class that processes HttpResponse objects and extracts key information into a Map.

Task: Implement the parse() method in the HttpResponseParser class to return a Map<String, String> containing:

  • "URL": Request URL (from the response's request)
  • "Status": HTTP status code as a string
  • "Server": Server header value (only if present)
  • "Content-Type": Content-Type header value (only if present)
  • "Content-Length": Content-Length header value (only if present)

Example usage:

Map<String, String> responseData = HttpResponseParser.parse(response);

System.out.println("URL: " + responseData.get("URL"));
System.out.println("Status: " + responseData.get("Status"));
if (responseData.containsKey("Server")) {
    System.out.println("Server: " + responseData.get("Server"));
}

Note: Use Java's built-in java.net.http package (available in Java 11+) for the HttpRequest and HttpResponse classes.

🕸️ HTML

An HTML form is a section of the page that collects input from the user. The input from the user is generally sent to a server (web servers, mail clients, etc). We use the <form> element to create forms in HTML.

Form Structure

Create a HTML form in login.html with the following structure:

Required Elements:

Create a <form> element containing:

  • Three <div> elements containing:
    1. An email-type <input> element with its id set to "email" and an external label
    2. A password-type <input> with its id set to "password" and an external label
    3. A checkbox-type input with its id set to "remember", inside a label and a "Forgot password" <a> element (you can use # for the href attribute)
  • A submit-type <button>

Accessibility Requirements:

  • All inputs should have a name attribute defined
  • All inputs should have associated labels using for attributes or being wrapped by the associated label
  • All text inputs should have a placeholder attribute to indicate the value that should be input

Visual Reference

HTML Form

Note: Styling for the form is provided, but you won't be graded on its appearance. Focus on the correct HTML structure and accessibility features.

✅ Verify Your Implementation

To verify that your form is structured as expected, run the tests:

./mvnw clean test

To run the HTTP exercises demo:

./mvnw exec:java -Dexec.mainClass="com.cbfacademy.App"

Learn more: Anatomy of an HTML Document

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published