Skip to content

Commit 9c1f9fe

Browse files
authored
Update UrlFetch sample (#1249)
* update sample for java8 and to make steps clearer * fix style and remove url fetch in pom * style fixes
1 parent 099b582 commit 9c1f9fe

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

appengine-java8/urlfetch/src/main/java/com/example/appengine/UrlFetchServlet.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.io.OutputStreamWriter;
2323
import java.net.HttpURLConnection;
2424
import java.net.URL;
25-
import java.net.URLEncoder;
2625
import javax.servlet.ServletException;
2726
import javax.servlet.annotation.WebServlet;
2827
import javax.servlet.http.HttpServlet;
@@ -35,8 +34,7 @@
3534
@WebServlet(
3635
name = "URLFetch",
3736
description = "URLFetch: Write low order IP address to Cloud SQL",
38-
urlPatterns = "/urlfetch"
39-
)
37+
urlPatterns = "/urlfetch")
4038
public class UrlFetchServlet extends HttpServlet {
4139

4240
@Override
@@ -67,23 +65,33 @@ public void doPost(HttpServletRequest req, HttpServletResponse resp)
6765
String id = req.getParameter("id");
6866
String text = req.getParameter("text");
6967

70-
if (id == null || text == null || id == "" || text == "") {
68+
// Validation for id and text inputs.
69+
if (id == null
70+
|| text == null
71+
|| id.isEmpty()
72+
|| text.isEmpty()
73+
|| Integer.parseInt(id) > 100) {
7174
req.setAttribute("error", "invalid input");
7275
req.getRequestDispatcher("/urlfetchresult.jsp").forward(req, resp);
7376
return;
7477
}
7578

76-
JSONObject jsonObj =
77-
new JSONObject().put("userId", 1).put("id", id).put("title", text).put("body", text);
78-
7979
// [START complex]
8080
URL url = new URL("http://jsonplaceholder.typicode.com/posts/" + id);
8181
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
82+
// Enable output for the connection.
8283
conn.setDoOutput(true);
84+
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
85+
conn.setRequestProperty("Accept", "application/json");
86+
// Set HTTP request method.
8387
conn.setRequestMethod("PUT");
8488

89+
// Create JSON request.
90+
JSONObject jsonObj =
91+
new JSONObject().put("userId", 1).put("id", id).put("title", text).put("body", text);
92+
8593
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
86-
writer.write(URLEncoder.encode(jsonObj.toString(), "UTF-8"));
94+
writer.write(jsonObj.toString());
8795
writer.close();
8896

8997
int respCode = conn.getResponseCode(); // New items get NOT_FOUND on PUT
@@ -92,6 +100,7 @@ public void doPost(HttpServletRequest req, HttpServletResponse resp)
92100
StringBuffer response = new StringBuffer();
93101
String line;
94102

103+
// Read input data stream.
95104
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
96105
while ((line = reader.readLine()) != null) {
97106
response.append(line);

appengine-java8/urlfetch/src/main/webapp/urlfetchresult.jsp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,20 @@ limitations under the License.
2626
<c:if test="${not empty joke}">
2727
<h2>Joke: ${joke}</h2>
2828
</c:if>
29-
<p /><p />
29+
3030
<c:if test="${not empty error}">
31-
<h2>${error}</h2>
31+
<h2 style="color:red">${error}</h2>
32+
</c:if>
33+
34+
<c:if test="${not empty response}">
35+
<p>${response}</p>
3236
</c:if>
33-
<p />
34-
<c:if test="${not empty response}">${response}</c:if>
35-
<p />
36-
<p>
37+
3738
<form method="post">
38-
<label for="id">&nbsp;&nbsp;ID:</label><input type="text" name="id" value="777"/><br />
39+
<label for="id">&nbsp;&nbsp;ID:</label><input type="text" name="id" value="1"/><br />
3940
<label for="text">Text:</label><input type="text" name="text" value="Lorem ipsum dolor sit amet, consectetur adipiscing elit."/><br />
4041
<input type="submit" value="Send"/>
4142
</form>
42-
</p>
43-
<c:if test="">
44-
45-
</c:if>
4643
</body>
4744
</html>
48-
<!-- [END base]-->
45+
<!-- [END base]-->

0 commit comments

Comments
 (0)