Create a web app using statistics from the American sport, baseball.
Your app must meet these criteria:
-
Scrape baseball data from at least two web sites. Document what sites you used with an on-screen annotation.
-
Use baseball data from one web API's. Document these sites as above. You must use the gson library to handle JSON records.
-
Include at least two baseball-related images. This can be specific baseball players, pictures of actual games or stadiums. Again, document the source in the output.
-
Be able to handle different user input - in other words, do not show the same information in your output each time - it must be dynamic, not static. It must also be repeatable: if a user enters the same input data another time, the same output should be presented.
-
Be coded primarily in Java and JSP. If you use something else, give a brief but convincing argument why this was necessary and not just a convenience.
-
Present a web site that requires user interaction using a text field and at least two of these:
- button
- drop-down menu
- radio buttons
-
Use at least two web pages for your output.
-
Use the Model-View-Contorller (MVC) pattern and good separation of concerns
-
Handle error conditions in a "reasonable" way.
-
Be well documented - the code, the usage of the app, and the structure/design of the app, any AI-related things as described above. You do not need to, but you are welcome to, use jsoup (https://jsoup.org/) which is a Java HTML Parser, to do the scraping. It is the Java version of Beautiful Soup, which you may have used in Python. The downsides of using jsoup are you will first need to understand the Document Object Model (DOM) and CSS Selectors. These are both useful to know. The upside of using jsoup is that it makes it much easier to find and select content from HTML (i.e. screen scrape). Refer to the JSON Maven notes below; adding jsoup will require a similar process.
JSON and gson JSON records are text records containing tag-value pairs, where the tag is the field name - think of it as a dictionary or map with nesting. It is much shorter than XML. In order to find what you need, use the JSON library GSON. To use GSON, download the gson v.2.10.1 jar file to a place you'll remember. To add it to your project, go to File->Project Structure->Modules, choose the Dependencies tab, click the + icon at the bottom choose Jars or Directories, navigate to where you put the jar file, click that, then Apply and OK. It should show up in your pom.xml file as the last entry in as:
If this does not appear, add the above lines manually to pom.xml. Finally, reload the Maven dependencies to have this new dependency take effect - an icon will probably appear in the pom.xml window, but if you don't see it, got to the Project View window, find the pom.xml entry (it should be near the bottom of the tree), right click it, choose Maven -> Reload Project. (FYI, Maven is a build management tool, different from the usual Gradle build; Maven uses the Project Object Model (pom) file to keep track of properties and dependencies.) Please use gson and not some other JSON library. SSLHandshakeException Most modern sites require you to make https, not http requests. When you do so from your Java program, you will hit an SSLHandshakeException. We will be covering SSL and related topics in a few weeks. In the meantime, you will have to deal with this exception. If you use jsoup, you should use validateTLSCertificates(false). (Refer to the jsoup API to understand this when you need it.) If you do not use jsoup, here is a code to replace the fetch method in InterestingPictureModel to ignore the exception. The parameter "certType" should be set to the string "TLSV1.3". private String fetch(String searchURL, String certType) { try { // Create trust manager, which lets you ignore SSLHandshakeExceptions createTrustManager(certType); } catch (KeyManagementException ex) { System.out.println("Shouldn't come here: "); ex.printStackTrace(); } catch (NoSuchAlgorithmException ex) { System.out.println("Shouldn't come here: "); ex.printStackTrace(); }
String response = "";
try {
URL url = new URL(searchURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String str;
// Read each line of "in" until done, adding each to "response"
while ((str = in.readLine()) != null) {
// str is one line of text readLine() strips newline characters
response += str;
}
in.close();
} catch (IOException e) {
System.err.println("Something wrong with URL");
return null;
}
return response;
}
private void createTrustManager(String certType) throws KeyManagementException, NoSuchAlgorithmException{ /**
- Annoying SSLHandShakeException. After trying several methods, finally this
- seemed to work.
-
Taken from: http://www.nakov.com/blog/2009/07/16/disable-certificate-validation-in-java-ssl-connections/ */ // Create a trust manager that does not validate certificate chains TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted(X509Certificate[] certs, String authType) { } public void checkServerTrusted(X509Certificate[] certs, String authType) { } } };
// Install the all-trusting trust manager SSLContext sc = SSLContext.getInstance(certType); sc.init(null, trustAllCerts, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
// Create all-trusting host name verifier HostnameVerifier allHostsValid = new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } };
// Install the all-trusting host verifier HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid); }