1+ /*
2+ * To change this template, choose Tools | Templates
3+ * and open the template in the editor.
4+ */
5+ package com .exchange ;
6+
7+ import java .io .*;
8+ import java .net .*;
9+ import java .util .*;
10+ import javax .servlet .*;
11+ import javax .servlet .http .*;
12+ import java .io .InputStream ;
13+ import java .net .*;
14+ import com .google .gson .*;
15+
16+ /**
17+ *
18+ * @author pakallis
19+ */
20+ class Recv
21+ {
22+ private String lhs ;
23+ private String rhs ;
24+ private String error ;
25+ private String icc ;
26+ public Recv (
27+ {
28+ }
29+ public String getLhs ()
30+ {
31+ return lhs ;
32+ }
33+ public String getRhs ()
34+ {
35+ return rhs ;
36+ }
37+ }
38+ public class Convert extends HttpServlet {
39+ /**
40+ * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
41+ * @param request servlet request
42+ * @param response servlet response
43+ * @throws ServletException if a servlet-specific error occurs
44+ * @throws IOException if an I/O error occurs
45+ */
46+ protected void processRequest (HttpServletRequest req , HttpServletResponse resp )
47+ throws ServletException , IOException {
48+ String query = "" ;
49+ String amount = "" ;
50+ String curTo = "" ;
51+ String curFrom = "" ;
52+ String submit = "" ;
53+ String res = "" ;
54+ HttpSession session ;
55+ resp .setContentType ("text/html;charset=UTF-8" );
56+ PrintWriter out = resp .getWriter ();
57+ /*Read request parameters*/
58+ amount = req .getParameter ("amount" );
59+ curTo = req .getParameter ("to" );
60+ curFrom = req .getParameter ("from" );
61+ /*Open a connection to google and read the result*/
62+
63+ try {
64+ query = "http://www.google.com/ig/calculator?hl=en&q=" + amount + curFrom + "=?" + curTo ;
65+ URL url = new URL (query );
66+ InputStreamReader stream = new InputStreamReader (url .openStream ());
67+ BufferedReader in = new BufferedReader (stream );
68+ String str = "" ;
69+ String temp = "" ;
70+ while ((temp = in .readLine ()) != null ) {
71+ str = str + temp ;
72+ }
73+
74+ /*Parse the result which is in json format*/
75+ Gson gson = new Gson ();
76+ Recv st = gson .fromJson (str , Recv .class );
77+ String rhs = st .getRhs ();
78+ rhs = rhs .replaceAll ("�" , "" );
79+ /*we do the check in order to print the additional word(millions,billions etc)*/
80+ StringTokenizer strto = new StringTokenizer (rhs );
81+ String nextToken ;
82+
83+ out .write (strto .nextToken ());
84+ nextToken = strto .nextToken ();
85+
86+ if ( nextToken .equals ("million" ) || nextToken .equals ("billion" ) || nextToken .equals ("trillion" ))
87+ {
88+ out .println (" " +nextToken );
89+ }
90+ } catch (NumberFormatException e ) {
91+ out .println ("The given amount is not a valid number" );
92+ }
93+ }
94+ // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
95+ /**
96+ * Handles the HTTP <code>GET</code> method.
97+ * @param request servlet request
98+ * @param response servlet response
99+ * @throws ServletException if a servlet-specific error occurs
100+ * @throws IOException if an I/O error occurs
101+ */
102+ @ Override
103+ protected void doGet (HttpServletRequest request , HttpServletResponse response )
104+ throws ServletException , IOException {
105+ processRequest (request , response );
106+ }
107+ /**
108+ * Handles the HTTP <code>POST</code> method.
109+ * @param request servlet request
110+ * @param response servlet response
111+ * @throws ServletException if a servlet-specific error occurs
112+ * @throws IOException if an I/O error occurs
113+ */
114+ @ Override
115+ protected void doPost (HttpServletRequest request , HttpServletResponse response )
116+ throws ServletException , IOException {
117+ processRequest (request , response );
118+ }
119+ /**
120+ * Returns a short description of the servlet.
121+ * @return a String containing servlet description
122+ */
123+ @ Override
124+ public String getServletInfo () {
125+ return "Short description" ;
126+ }// </editor-fold>
127+ }
0 commit comments