Finish exo9 code and fix unicode support where possible.
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 11 Apr 2018 19:34:35 +0000 (21:34 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 11 Apr 2018 19:34:35 +0000 (21:34 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
ROOT/exo4/script-page-contenttype-xml.jsp
ROOT/exo7/saveinfos.jsp
ROOT/exo7/setcookie.jsp
ROOT/exo8/getinfos.jsp
ROOT/exo8/saveinfos.jsp
ROOT/exo9/addbook.jsp [new file with mode: 0644]
ROOT/exo9/connectJspMysql.jsp
ROOT/exo9/viewbooks.jsp [new file with mode: 0644]

index d7a72a3ed4c9cf5987f1d75c543956833eacd451..0cdb947790ba3b8770d86b921428de9dc0685405 100644 (file)
@@ -1,4 +1,4 @@
-<%@ page contentType="text/xml, charset=ISO-8859-1" %>
+<%@ page contentType="text/xml; charset=ISO-8859-1" %>
 <html>
 <body>
 
index 37da3fd1a4f5f61946ec0ab6baaf8463cfbc1a35..3812b56ee00921f0cc6985664898365c6c28f02a 100644 (file)
@@ -1,4 +1,5 @@
 <%
+request.setCharacterEncoding("UTF-8");
 String firstname = request.getParameter("firstname");
 String lastname = request.getParameter("lastname");
 String address = request.getParameter("address");
index 43ffe8db44d62f7bf742de99ac14e2eba0ef2f7f..e6d891eb6c68b1fbc38a0364be9ed8b55e1c294f 100644 (file)
@@ -1,5 +1,6 @@
 <%@ page import="java.io.*, java.util.*, javax.servlet.*" %>
 <%
+request.setCharacterEncoding("UTF-8");
 String firstname = request.getParameter("firstname");
 String lastname = request.getParameter("lastname");
 String username = firstname + lastname;
index f1c9ef345f0f81c7d7b2f587b46fb62ba7b845f8..0d2d1055428d32b9ceb4c4b97a1a97de071a594c 100644 (file)
@@ -1,4 +1,4 @@
-<%@ page contentType="text/html" pageEncoding="UTF-8" %>
+<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
 <jsp:useBean id="user" class="user.UserData" scope="session" />
 <meta charset="UTF-8">
 <html>
index a1d223c012e787a34fb5255ef6856e2ff904b4aa..8cc613da7a1bc7ab875058e592471e9ab1521d1a 100644 (file)
@@ -1,4 +1,4 @@
-<%@ page contentType="text/html" pageEncoding="UTF-8" %>
+<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
 <jsp:useBean id="user" class="user.UserData" scope="session" />
 <jsp:setProperty name="user" property="*"/>
 <meta charset="UTF-8">
diff --git a/ROOT/exo9/addbook.jsp b/ROOT/exo9/addbook.jsp
new file mode 100644 (file)
index 0000000..a0fb035
--- /dev/null
@@ -0,0 +1,52 @@
+<%@ page import="java.io.*, java.util.*, javax.servlet.*, java.sql.*" %>
+<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
+
+<meta charset="UTF-8">
+<html>
+<body>
+
+<h1>Ajouter un livre</h1>
+<form method="POST" action="addbook.jsp?new=true">
+  Titre:<br>
+  <input type="text" name="title"><br>
+  Auteur:<br>
+  <input type="text" name="author"><br><br>
+  <input type="submit" value="Submit">
+</form>
+
+<%
+request.setCharacterEncoding("UTF-8");
+String isNew = request.getParameter("new");
+
+if (isNew != null && isNew.equals("true")) {
+    String title = request.getParameter("title");
+    String author = request.getParameter("author");
+    Connection connection = null;
+    PreparedStatement stmt = null;
+    try {
+        String connectionURL = "jdbc:mysql://localhost/MyBd?useUnicode=yes&characterEncoding=UTF-8";
+        Class.forName("com.mysql.jdbc.Driver").newInstance();
+        connection = DriverManager.getConnection(connectionURL, "MyBd", "MyBd");
+        if(!connection.isClosed()) {
+            stmt = connection.prepareStatement("INSERT INTO details_livres (nom_livre, auteur) VALUES (?, ?)");
+            stmt.setString(1, title);
+            stmt.setString(2, author);
+            stmt.executeUpdate();
+            out.println("Book " + title + " by " + author + " successfully added");
+        }
+        } catch (Exception e){
+            out.println("Unable to connect to database or run query: " + e);
+        }
+        finally {
+            if (connection != null)
+                connection.close();
+            if (stmt != null)
+                stmt.close();
+        }
+        
+}
+
+%> 
+
+</body>
+</html>
\ No newline at end of file
index 5428f7a6845d7d044c70c2bc83fff843902a1805..8ce40ed9faca33a3a97bdf9956fcec6240826ca3 100644 (file)
@@ -1,16 +1,19 @@
 <%@ page import="java.io.*, java.util.*, javax.servlet.*, java.sql.*" %>
 <%
 
+Connection connection = null;
 try {
-    String connectionURL = "jdbc:mysql://localhost/MyBd";
-    Connection connection = null;
+    String connectionURL = "jdbc:mysql://localhost/MyBd?useUnicode=yes&characterEncoding=UTF-8";
     Class.forName("com.mysql.jdbc.Driver").newInstance();
     connection = DriverManager.getConnection(connectionURL, "MyBd", "MyBd");
     if(!connection.isClosed())
         out.println("Successfully connected to " + connectionURL + " MySQL server using TCP/IP");
-    connection.close();
     } catch (Exception e){
-            out.println("Unable to connect to database: " + e);
+        out.println("Unable to connect to database: " + e);
+    }
+    finally {
+        if (connection != null)
+            connection.close();
     }
 
 %>
diff --git a/ROOT/exo9/viewbooks.jsp b/ROOT/exo9/viewbooks.jsp
new file mode 100644 (file)
index 0000000..02b2591
--- /dev/null
@@ -0,0 +1,42 @@
+<%@ page import="java.io.*, java.util.*, javax.servlet.*, java.sql.*" %>
+<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
+
+<meta charset="UTF-8">
+<html>
+<body>
+
+<h1>Liste des livres</h1>
+
+<%
+
+Connection connection = null;
+Statement stmt = null;
+try {
+    String connectionURL = "jdbc:mysql://localhost/MyBd?useUnicode=yes&characterEncoding=UTF-8";
+    Class.forName("com.mysql.jdbc.Driver").newInstance();
+    connection = DriverManager.getConnection(connectionURL, "MyBd", "MyBd");
+    if(!connection.isClosed()) {
+        String query = "SELECT nom_livre, auteur FROM details_livres";
+        stmt = connection.createStatement();
+        ResultSet rs = stmt.executeQuery(query);
+        while (rs.next()) {
+            String title = rs.getString("nom_livre");
+            String author = rs.getString("auteur");
+            //TODO: make a html table
+            out.println(title + " par " + author + "<br>");
+        }
+    }
+    } catch (Exception e){
+        out.println("Unable to connect to database or run query: " + e);
+    }
+    finally {
+        if (connection != null)
+            connection.close();
+        if (stmt != null)
+            stmt.close();
+    }
+
+%> 
+
+</body>
+</html>
\ No newline at end of file