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 d7a72a3..0cdb947 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 37da3fd..3812b56 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 43ffe8d..e6d891e 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 f1c9ef3..0d2d105 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 a1d223c..8cc613d 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 5428f7a..8ce40ed 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