Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- import javax.xml.parsers.*;
- import org.w3c.dom.*;
- import org.xml.sax.SAXException;
- public class connectionTest extends HttpServlet {
- final private String driver = "com.mysql.jdbc.Driver";
- final private String dbms_url = "jdbc:mysql://127.0.0.1:3306/";
- final private String database = "Prenotazioni";
- final private String user = "root";
- final private String password = "";
- private Connection connessione;
- private boolean connected;
- // attivazione servlet (connessione a DBMS)
- public void init() {
- String url = dbms_url + database;
- try {
- Class.forName(driver);
- connessione = DriverManager.getConnection(url, user, password);
- connected = true;
- } catch (SQLException exception) {
- connected = false;
- } catch (ClassNotFoundException exception) {
- connected = false;
- }
- }
- // disattivazione servlet (disconnessione da DBMS)
- public void destroy() {
- try {
- connessione.close();
- } catch (SQLException exception) {
- }
- }
- // richiesta GET
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String cliente;
- String risultato;
- String url;
- String[] url_section;
- // verifica stato connessione a DBMS
- if (!connected) {
- response.sendError(500, "DBMS server error!");
- return;
- }
- // estrazione nominativo da URL
- url = request.getRequestURL().toString();
- url_section = url.split("/");
- cliente = url_section[url_section.length - 1];
- if (cliente == null) {
- response.sendError(400, "Request syntax error!");
- return;
- }
- if (cliente.isEmpty()) {
- response.sendError(400, "Request syntax error!");
- return;
- }
- try {
- // ricerca nominativo nel database
- Statement statement = connessione.createStatement();
- ResultSet result = statement.executeQuery( "SELECT * FROM Appuntamenti WHERE ModelloVeicolo = 'Fiat Panda 4x4';");//'" + name +"';");
- if (result.next()) {
- risultato = result.getString(1);
- } else {
- response.sendError(404, "Entry not found!");
- result.close();
- statement.close();
- return;
- }
- result.close();
- statement.close();
- // scrittura del body della risposta
- response.setContentType("text/xml;charset=UTF-8");
- PrintWriter out = response.getWriter();
- try { // ipotesi su come strutturare xml da mettere in cima al file
- out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
- out.println("<dati>");
- out.print("<appuntamento>");
- out.print(risultato);
- out.println("</appuntamento>");
- out.println("</dati>");
- } finally {
- out.close();
- }
- response.setStatus(200); // OK
- } catch (SQLException exception) {
- response.sendError(500, "DBMS server error!");
- }
- }
- // richiesta POST
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String line;
- // verifica stato connessione a DBMS
- if (!connected) {
- response.sendError(500, "DBMS server error!");
- return;
- }
- try {
- // scrittura nel file "entry.xml" del body della richiesta
- BufferedReader input = request.getReader();
- BufferedWriter file = new BufferedWriter(new FileWriter("entry.xml"));
- while ((line = input.readLine()) != null) {
- file.write(line);
- file.newLine();
- }
- input.close();
- file.flush();
- file.close();
- // estrazione dei valori degli elementi "name" e "number"
- // dal file "entry.xml"
- DocumentBuilderFactory factory
- = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder = factory.newDocumentBuilder();
- Document document = builder.parse("entry.xml");
- Element root = document.getDocumentElement();
- NodeList list = root.getElementsByTagName("cliente");
- String cliente = null;
- if (list != null && list.getLength() > 0) {
- cliente = list.item(0).getFirstChild().getNodeValue();
- }
- list = root.getElementsByTagName("veicolo");
- String modelloVeicolo = null;
- if (list != null && list.getLength() > 0) {
- modelloVeicolo = list.item(0).getFirstChild().getNodeValue();
- }
- list = root.getElementsByTagName("appuntamento");
- String appuntamento = null;
- if (list != null && list.getLength() > 0) {
- appuntamento = list.item(0).getFirstChild().getNodeValue();
- }
- if (cliente == null || modelloVeicolo == null || appuntamento == null) {
- response.sendError(400, "Malformed XML!");
- return;
- }
- if (cliente.isEmpty() || modelloVeicolo.isEmpty() || appuntamento.isEmpty()) {
- response.sendError(400, "Malformed XML!");
- return;
- }
- try {
- // aggiunta voce nel database
- Statement statement = connessione.createStatement();
- if (statement.executeUpdate( "INSERT INTO Appuntamenti(cliente, ModelloVeicolo, Appuntamento) VALUES('" + cliente + "' , '" + modelloVeicolo + "' ,'" + appuntamento + "' );") <= 0) {
- response.sendError(403, "Name exist!");
- statement.close();
- return;
- }
- statement.close();
- } catch (SQLException exception) {
- response.sendError(500, "DBMS server error!");
- return;
- }
- response.setStatus(201); // OK
- } catch (ParserConfigurationException exception) {
- response.sendError(500, "XML parser error!");
- } catch (SAXException exception) {
- response.sendError(500, "XML parser error!");
- }
- }
- // richiesta PUT
- protected void doPut(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String url_cliente;
- String url;
- String line;
- String[] url_section;
- // verifica stato connessione a DBMS
- if (!connected) {
- response.sendError(500, "DBMS server error!");
- return;
- }
- // estrazione nominativo da URL
- url = request.getRequestURL().toString();
- url_section = url.split("/");
- url_cliente = url_section[url_section.length - 1];
- if (url_cliente == null) {
- response.sendError(400, "Request syntax error!");
- return;
- }
- if (url_cliente.isEmpty()) {
- response.sendError(400, "Request syntax error!");
- return;
- }
- try {
- // scrittura nel file "entry.xml" del body della richiesta
- BufferedReader input = request.getReader();
- BufferedWriter file = new BufferedWriter(new FileWriter("entry.xml"));
- while ((line = input.readLine()) != null) {
- file.write(line);
- file.newLine();
- }
- input.close();
- file.flush();
- file.close();
- // estrazione dei valori degli elementi "name" e "number"
- // dal file "entry.xml"
- DocumentBuilderFactory factory
- = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder = factory.newDocumentBuilder();
- Document document = builder.parse("entry.xml");
- Element root = document.getDocumentElement();
- NodeList list = root.getElementsByTagName("cliente");
- String cliente = null;
- if (list != null && list.getLength() > 0) {
- cliente = list.item(0).getFirstChild().getNodeValue();
- }
- list = root.getElementsByTagName("veicolo");
- String modelloVeicolo = null;
- if (list != null && list.getLength() > 0) {
- modelloVeicolo = list.item(0).getFirstChild().getNodeValue();
- }
- list = root.getElementsByTagName("appuntamento");
- String appuntamento = null;
- if (list != null && list.getLength() > 0) {
- appuntamento = list.item(0).getFirstChild().getNodeValue();
- }
- if (cliente == null || modelloVeicolo == null || appuntamento == null) {
- response.sendError(400, "Malformed XML!");
- return;
- }
- if (cliente.isEmpty() || modelloVeicolo.isEmpty() || appuntamento.isEmpty()) {
- response.sendError(400, "Malformed XML!");
- return;
- }
- if (!cliente.equalsIgnoreCase(url_cliente)) {
- response.sendError(400, "URL name mismtach XML name!");
- return;
- }
- try {
- Statement statement = connessione.createStatement();
- if (statement.executeUpdate("UPDATE Appuntamenti SET ModelloVeicolo='" + modelloVeicolo + "'WHERE Cliente = '" + cliente + "';") <= 0) {
- response.sendError(404, "Entry not found!");
- statement.close();
- return;
- }
- statement.close();
- } catch (SQLException exception) {
- response.sendError(500, "DBMS server error!");
- return;
- }
- response.setStatus(204); // OK
- } catch (ParserConfigurationException exception) {
- response.sendError(500, "XML parser error!");
- } catch (SAXException exception) {
- response.sendError(500, "XML parser error!");
- }
- }
- // richiesta DELETE
- protected void doDelete(HttpServletRequest request,
- HttpServletResponse response)
- throws ServletException, IOException {
- String cliente;
- String url;
- String[] url_section;
- // verifica stato connessione a DBMS
- if (!connected) {
- response.sendError(500, "DBMS server error!");
- return;
- }
- // estrazione nominativo da URL
- url = request.getRequestURL().toString();
- url_section = url.split("/");
- cliente = url_section[url_section.length - 1];
- if (cliente == null) {
- response.sendError(400, "Request syntax error!");
- return;
- }
- if (cliente.isEmpty()) {
- response.sendError(400, "Request syntax error!");
- return;
- }
- try {
- Statement statement = connessione.createStatement();
- if (statement.executeUpdate("DELETE FROM Prenotazioni WHERE Cliente = '"
- + cliente + "';") <= 0) {
- response.sendError(404, "Entry not found!");
- statement.close();
- return;
- }
- statement.close();
- response.setStatus(204); // OK
- } catch (SQLException exception) {
- response.sendError(500, "DBMS server error!");
- return;
- }
- }
- // richiesta informazioni servlet
- public String getServletInfo() {
- return "Prenotazione";
- }
- }
Add Comment
Please, Sign In to add comment