From d2793705bda98fb61daa7811d9607f5002c2595e Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 11 Apr 2018 21:34:35 +0200 Subject: [PATCH] Finish exo9 code and fix unicode support where possible. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- ROOT/exo4/script-page-contenttype-xml.jsp | 2 +- ROOT/exo7/saveinfos.jsp | 1 + ROOT/exo7/setcookie.jsp | 1 + ROOT/exo8/getinfos.jsp | 2 +- ROOT/exo8/saveinfos.jsp | 2 +- ROOT/exo9/addbook.jsp | 52 +++++++++++++++++++++++ ROOT/exo9/connectJspMysql.jsp | 11 +++-- ROOT/exo9/viewbooks.jsp | 42 ++++++++++++++++++ 8 files changed, 106 insertions(+), 7 deletions(-) create mode 100644 ROOT/exo9/addbook.jsp create mode 100644 ROOT/exo9/viewbooks.jsp diff --git a/ROOT/exo4/script-page-contenttype-xml.jsp b/ROOT/exo4/script-page-contenttype-xml.jsp index d7a72a3..0cdb947 100644 --- a/ROOT/exo4/script-page-contenttype-xml.jsp +++ b/ROOT/exo4/script-page-contenttype-xml.jsp @@ -1,4 +1,4 @@ -<%@ page contentType="text/xml, charset=ISO-8859-1" %> +<%@ page contentType="text/xml; charset=ISO-8859-1" %> diff --git a/ROOT/exo7/saveinfos.jsp b/ROOT/exo7/saveinfos.jsp index 37da3fd..3812b56 100644 --- a/ROOT/exo7/saveinfos.jsp +++ b/ROOT/exo7/saveinfos.jsp @@ -1,4 +1,5 @@ <% +request.setCharacterEncoding("UTF-8"); String firstname = request.getParameter("firstname"); String lastname = request.getParameter("lastname"); String address = request.getParameter("address"); diff --git a/ROOT/exo7/setcookie.jsp b/ROOT/exo7/setcookie.jsp index 43ffe8d..e6d891e 100644 --- a/ROOT/exo7/setcookie.jsp +++ b/ROOT/exo7/setcookie.jsp @@ -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; diff --git a/ROOT/exo8/getinfos.jsp b/ROOT/exo8/getinfos.jsp index f1c9ef3..0d2d105 100644 --- a/ROOT/exo8/getinfos.jsp +++ b/ROOT/exo8/getinfos.jsp @@ -1,4 +1,4 @@ -<%@ page contentType="text/html" pageEncoding="UTF-8" %> +<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> diff --git a/ROOT/exo8/saveinfos.jsp b/ROOT/exo8/saveinfos.jsp index a1d223c..8cc613d 100644 --- a/ROOT/exo8/saveinfos.jsp +++ b/ROOT/exo8/saveinfos.jsp @@ -1,4 +1,4 @@ -<%@ page contentType="text/html" pageEncoding="UTF-8" %> +<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> diff --git a/ROOT/exo9/addbook.jsp b/ROOT/exo9/addbook.jsp new file mode 100644 index 0000000..a0fb035 --- /dev/null +++ b/ROOT/exo9/addbook.jsp @@ -0,0 +1,52 @@ +<%@ page import="java.io.*, java.util.*, javax.servlet.*, java.sql.*" %> +<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> + + + + + +

Ajouter un livre

+
+ Titre:
+
+ Auteur:
+

+ +
+ +<% +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(); + } + +} + +%> + + + \ No newline at end of file diff --git a/ROOT/exo9/connectJspMysql.jsp b/ROOT/exo9/connectJspMysql.jsp index 5428f7a..8ce40ed 100644 --- a/ROOT/exo9/connectJspMysql.jsp +++ b/ROOT/exo9/connectJspMysql.jsp @@ -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 index 0000000..02b2591 --- /dev/null +++ b/ROOT/exo9/viewbooks.jsp @@ -0,0 +1,42 @@ +<%@ page import="java.io.*, java.util.*, javax.servlet.*, java.sql.*" %> +<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> + + + + + +

Liste des livres

+ +<% + +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 + "
"); + } + } + } 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(); + } + +%> + + + \ No newline at end of file -- 2.34.1