1
- /**
2
- * Copyright 2015 Google Inc. All Rights Reserved.
1
+ /*
2
+ * Copyright 2016 Google Inc. All Rights Reserved.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
6
+ * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
9
7
*
10
8
* Unless required by applicable law or agreed to in writing, software
11
9
* distributed under the License is distributed on an "AS IS" BASIS,
21
19
import java .io .BufferedReader ;
22
20
import java .io .IOException ;
23
21
import java .io .InputStreamReader ;
24
- import java .io .PrintWriter ;
22
+ import java .io .OutputStreamWriter ;
23
+ import java .net .HttpURLConnection ;
25
24
import java .net .URL ;
26
-
25
+ import java .net .URLEncoder ;
26
+ import javax .servlet .ServletException ;
27
27
import javax .servlet .http .HttpServlet ;
28
28
import javax .servlet .http .HttpServletRequest ;
29
29
import javax .servlet .http .HttpServletResponse ;
@@ -33,25 +33,71 @@ public class UrlFetchServlet extends HttpServlet {
33
33
34
34
@ Override
35
35
public void doGet (HttpServletRequest req , HttpServletResponse resp )
36
- throws IOException {
37
- PrintWriter out = resp .getWriter ();
38
- out .println ("<html><body>" );
36
+ throws IOException , ServletException {
39
37
40
38
// [START example]
41
39
URL url = new URL ("http://api.icndb.com/jokes/random" );
42
40
BufferedReader reader = new BufferedReader (new InputStreamReader (url .openStream ()));
43
- String json = "" ;
41
+ StringBuffer json = new StringBuffer () ;
44
42
String line ;
45
43
46
44
while ((line = reader .readLine ()) != null ) {
47
- json += line ;
45
+ json . append ( line ) ;
48
46
}
49
47
reader .close ();
50
48
// [END example]
51
- JSONObject jo = new JSONObject (json );
52
- out .println ("<h2>"
53
- + jo .getJSONObject ("value" ).getString ("joke" )
54
- + "</h2>" );
55
- out .println ("</body></html>" );
49
+ JSONObject jo = new JSONObject (json .toString ());
50
+
51
+ req .setAttribute ("joke" , jo .getJSONObject ("value" ).getString ("joke" ));
52
+ req .getRequestDispatcher ("/main.jsp" ).forward (req , resp );
53
+ }
54
+
55
+ @ Override
56
+ public void doPost (HttpServletRequest req , HttpServletResponse resp )
57
+ throws IOException , ServletException {
58
+
59
+ String id = req .getParameter ("id" );
60
+ String text = req .getParameter ("text" );
61
+
62
+ if (id == null || text == null || id == "" || text == "" ) {
63
+ req .setAttribute ("error" , "invalid input" );
64
+ req .getRequestDispatcher ("/main.jsp" ).forward (req , resp );
65
+ return ;
66
+ }
67
+
68
+ JSONObject jsonObj = new JSONObject ()
69
+ .put ("userId" , 33 )
70
+ .put ("id" , id )
71
+ .put ("title" , text )
72
+ .put ("body" , text );
73
+
74
+ // [START complex]
75
+ URL url = new URL ("http://jsonplaceholder.typicode.com/posts/" + id );
76
+ HttpURLConnection conn = (HttpURLConnection ) url .openConnection ();
77
+ conn .setDoOutput (true );
78
+ conn .setRequestMethod ("PUT" );
79
+
80
+ OutputStreamWriter writer = new OutputStreamWriter (conn .getOutputStream ());
81
+ writer .write (URLEncoder .encode (jsonObj .toString (), "UTF-8" ));
82
+ writer .close ();
83
+
84
+ int respCode = conn .getResponseCode (); // New items get NOT_FOUND on PUT
85
+ if (respCode == HttpURLConnection .HTTP_OK || respCode == HttpURLConnection .HTTP_NOT_FOUND ) {
86
+ req .setAttribute ("error" , "" );
87
+ StringBuffer response = new StringBuffer ();
88
+ String line ;
89
+
90
+ BufferedReader reader = new BufferedReader (new InputStreamReader (conn .getInputStream ()));
91
+ while ((line = reader .readLine ()) != null ) {
92
+ response .append (line );
93
+ }
94
+ reader .close ();
95
+ req .setAttribute ("response" , response .toString ());
96
+ } else {
97
+ req .setAttribute ("error" , conn .getResponseCode () + " " + conn .getResponseMessage ());
98
+ }
99
+ // [END complex]
100
+ req .getRequestDispatcher ("/main.jsp" ).forward (req , resp );
56
101
}
102
+
57
103
}
0 commit comments