Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion portfolio/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
</properties>

<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
Expand All @@ -41,4 +47,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
15 changes: 12 additions & 3 deletions portfolio/src/main/java/com/google/sps/servlets/DataServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,28 @@

package com.google.sps.servlets;

import com.google.gson.Gson;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/** Servlet that returns some example content. TODO: modify this file to handle comments data */
/** Servlet that returns some example content. */
@WebServlet("/data")
public class DataServlet extends HttpServlet {

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html;");
response.getWriter().println("<h1>Hello world!</h1>");
List<String> messages = new ArrayList<>();
messages.add("This is brought to you by JSON");
messages.add("JSON is JavaScript Object Notation");
messages.add("It is great for storing data like this");

response.setContentType("application/json;");
String json = new Gson().toJson(messages);
response.getWriter().println(json);
}
}
3 changes: 2 additions & 1 deletion portfolio/src/main/webapp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
<body onload="displayMessages()">
<div id="content">
<h1>Ihsan Olawale's Portfolio</h1>
<p>This is Ihsan Olawale's portfolio.</p>
Expand All @@ -19,6 +19,7 @@ <h1>Ihsan Olawale's Portfolio</h1>
<p>After seeing what I look like, you might be interested to know more about me. Don't worry, I considered this during the design phase, and presented an opportunity to cycle through assorted facts.</p>
<button onclick="addRandomFact()">Learn about me</button>
<div id="fact-container"></div>
<div id="messages-container"></div>
</div>
</body>
</html>
14 changes: 14 additions & 0 deletions portfolio/src/main/webapp/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,17 @@ function addRandomFact() {
const factContainer = document.getElementById('fact-container');
factContainer.innerText = fact;
}

/**
* Fetches messages from the server and adds them to the webpage
*/
function displayMessages() {
fetch('/data').then(response => response.json()).then((messages) => {
console.log(messages);
const messageContainer = document.getElementById('messages-container');
messageContainer.innerText = '';
for (const message of messages) {
messageContainer.innerText += message + '\n';
}
});
}