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
6 changes: 6 additions & 0 deletions portfolio/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
<artifactId>jetty-annotations</artifactId>
<version>${jetty.version}</version>
</dependency>

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

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.google.sps.servlets;

import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/form-handler")

public class FormHandlerServlet extends HttpServlet {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
// Get the value entered in the form.
String textValue = request.getParameter("text-input");

// Print the value so you can see it in the server logs.
System.out.println("You submitted: " + textValue);

Comment thread
gasigala marked this conversation as resolved.
// Write the value to the response so the user can see it.
//want to be able to confirm resolution then go back to the page
response.setContentType("text/html;");
response.getWriter().println("You submitted: " + textValue);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to add html tags to this, like "println(<h1>You submitted: " + textValue + "</h1>)"?


response.sendRedirect("http://gsigala-sps-spring21.appspot.com");
}
}
23 changes: 18 additions & 5 deletions portfolio/src/main/java/com/google/sps/servlets/week2Servelet.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
package com.google.sps.servlets;


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


/** Servlet that returns HTML that contains the page view count. */
@WebServlet("/week2")
public class week2Servelet extends HttpServlet {


@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
ArrayList<String> randomMessage= new ArrayList<String>();
randomMessage.add("Hello there!");
randomMessage.add("Hope you enjoy your stay.");
randomMessage.add("Stay tuned for what to come");

response.setContentType("text/html;");
response.getWriter().println("<h1>This is a servelet example:</h1>");
response.getWriter().println("<p>Hello world!</p>");
String json = convertToJsonUsingGson(randomMessage);
//not exactly sure
response.setContentType("application/json;");
response.getWriter().println(json);

}
private static String convertToJsonUsingGson(ArrayList<String> randomMessage) {
Gson gson = new Gson();
String json = gson.toJson(randomMessage);
return json;
}
}
14 changes: 12 additions & 2 deletions portfolio/src/main/webapp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<title>My Portfolio</title>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
<script src="new_script.js"></script>
</head>
<body>
<div id="content">
Expand All @@ -24,11 +25,20 @@ <h1>Gregorio Sigala's Portfolio</h1>
either "I love it" or "this just ruined the app for me." Regardless if the feature is usefull or not, if it
doesn't feel nice then no one is going to use it.
</p>
<p>Click <a href="/hello">here</a> to see content created by server code.</p>
<p>Click<a href= "/week2"> here</a> to see a servelet in action</p>

<p id= "message-container"></p>
<button type="button" onclick="random_greeting()">Click for a random message using JSON</button>
<p>Click here to get a random message:</p>
<button onclick="addRandomGreeting()">Click me!</button>
<div id="greeting-container"></div>

<form action="/form-handler" method="POST">
<p>Enter some text here:</p>
<textarea name="text-input">hello world</textarea>
<br/>
<input type="submit" />
</form>

</div>
<div class= "row">
<div class= "column">
Expand Down
8 changes: 8 additions & 0 deletions portfolio/src/main/webapp/new_script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//will return a hardcoded message from json
async function random_greeting() {
const responseFromServer = await fetch('/week2');// week 2 is the week2Servelet.java
const messageContainer = document.getElementById('message-container');

const textFromResponse = await responseFromServer.json();
messageContainer.innerText = textFromResponse[Math.floor(Math.random() * 3)];
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro

// Convert the server stats to JSON
ServerStats serverStats = new ServerStats(startTime, currentTime, maxMemory, usedMemory);
String json = convertToJson(serverStats);
//String json = convertToJson(serverStats);
String json = convertToJsonUsingGson(serverStats);

// Send the JSON as the response
response.setContentType("application/json;");
Expand Down