Advertisement
Guest User

Untitled

a guest
May 5th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.20 KB | None | 0 0
  1. import java.sql.*;
  2. import java.io.*;
  3. import javax.servlet.*;
  4. import javax.servlet.http.*;
  5. import javax.xml.parsers.*;
  6. import org.w3c.dom.*;
  7. import org.xml.sax.SAXException;
  8.  
  9. public class connectionTest extends HttpServlet {
  10.  
  11. final private String driver = "com.mysql.jdbc.Driver";
  12. final private String dbms_url = "jdbc:mysql://127.0.0.1:3306/";
  13. final private String database = "Prenotazioni";
  14. final private String user = "root";
  15. final private String password = "";
  16. private Connection connessione;
  17. private boolean connected;
  18. // attivazione servlet (connessione a DBMS)
  19.  
  20. public void init() {
  21. String url = dbms_url + database;
  22. try {
  23. Class.forName(driver);
  24. connessione = DriverManager.getConnection(url, user, password);
  25. connected = true;
  26. } catch (SQLException exception) {
  27. connected = false;
  28. } catch (ClassNotFoundException exception) {
  29. connected = false;
  30. }
  31. }
  32. // disattivazione servlet (disconnessione da DBMS)
  33.  
  34. public void destroy() {
  35. try {
  36. connessione.close();
  37. } catch (SQLException exception) {
  38. }
  39. }
  40. // richiesta GET
  41.  
  42. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  43. throws ServletException, IOException {
  44. String cliente;
  45. String risultato;
  46. String url;
  47. String[] url_section;
  48. // verifica stato connessione a DBMS
  49.  
  50. if (!connected) {
  51. response.sendError(500, "DBMS server error!");
  52. return;
  53. }
  54. // estrazione nominativo da URL
  55.  
  56. url = request.getRequestURL().toString();
  57. url_section = url.split("/");
  58. cliente = url_section[url_section.length - 1];
  59. if (cliente == null) {
  60. response.sendError(400, "Request syntax error!");
  61. return;
  62. }
  63. if (cliente.isEmpty()) {
  64. response.sendError(400, "Request syntax error!");
  65. return;
  66. }
  67. try {
  68.  
  69. // ricerca nominativo nel database
  70. Statement statement = connessione.createStatement();
  71.  
  72. ResultSet result = statement.executeQuery( "SELECT * FROM Appuntamenti WHERE ModelloVeicolo = 'Fiat Panda 4x4';");//'" + name +"';");
  73.  
  74. if (result.next()) {
  75.  
  76. risultato = result.getString(1);
  77.  
  78. } else {
  79.  
  80. response.sendError(404, "Entry not found!");
  81.  
  82. result.close();
  83.  
  84. statement.close();
  85.  
  86. return;
  87.  
  88. }
  89.  
  90. result.close();
  91.  
  92. statement.close();
  93.  
  94. // scrittura del body della risposta
  95. response.setContentType("text/xml;charset=UTF-8");
  96.  
  97. PrintWriter out = response.getWriter();
  98.  
  99. try { // ipotesi su come strutturare xml da mettere in cima al file
  100.  
  101. out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  102.  
  103. out.println("<dati>");
  104.  
  105. out.print("<appuntamento>");
  106.  
  107. out.print(risultato);
  108.  
  109. out.println("</appuntamento>");
  110.  
  111. out.println("</dati>");
  112.  
  113. } finally {
  114.  
  115. out.close();
  116.  
  117. }
  118.  
  119. response.setStatus(200); // OK
  120.  
  121. } catch (SQLException exception) {
  122.  
  123. response.sendError(500, "DBMS server error!");
  124.  
  125. }
  126.  
  127. }
  128.  
  129. // richiesta POST
  130. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  131. throws ServletException, IOException {
  132.  
  133. String line;
  134.  
  135.  
  136.  
  137. // verifica stato connessione a DBMS
  138. if (!connected) {
  139.  
  140. response.sendError(500, "DBMS server error!");
  141.  
  142. return;
  143.  
  144. }
  145.  
  146. try {
  147.  
  148. // scrittura nel file "entry.xml" del body della richiesta
  149. BufferedReader input = request.getReader();
  150.  
  151. BufferedWriter file = new BufferedWriter(new FileWriter("entry.xml"));
  152. while ((line = input.readLine()) != null) {
  153.  
  154. file.write(line);
  155.  
  156. file.newLine();
  157.  
  158. }
  159.  
  160. input.close();
  161.  
  162. file.flush();
  163.  
  164. file.close();
  165.  
  166. // estrazione dei valori degli elementi "name" e "number"
  167. // dal file "entry.xml"
  168. DocumentBuilderFactory factory
  169. = DocumentBuilderFactory.newInstance();
  170.  
  171. DocumentBuilder builder = factory.newDocumentBuilder();
  172.  
  173. Document document = builder.parse("entry.xml");
  174.  
  175. Element root = document.getDocumentElement();
  176.  
  177. NodeList list = root.getElementsByTagName("cliente");
  178.  
  179. String cliente = null;
  180.  
  181. if (list != null && list.getLength() > 0) {
  182. cliente = list.item(0).getFirstChild().getNodeValue();
  183. }
  184.  
  185. list = root.getElementsByTagName("veicolo");
  186.  
  187. String modelloVeicolo = null;
  188.  
  189. if (list != null && list.getLength() > 0) {
  190. modelloVeicolo = list.item(0).getFirstChild().getNodeValue();
  191. }
  192.  
  193. list = root.getElementsByTagName("appuntamento");
  194.  
  195. String appuntamento = null;
  196.  
  197. if (list != null && list.getLength() > 0) {
  198. appuntamento = list.item(0).getFirstChild().getNodeValue();
  199. }
  200.  
  201. if (cliente == null || modelloVeicolo == null || appuntamento == null) {
  202.  
  203. response.sendError(400, "Malformed XML!");
  204.  
  205. return;
  206.  
  207. }
  208.  
  209. if (cliente.isEmpty() || modelloVeicolo.isEmpty() || appuntamento.isEmpty()) {
  210.  
  211. response.sendError(400, "Malformed XML!");
  212.  
  213. return;
  214.  
  215. }
  216.  
  217. try {
  218.  
  219. // aggiunta voce nel database
  220. Statement statement = connessione.createStatement();
  221.  
  222. if (statement.executeUpdate( "INSERT INTO Appuntamenti(cliente, ModelloVeicolo, Appuntamento) VALUES('" + cliente + "' , '" + modelloVeicolo + "' ,'" + appuntamento + "' );") <= 0) {
  223.  
  224.  
  225. response.sendError(403, "Name exist!");
  226.  
  227. statement.close();
  228.  
  229. return;
  230.  
  231. }
  232.  
  233. statement.close();
  234.  
  235. } catch (SQLException exception) {
  236.  
  237. response.sendError(500, "DBMS server error!");
  238.  
  239. return;
  240.  
  241. }
  242.  
  243. response.setStatus(201); // OK
  244.  
  245. } catch (ParserConfigurationException exception) {
  246.  
  247. response.sendError(500, "XML parser error!");
  248.  
  249. } catch (SAXException exception) {
  250.  
  251. response.sendError(500, "XML parser error!");
  252.  
  253. }
  254.  
  255. }
  256.  
  257. // richiesta PUT
  258. protected void doPut(HttpServletRequest request, HttpServletResponse response)
  259. throws ServletException, IOException {
  260.  
  261. String url_cliente;
  262.  
  263. String url;
  264.  
  265. String line;
  266.  
  267. String[] url_section;
  268.  
  269. // verifica stato connessione a DBMS
  270. if (!connected) {
  271.  
  272. response.sendError(500, "DBMS server error!");
  273.  
  274. return;
  275.  
  276. }
  277.  
  278. // estrazione nominativo da URL
  279. url = request.getRequestURL().toString();
  280.  
  281. url_section = url.split("/");
  282.  
  283. url_cliente = url_section[url_section.length - 1];
  284.  
  285. if (url_cliente == null) {
  286.  
  287. response.sendError(400, "Request syntax error!");
  288.  
  289. return;
  290.  
  291. }
  292.  
  293. if (url_cliente.isEmpty()) {
  294.  
  295. response.sendError(400, "Request syntax error!");
  296.  
  297. return;
  298.  
  299. }
  300.  
  301. try {
  302.  
  303. // scrittura nel file "entry.xml" del body della richiesta
  304. BufferedReader input = request.getReader();
  305.  
  306. BufferedWriter file = new BufferedWriter(new FileWriter("entry.xml"));
  307.  
  308. while ((line = input.readLine()) != null) {
  309.  
  310. file.write(line);
  311.  
  312. file.newLine();
  313.  
  314. }
  315.  
  316. input.close();
  317.  
  318. file.flush();
  319.  
  320. file.close();
  321.  
  322. // estrazione dei valori degli elementi "name" e "number"
  323. // dal file "entry.xml"
  324. DocumentBuilderFactory factory
  325. = DocumentBuilderFactory.newInstance();
  326.  
  327. DocumentBuilder builder = factory.newDocumentBuilder();
  328.  
  329. Document document = builder.parse("entry.xml");
  330.  
  331. Element root = document.getDocumentElement();
  332.  
  333. NodeList list = root.getElementsByTagName("cliente");
  334.  
  335. String cliente = null;
  336.  
  337. if (list != null && list.getLength() > 0) {
  338. cliente = list.item(0).getFirstChild().getNodeValue();
  339. }
  340.  
  341. list = root.getElementsByTagName("veicolo");
  342.  
  343. String modelloVeicolo = null;
  344.  
  345. if (list != null && list.getLength() > 0) {
  346. modelloVeicolo = list.item(0).getFirstChild().getNodeValue();
  347. }
  348.  
  349. list = root.getElementsByTagName("appuntamento");
  350.  
  351. String appuntamento = null;
  352.  
  353. if (list != null && list.getLength() > 0) {
  354. appuntamento = list.item(0).getFirstChild().getNodeValue();
  355. }
  356.  
  357. if (cliente == null || modelloVeicolo == null || appuntamento == null) {
  358.  
  359. response.sendError(400, "Malformed XML!");
  360.  
  361. return;
  362.  
  363. }
  364.  
  365. if (cliente.isEmpty() || modelloVeicolo.isEmpty() || appuntamento.isEmpty()) {
  366.  
  367. response.sendError(400, "Malformed XML!");
  368.  
  369. return;
  370.  
  371. }
  372.  
  373. if (!cliente.equalsIgnoreCase(url_cliente)) {
  374.  
  375. response.sendError(400, "URL name mismtach XML name!");
  376.  
  377. return;
  378.  
  379. }
  380.  
  381. try {
  382.  
  383. Statement statement = connessione.createStatement();
  384.  
  385. if (statement.executeUpdate("UPDATE Appuntamenti SET ModelloVeicolo='" + modelloVeicolo + "'WHERE Cliente = '" + cliente + "';") <= 0) {
  386.  
  387. response.sendError(404, "Entry not found!");
  388.  
  389. statement.close();
  390.  
  391. return;
  392.  
  393. }
  394.  
  395. statement.close();
  396.  
  397. } catch (SQLException exception) {
  398.  
  399. response.sendError(500, "DBMS server error!");
  400.  
  401. return;
  402.  
  403. }
  404.  
  405. response.setStatus(204); // OK
  406.  
  407. } catch (ParserConfigurationException exception) {
  408.  
  409. response.sendError(500, "XML parser error!");
  410.  
  411. } catch (SAXException exception) {
  412.  
  413. response.sendError(500, "XML parser error!");
  414.  
  415. }
  416.  
  417. }
  418.  
  419. // richiesta DELETE
  420. protected void doDelete(HttpServletRequest request,
  421. HttpServletResponse response)
  422. throws ServletException, IOException {
  423.  
  424. String cliente;
  425.  
  426. String url;
  427.  
  428. String[] url_section;
  429.  
  430. // verifica stato connessione a DBMS
  431. if (!connected) {
  432.  
  433. response.sendError(500, "DBMS server error!");
  434.  
  435. return;
  436.  
  437. }
  438.  
  439. // estrazione nominativo da URL
  440. url = request.getRequestURL().toString();
  441.  
  442. url_section = url.split("/");
  443.  
  444. cliente = url_section[url_section.length - 1];
  445.  
  446. if (cliente == null) {
  447.  
  448. response.sendError(400, "Request syntax error!");
  449.  
  450. return;
  451.  
  452. }
  453.  
  454. if (cliente.isEmpty()) {
  455.  
  456. response.sendError(400, "Request syntax error!");
  457.  
  458. return;
  459.  
  460. }
  461.  
  462. try {
  463.  
  464. Statement statement = connessione.createStatement();
  465.  
  466. if (statement.executeUpdate("DELETE FROM Prenotazioni WHERE Cliente = '"
  467. + cliente + "';") <= 0) {
  468.  
  469. response.sendError(404, "Entry not found!");
  470.  
  471. statement.close();
  472.  
  473. return;
  474.  
  475. }
  476.  
  477. statement.close();
  478.  
  479. response.setStatus(204); // OK
  480.  
  481. } catch (SQLException exception) {
  482.  
  483. response.sendError(500, "DBMS server error!");
  484.  
  485. return;
  486.  
  487. }
  488.  
  489. }
  490.  
  491. // richiesta informazioni servlet
  492. public String getServletInfo() {
  493.  
  494. return "Prenotazione";
  495.  
  496. }
  497.  
  498. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement