fork(4) download
  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. import="java.io.*" import="java.util.*" import="java.text.*"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://w...content-available-to-author-only...3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <title>Insert title here</title>
  7. <link rel='stylesheet' type='text/css' href='round.css'>
  8. </head>
  9. <body>
  10. <FORM METHOD=post action="GoodsAndRates.jsp">
  11. Курс <INPUT TYPE=TEXT NAME="rate"> <INPUT TYPE=SUBMIT
  12. value="Пересчитать">
  13. </FORM>
  14. <%
  15. request.setCharacterEncoding("UTF-8");
  16.  
  17. final NumberFormat nf = NumberFormat.getInstance();
  18. nf.setMaximumFractionDigits(2);
  19. nf.setMinimumFractionDigits(2);
  20.  
  21. try {
  22. String rateParameter = request.getParameter("rate"); // как не дожидаться этого параметра? и продолжать работу
  23. Double rate = Double.parseDouble(rateParameter);
  24.  
  25. Scanner sc = null;
  26. String real_filename = pageContext.getServletContext()
  27. .getRealPath("/goods.txt");
  28. try {
  29. sc = new Scanner(new File(real_filename));
  30. } catch (Exception e) {
  31. e.printStackTrace();
  32. }
  33. Map<String, Double> goods = new TreeMap<String, Double>();
  34.  
  35. while (sc.hasNext()) {
  36. String line = sc.nextLine();
  37. String[] goodsData = line.split("=");
  38. goods.put(goodsData[0], Double.parseDouble(goodsData[1])
  39. * rate);
  40. }
  41. %>
  42. <b>Список товаров</b>
  43. <br>
  44. <table cellpadding=3 cellspacing=3 bgcolor=9AFF92>
  45. <tr bgcolor=3CE022>
  46. <th>Товар</th>
  47. <th>Цена(бел. руб.)</th>
  48. </tr>
  49. <tr>
  50. <td>
  51. <%
  52. //iterating over keys only
  53. for (String key : goods.keySet()) {
  54. out.println(key + "<br>");
  55. }
  56. %>
  57.  
  58. <td>
  59. <%
  60. //iterating over values only
  61. for (Double value : goods.values()) {
  62. out.println(nf.format(value) + "<br>");
  63. }
  64. %>
  65.  
  66. </table>
  67. <%
  68. } catch (NumberFormatException nfe) {
  69. out.println(nfe.toString());
  70. } catch (NullPointerException npe) {
  71. out.println(npe.toString());
  72. } catch (Exception e) {
  73. out.println(e.toString());
  74. }
  75. %>
  76.  
  77. </body>
  78. </html>
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty