1
+ package com .exchange ;
2
+
3
+ import java .io .*;
4
+
5
+ import java .net .*;
6
+
7
+ import java .util .*;
8
+
9
+ import javax .servlet .*;
10
+
11
+ import javax .servlet .http .*;
12
+
13
+ import java .io .InputStream ;
14
+
15
+ import java .net .*;
16
+
17
+ import com .google .gson .*;
18
+
19
+ /**
20
+
21
+ *
22
+
23
+ * @author pakallis
24
+
25
+ */
26
+
27
+ classRecv
28
+
29
+ {
30
+
31
+ private String lhs ;
32
+
33
+ private String rhs ;
34
+
35
+ private String error ;
36
+
37
+ private String icc ;
38
+
39
+ public Recv (
40
+
41
+ {
42
+
43
+ }
44
+
45
+ public String getLhs ()
46
+
47
+ {
48
+
49
+ return lhs ;
50
+
51
+ }
52
+
53
+ public String getRhs ()
54
+
55
+ {
56
+
57
+ return rhs ;
58
+
59
+ }
60
+
61
+ }
62
+
63
+ public classConvertextendsHttpServlet {
64
+
65
+ /**
66
+
67
+ * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
68
+
69
+ * @param request servlet request
70
+
71
+ * @param response servlet response
72
+
73
+ * @throws ServletException if a servlet-specific error occurs
74
+
75
+ * @throws IOException if an I/O error occurs
76
+
77
+ */
78
+
79
+ protected void processRequest (HttpServletRequest req , HttpServletResponse resp )
80
+
81
+ throws ServletException , IOException {
82
+
83
+ String query = "" ;
84
+
85
+ String amount = "" ;
86
+
87
+ String curTo = "" ;
88
+
89
+ String curFrom = "" ;
90
+
91
+ String submit = "" ;
92
+
93
+ String res = "" ;
94
+
95
+ HttpSession session ;
96
+
97
+ resp .setContentType ("text/html;charset=UTF-8" );
98
+
99
+ PrintWriter out = resp .getWriter ();
100
+
101
+ /*Read request parameters*/
102
+
103
+ amount = req .getParameter ("amount" );
104
+
105
+ curTo = req .getParameter ("to" );
106
+
107
+ curFrom = req .getParameter ("from" );
108
+
109
+ /*Open a connection to google and read the result*/
110
+
111
+ try {
112
+
113
+ query = "http://www.google.com/ig/calculator?hl=en&q=" + amount + curFrom + "=?" + curTo ;
114
+
115
+ URL url = new URL (query );
116
+
117
+ InputStreamReader stream = new InputStreamReader (url .openStream ());
118
+
119
+ BufferedReader in = new BufferedReader (stream );
120
+
121
+ String str = "" ;
122
+
123
+ String temp = "" ;
124
+
125
+ while ((temp = in .readLine ()) != null ) {
126
+
127
+ str = str + temp ;
128
+
129
+ }
130
+
131
+ /*Parse the result which is in json format*/
132
+
133
+ Gson gson = new Gson ();
134
+
135
+ Recv st = gson .fromJson (str , Recv .class );
136
+
137
+ String rhs = st .getRhs ();
138
+
139
+ rhs = rhs .replaceAll ("�" , "" );
140
+
141
+ /*we do the check in order to print the additional word(millions,billions etc)*/
142
+
143
+ StringTokenizer strto = new StringTokenizer (rhs );
144
+
145
+ String nextToken ;
146
+
147
+ out .write (strto .nextToken ());
148
+
149
+ nextToken = strto .nextToken ();
150
+
151
+ if ( nextToken .equals ("million" ) || nextToken .equals ("billion" ) || nextToken .equals ("trillion" ))
152
+
153
+ {
154
+
155
+ out .println (" " +nextToken );
156
+
157
+ }
158
+
159
+ } catch (NumberFormatException e ) {
160
+
161
+ out .println ("The given amount is not a valid number" );
162
+
163
+ }
164
+
165
+ }
166
+
167
+ // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
168
+
169
+ /**
170
+
171
+ * Handles the HTTP <code>GET</code> method.
172
+
173
+ * @param request servlet request
174
+
175
+ * @param response servlet response
176
+
177
+ * @throws ServletException if a servlet-specific error occurs
178
+
179
+ * @throws IOException if an I/O error occurs
180
+
181
+ */
182
+
183
+ @ Override
184
+
185
+ protected void doGet (HttpServletRequest request , HttpServletResponse response )
186
+
187
+ throws ServletException , IOException {
188
+
189
+ processRequest (request , response );
190
+
191
+ }
192
+
193
+ /**
194
+
195
+ * Handles the HTTP <code >POST </co
0 commit comments