Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. package ssi;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.util.ArrayList;
  6.  
  7. import javax.print.DocFlavor.STRING;
  8. import javax.servlet.ServletException;
  9. import javax.servlet.annotation.WebServlet;
  10. import javax.servlet.http.HttpServlet;
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletResponse;
  13.  
  14. import org.json.JSONObject;
  15.  
  16. /**
  17. * Servlet implementation class sugestieXML
  18. */
  19. @WebServlet("/sugestieXML")
  20. public class sugestieJSON extends HttpServlet {
  21. private static final long serialVersionUID = 1L;
  22.  
  23. /**
  24. * @see HttpServlet#HttpServlet()
  25. */
  26. public sugestieJSON() {
  27. super();
  28. // TODO Auto-generated constructor stub
  29. }
  30.  
  31. /**
  32. * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  33. */
  34. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  35. // TODO Auto-generated method stub
  36. response.getWriter().append("Served at: ").append(request.getContextPath());
  37. }
  38.  
  39. /**
  40. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  41. */
  42. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  43. response.setContentType("application/json;charset=UTF-8");
  44. request.setCharacterEncoding("UTF-8");
  45.  
  46. String []lista = {"ALFA ROMEO", "Audi", "BMW", "CITROEN", "TOYOTA",
  47. "SUZUKI", "SEAT", "CITROEN", "SAAB"};
  48.  
  49. String query = request.getParameter("query");
  50. PrintWriter out = response.getWriter();
  51.  
  52. JSONObject json = new JSONObject();
  53. ArrayList<String> sugestie = new ArrayList<String>();
  54.  
  55.  
  56. try {
  57.  
  58. for(String tmp: lista) {
  59. if(tmp.toLowerCase().startsWith(query.toLowerCase())) {
  60. sugestie.add('"' +tmp + '"');
  61.  
  62. }
  63.  
  64. }
  65.  
  66. }finally{
  67. json.put("sugestia", sugestie);
  68. out.println(json.toString());
  69. out.close();
  70. }
  71.  
  72. }
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement