Advertisement
Sothian

SSI_Podsumowanie_Lab2

Oct 21st, 2019
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.10 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.  * Servlet implementation class sugestieJSON
  20.  */
  21. @WebServlet("/MOsugestie")
  22. public class MOsugestie extends HttpServlet {
  23.     private static final long serialVersionUID = 1L;
  24.  
  25.     /**
  26.      * @see HttpServlet#HttpServlet()
  27.      */
  28.     public MOsugestie() {
  29.         super();
  30.         // TODO Auto-generated constructor stub
  31.     }
  32.  
  33.     /**
  34.      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
  35.      *      response)
  36.      */
  37.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  38.             throws ServletException, IOException {
  39.         // TODO Auto-generated method stub
  40.         response.getWriter().append("Served at: ").append(request.getContextPath());
  41.     }
  42.  
  43.     /**
  44.      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
  45.      *      response)
  46.      */
  47.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  48.             throws ServletException, IOException {
  49.         response.setContentType("application/json;charset=UTF-8");
  50.         request.setCharacterEncoding("UTF-8");
  51.  
  52.         String[] lista = { "alfa", "audi", "bmw", "citroen" };
  53.  
  54.         String jsonText = "";
  55.         BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
  56.  
  57.         if (br != null) {
  58.             jsonText = br.readLine();
  59.         }
  60.  
  61.         JSONObject json;
  62.         try {
  63.             json = new JSONObject(jsonText);
  64.         } catch (ParseException e1) {
  65.             json = new JSONObject();
  66.             // e1.printStackTrace();
  67.         }
  68.  
  69.         String query = json.getString("wartosc");
  70.         PrintWriter out = response.getWriter();
  71.  
  72.         String ifChecked = json.getString("zaznaczone");
  73.  
  74.         ArrayList<String> sugestie = new ArrayList<String>();
  75.  
  76.         try {
  77.             if(ifChecked == "off") {
  78.                 for (String tmp : lista) {
  79.                     if (tmp.startsWith(query)) {
  80.                         sugestie.add('"' + tmp + '"');
  81.  
  82.                     }
  83.                 }
  84.             }
  85.             else {
  86.                 for (String tmp : lista) {
  87.                     if (tmp.toLowerCase().startsWith(query.toLowerCase())) {
  88.                         sugestie.add('"' + tmp + '"');
  89.  
  90.                     }
  91.                 }
  92.             }  
  93.         } catch (Exception e) {
  94.             e.printStackTrace();
  95.         } finally {
  96.             json.put("sugestia", sugestie);
  97.             out.println(json.toString());
  98.             out.close();
  99.         }
  100.     }
  101.  
  102. }
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113. /////////////////////////////////////////////////////////////////////
  114. @charset "UTF-8";
  115.  
  116. * {
  117.     background-color: #ededed;
  118.     font-family: Arial;
  119. }
  120.  
  121. main {
  122.     text-align: center;
  123. }
  124.  
  125. h1 {
  126.     font-size: 30px;
  127.     margin: 30px 0;
  128.     font-weight: 700;
  129. }
  130. h2{
  131.     font-size: 26px;
  132.     margin: 30px 0;
  133.     font-weight: 700;
  134. }
  135.  
  136. .wyszukiwarka {
  137.     width: 70%;
  138.     margin: 0 auto;
  139.    
  140. }
  141. #pole {
  142.     width: 100%;
  143.     font-size: 22px;
  144.     height: 36px;
  145.     border-radius: 5px;
  146.     background-color: white;
  147.     border: 1px solid #000;
  148.     padding: 5px 10px;
  149. }
  150. #wyniki {
  151.     text-align: left;
  152.     width: 100%;
  153.     margin: 0 1%;
  154. }
  155. .lista {
  156.     background-color: #ccc;
  157.     font-size: 22px;
  158.     padding: 5px 10px;
  159. }
  160. .lista:hover {
  161.     background-color: #aaa;
  162. }
  163.  
  164. .fast-right {
  165.     float: right;
  166.     margin: 5px 0 10px 0;
  167. }
  168. //////////////////////////////////////////////////////////
  169. function wyslijAJAX() {
  170.        
  171.         //var parametry = {
  172.         //      "pole": $('#pole').val(),
  173.         //      "wartosc": $('#pole').val(),
  174.         //      "zaznaczone": $('#wielkoscLiter').val()
  175.         //}
  176.        
  177.         // Inny sposob
  178.         var parametry = new Object();
  179.         parametry.wartosc = $('#pole').val();
  180.         parametry.pole = $('#pole').val();
  181.        
  182.         if($('#wielkoscLiter').prop("checked") == true){
  183.             parametry.zaznaczone = "on";
  184.         } else {
  185.             parametry.zaznaczone = "off";
  186.         }
  187.        
  188.         if(parametry.wartosc) {
  189.             $.ajax({
  190.                 url: "MOsugestie",
  191.                 type: "POST",
  192.                 dataType: "json",
  193.                 data: JSON.stringify(parametry),
  194.                 contentType: "application/json",
  195.                 mimeType: "application/json",
  196.                
  197.                 success: function(data) {
  198.                     $("#wyniki").html("");
  199.                     $.each(data.sugestia, function(index, wynik) {
  200.                         $("#wyniki").append('<div class="lista">' + wynik + '</div>');
  201.                     });
  202.                 },
  203.            
  204.                 error: function(data,status,er) {
  205.                     alert("error: " + data +
  206.                             " status: " + status +
  207.                             " error: " + er);
  208.                 }
  209.             })
  210.         }
  211.        
  212.     }
  213.  
  214.  
  215.  
  216. //////////////////////////////////////////////////////////
  217. <!DOCTYPE html>
  218. <html>
  219. <head>
  220. <meta charset="UTF-8">
  221. <title>Mateusz Olejarz - SSI_Lab2</title>
  222. <link rel="stylesheet" href="css/reset.css">
  223. <link rel="stylesheet" href="css/style.css">
  224. </head>
  225.  
  226. <body>
  227.     <main>
  228.     <h1>Mateusz Olejarz, L1</h1>
  229.     <h2>Wpisz markฤ™ samochodow</h2>
  230.    
  231.         <section class="wyszukiwarka">
  232.             <div class="fast-right">
  233.                 <input type="checkbox" id="wielkoscLiter" onclick="wyslijAJAX()"> Pomiล„ wielkoล›ฤ‡ liter
  234.             </div>
  235.             <input id="pole" onkeyup="wyslijAJAX()" />
  236.             <div id="wyniki"></div>
  237.         </section>
  238.     </main>
  239.    
  240.     <script src="jquery-3.4.1.js"></script>
  241.     <script type="text/javascript" src="js/main.js"></script>
  242. </body>
  243. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement