Hello Friends, Well for a change i was was working on making a money converter. As usual i got this Idea from one of the web site and i was working on it to see if i could make it or not. I have used servlet and JDBC. After the servlet is invoked by the server, it fetches the data from the database and send it to the cliend machine. And the conversion calculation is done on the client machine using javascript....which confirm whether the given entery is a number, formats the number and exchange rate to 6 decimal places and performs the calculations. For exchange rate i have takes US $ as base and all the currency is equavalent to 1.00 US$.
This is first time I am using javascript with servlets. Until now i only used to use javascript for form creation i.e. registration form, posting a new topic form. In that case my page used to be in HTMl alone. Here is this example i have combined javascript, html, servlet, jdbc. I would like to know if this method of writting code is correct? Is their is a better way in which i can write my code...if i have to uses javascript, html, servlets, jdbc?
Second pb it that i am not able to make this code work. I am getting the HTMl from the server but complied with error. That is on status line i am getting message "Done Error on page". Can anyone pls go through it and see where the pb is. I have tried working o this code for 6 hours but no luck. I would really appreciate if somebody tell me weak points of my code writting and suggestion for improvement in writting similar code.
Thanks , regards Raj. =============================================================================================== import java.sql.*; import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class Currency extends HttpServlet { public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
// setting the responce type res.setContentType("text/html"); PrintWriter pw = res.getWriter(); //writting the javascript code and writting functions pw.println("<html>\n<head>\n <title>Currency Convertor</title>\n"); pw.println("<script language = javascript>\n "); //This function whether the given entry is a valid number or not pw.println("function checkNum(str) {"); pw.println("if (str.indexOf(\".\") != str.lastIndexOf(\".\")) {"); pw.println("alert(\"Please enter number only ! \");"); pw.println("return(false); \n } "); pw.println("for(var i =0; i <length.str; i++ ) {"); pw.println("var ch = str.substring(i, i+1);"); pw.println("if ( (ch<\"0\" | | ch > \"9\") && (ch != \".\") && (ch != \"-\") ) {"); pw.println("alert(\"Please enter number only !\");"); pw.println("return(false); \n } \n }"); pw.println("return(true); \n }"); //This function format the number in the specified format i.e upto 6 decimal places pw.println("function formatNumber(num) {"); pw.println("num = \"\" + num;"); pw.println("if (num.indexOf(\".\")==0) { "); pw.println("num=\"0\"+num; \n }"); pw.println("if (num.indexOf(\".\") == -1) {"); pw.println("num=num+\".0\"; \n } "); pw.println("num = num + \"000000\";"); pw.println("return(num.substring(0, num.indexOf(\".\")+6)); \n }"); //This function claculates the Exchange rate from the two given exchange rate compared to US$ pw.println("function calcFxRate(rate1, rate2) { "); pw.println("if ((rate1 == -1) | | (rate2 == -1)) {"); pw.println("return(-1); \n }"); pw.println("else {"); pw.println("return(formatNumber(rate1/rate2)); \n } \n }"); //This function calculate the result amount based on the amount and the exchange rate //calculated pw.println("function calcForm (form) {"); pw.println("if ( checkNum(form.Amount.value) && checkNum(form.ExRate.value) && (form.Amount.value >= 0) && (form.ExRate.value >= 0) { "); pw.println("form.Result.value = FormatNumber(form.Amount.value * form.ExRate.value); \n }"); pw.println("else { "); pw.println("form.ExRtae.value = \"NA\";"); pw.println("form.Result.value = \"NA\"; \n }"); pw.println("return(form); \n }"); pw.println("</script> \n </head>"); //end of javascript and the head portion
//Begining of the Body part pw.println(" <body>\n <center><font face=verdana > \n \n"); pw.println("<h2 >Currency Converter</h2>\n<br>"); pw.println("<h3>I want to convert...</h3>\n<br></font>\n"); pw.println("<form><table> \n "); pw.println("<tr align=middle valign=top bgcolor=grey><center><b>this amount</b><br>"); pw.println("<input type=text name=Amount value=1 size=20 onchage=\"CalcForm(this.form)\">"+ " </center></tr> <tr align=center bgcolor=lightgreen> <td><b>from this currency</b><br>");
Connection conn = null; Statement st = null; ResultSet rs = null; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); conn = DriverManager.getConnection("jdbc dbc:Travel"," ", " "); st = conn.createStatement(); //Defining query statement that we will use to retrive data from database String query1 = "SELECT CurrencyFrom,value FROM Travel.Currency"; String query2 = "SELECT CurrencyTo,value FROM Travel.Currency"; rs = st.executeQuery(query1);
//making a select drop down box with size=1(default) pw.println("<select name=From onChange=\"FxRate.value=calcFxRate(To.options[To.options.selectedIndex].value, this.options[selectedIndex].value); calcForm(this.form);\" > "); while (rs.next()) { pw.println("<OPTION value= "+rs.getString("value")+ ">"+rs.getString("CurrencyFrom")+"</OPTION>\n"); } // end of while loop pw.println("</select>\n</td>\n"); //retriving other data and making another drop down box rs = st.executeQuery(query2); pw.println(" <td><b>to this currency</b><br>\n"); pw.println("<select name=To onChange=\"FxRate.value=calcFxRate(this.option[selectedIndex].value, From.options[From.options.selectedIndex].value); calcForm(this.form);\" > "); while (rs.next()) { pw.println("<OPTION value= "+rs.getString("value")+ ">"+rs.getString("CurrencyTo")+"</OPTION>\n"); } // end of while loop pw.println("</select>\n</td>\n</tr>\n"); // creating text boxes for display of exchange rate and result amount pw.println("<tr align = center bgcolor=grey><td><b>Exchange Rate</b><br>"); pw.println("<input type=text name = FxRate onChange=\"calcForm(this.form) value=1.00 \"></td>"); pw.println("<td><b>Result Amount</b><br>"); pw.println("<input type=text name=Result onChange=\"Amount.value=formatNumber(calcFxRate(this.value,FxRate.value))\"></td></tr><br><br>"); // creating two button one is for calculation other is to reset values to default. pw.println("<tr align=middle><td>"); pw.println("<input onClick=\"calcForm(this.form)\" type=button value=Convert> </td>"); pw.println("<td><input type=reset name=Reset value=Reset></td> </tr>"); pw.println("</table>\n<form>\n </body>\n </html>"); pw.close(); } catch( Exception e) { System.out.println(e.getMessage()); }
} public void destroy() { } } ================================================================================================= This is the source code as viewed from Browser View Source option: <html> <head> <title>Currency Convertor</title> <script language = javascript>
function checkNum(str) { if (str.indexOf(".") != str.lastIndexOf(".")) { alert("Please enter number only ! "); return(false); } for(var i =0; i <length.str; i++ ) { var ch = str.substring(i, i+1); if ( (ch<"0" | | ch > "9") && (ch != ".") && (ch != "-") ) { alert("Please enter number only !"); return(false); } } return(true); } function formatNumber(num) { num = "" + num; if (num.indexOf(".")==0) { num="0"+num; } if (num.indexOf(".") == -1) { num=num+".0"; } num = num + "000000"; return(num.substring(0, num.indexOf(".")+6)); } function calcFxRate(rate1, rate2) { if ((rate1 == -1) | | (rate2 == -1)) { return(-1); } else { return(formatNumber(rate1/rate2)); } } function calcForm (form) { if ( checkNum(form.Amount.value) && checkNum(form.ExRate.value) && (form.Amount.value >= 0) && (form.ExRate.value >= 0) { form.Result.value = FormatNumber(form.Amount.value * form.ExRate.value); } else { form.ExRtae.value = "NA"; form.Result.value = "NA"; } return(form); } </script> </head> <body> <center><font face=verdana >
<h2 >Currency Converter</h2> <br> <h3>I want to convert...</h3> <br></font> <form><table>
Have a wonderful day and wish u success<p>S Chandra Mohan<br />sc_mohan_us@yahoo.com
Rajpal Kandhari
Ranch Hand
Joined: Aug 26, 2000
Posts: 126
posted
0
Hello Mohan, Thanks for going through my code. I will try out and come back to you. Regards, Raj.
Angela Poynton
Ranch Hand
Joined: Mar 02, 2000
Posts: 3143
posted
0
Although this does inlcude some Javascript I think you are more likely to get a better response from the Servlets forum so I am moving this thread there!
Pounding at a thick stone wall won't move it, sometimes, you need to step back to see the way around.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.