Advertisement
majczel23000

Untitled

Oct 25th, 2019
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. package ssi;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.io.PrintWriter;
  7. import java.text.ParseException;
  8. import java.util.ArrayList;
  9.  
  10. import javax.servlet.ServletException;
  11. import javax.servlet.annotation.WebServlet;
  12. import javax.servlet.http.HttpServlet;
  13. import javax.servlet.http.HttpServletRequest;
  14. import javax.servlet.http.HttpServletResponse;
  15.  
  16. import org.json.JSONObject;
  17.  
  18.  
  19. @WebServlet("/imieniny")
  20. public class imieniny extends HttpServlet {
  21. private static final long serialVersionUID = 1L;
  22.  
  23.  
  24. public imieniny() {
  25. super();
  26. // TODO Auto-generated constructor stub
  27. }
  28.  
  29. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  30. doPost(request, response);
  31. }
  32.  
  33. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  34. request.setCharacterEncoding("UTF-8");
  35. response.setContentType("application/json;charset=UTF-8");
  36.  
  37. // String query = ""+request.getParameter("query");
  38. PrintWriter out = response.getWriter();
  39.  
  40. bazaImienin baza = new bazaImienin();
  41. String lista[][] = baza.imieniny;
  42.  
  43. String jsonText = "";
  44.  
  45. BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
  46. if (br != null ) {
  47. jsonText = br.readLine();
  48. }
  49.  
  50. JSONObject json;
  51.  
  52. try {
  53. json = new JSONObject(jsonText);
  54. } catch (ParseException e) {
  55. json = new JSONObject();
  56. }
  57.  
  58. Integer month = Integer.parseInt(json.getString("month"));
  59. Integer day = Integer.parseInt(json.getString("day"));
  60. String razem = json.getString("razem");
  61.  
  62. ArrayList<String> imieniny = new ArrayList<String>();
  63.  
  64. if (razem.equals("true")) {
  65. json.put("imiona", lista[month][day]);
  66.  
  67. out.println(json.toString());
  68. out.close();
  69. } else {
  70. ArrayList<String> sugestie = new ArrayList<String>();
  71. String[] ar = lista[month][day].split(";");
  72. for (int i = 0; i < ar.length; i++) {
  73. sugestie.add('"'+ar[i]+'"');
  74. }
  75. json.put("imiona", sugestie);
  76.  
  77. out.println(json.toString());
  78. out.close();
  79. }
  80.  
  81.  
  82. }
  83.  
  84. }
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107. <!DOCTYPE html>
  108. <html>
  109. <head>
  110. <meta charset="UTF-8">
  111. <title>Imieniny</title>
  112. <link rel="Stylesheet" href="style.css">
  113. <script src="jquery-3.4.1.js"></script>
  114.  
  115. <script type="text/javascript">
  116. let razem = false;
  117.  
  118. $(document).ready(function () {
  119.  
  120. $('#razem').change(function () {
  121. razem = this.checked;
  122. });
  123. });
  124.  
  125. function wyslijAJAX() {
  126. let parametry = new Object();
  127. parametry.month = $('#data').val().split('-')[1];
  128. parametry.day = $('#data').val().split('-')[2];
  129. setTimeout(function() {
  130. parametry.razem = razem;
  131. console.log(parametry);
  132.  
  133. $("#wyniki").html("");
  134.  
  135. if (parametry.month && parametry.day) {
  136. $.ajax({
  137. url: "imieniny",
  138. type: 'POST',
  139. dataType: 'json',
  140. data: JSON.stringify(parametry),
  141. contentType: 'application/json',
  142. mimeType: 'application/json',
  143. success: function(data) {
  144. console.log(data);
  145. $("#wyniki").html("");
  146. if (Array.isArray(data.imiona)) {
  147. $.each(data.imiona, function(index, wynik) {
  148. $("#wyniki").append("<div class='wynik'>" + wynik + "</div>");
  149. })
  150. } else {
  151. $("#wyniki").append("<div class='wynik'>" + data.imiona + "</div>");
  152. }
  153.  
  154. },
  155. error: function(data, status, err) {
  156. console.log(data, status, err);
  157. }
  158. });
  159. }
  160. }, 50);
  161. }
  162.  
  163. </script>
  164.  
  165. </head>
  166. <body>
  167.  
  168. <div class="container">
  169. <div class="title">
  170. Michał Raźny, L2
  171. </div>
  172.  
  173. <div class="date-text">
  174. Proszę wskazać datę
  175. </div>
  176.  
  177. <div class="input">
  178. <label><input type="checkbox" id="razem" onchange="javascript:wyslijAJAX();">Wszystkie imiona razem</label>
  179. <input type="date" id="data" onkeyup="javascript:wyslijAJAX();" onchange="javascript:wyslijAJAX();"/>
  180. </div>
  181.  
  182. <div id="wyniki">
  183.  
  184. </div>
  185.  
  186. </div>
  187.  
  188. </body>
  189. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement