· 9 years ago · Aug 21, 2017, 08:50 PM
1@SuppressWarnings("serial")
2public class CloudSqlServlet extends HttpServlet {
3
4 @Override
5 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException,
6 ServletException {
7 String path = req.getRequestURI();
8 if (path.startsWith("/favicon.ico")) {
9 return; // ignore the request for favicon.ico
10 }
11 // store only the first two octets of a users ip address
12 String userIp = req.getRemoteAddr();
13 InetAddress address = InetAddress.getByName(userIp);
14 if (address instanceof Inet6Address) {
15 // nest indexOf calls to find the second occurrence of a character in a string
16 // an alternative is to use Apache Commons Lang: StringUtils.ordinalIndexOf()
17 userIp = userIp.substring(0, userIp.indexOf(":", userIp.indexOf(":") + 1)) + ":*:*:*:*:*:*";
18 } else if (address instanceof Inet4Address) {
19 userIp = userIp.substring(0, userIp.indexOf(".", userIp.indexOf(".") + 1)) + ".*.*";
20 }
21
22 final String createTableSql = "CREATE TABLE IF NOT EXISTS visits ( visit_id INT NOT NULL "
23 + "AUTO_INCREMENT, user_ip VARCHAR(46) NOT NULL, nombre VARCHAR(46) NOT NULL)";
24 final String selectSql = "SELECT * from visits;";
25 PrintWriter out = resp.getWriter();
26 resp.setContentType("text/plain");
27 String url;
28 if (System.getProperty("com.google.appengine.runtime.version").startsWith("Google App Engine/")) {
29 // Check the System properties to determine if we are running on appengine or not
30 // Google App Engine sets a few system properties that will reliably be present on a remote
31 // instance.
32 url = System.getProperty("ae-cloudsql.local-database-url");
33 try {
34 System.out.println("Entra en appengine cloud");
35 // Load the class that provides the new "jdbc:google:mysql://" prefix.
36 Class.forName("com.mysql.jdbc.GoogleDriver");
37 } catch (ClassNotFoundException e) {
38 throw new ServletException("Error loading Google JDBC Driver", e);
39 }
40 } else {
41 System.out.println("NO entra en appengine cloud");
42 // Set the url with the local MySQL database connection url when running locally
43 url = System.getProperty("ae-cloudsql.local-database-url");
44 }
45 try (Connection conn = DriverManager.getConnection(url)){
46
47 try (ResultSet rs = conn.prepareStatement(selectSql).executeQuery()) {
48 out.print("Datos:");
49 while (rs.next()) {
50 String savedIp = rs.getString("user_ip");
51 String name = rs.getString("nombre");
52 out.print("IP: " + name + " Nombre: " + savedIp);
53 }
54 }
55 } catch (SQLException e) {
56 throw new ServletException("SQL error", e);
57 }
58 }
59}
60
61<?xml version="1.0" encoding="utf-8"?>
62<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
63 <application>myApp</application>
64 <version>1</version>
65
66
67 <threadsafe>true</threadsafe>
68 <use-google-connector-j>true</use-google-connector-j>
69
70 <!-- Configure java.util.logging -->
71 <system-properties>
72 <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
73 </system-properties>
74
75 <system-properties>
76 <property name="ae-cloudsql.cloudsql-database-url" value="jdbc:google:mysql://project-id-4811673067964427457:europe-west1:con-bd-myApp/BD-myApp?user=root;password=root"/>
77 <property name="ae-cloudsql.local-database-url" value="jdbc:mysql://104.155.83.62/BD-myApp?user=root;password=root"/>
78 </system-properties>
79
80
81 <!--
82 HTTP Sessions are disabled by default. To enable HTTP sessions specify:
83
84 <sessions-enabled>true</sessions-enabled>
85
86 It's possible to reduce request latency by configuring your application to
87 asynchronously write HTTP session data to the datastore:
88
89 <async-session-persistence enabled="true" />
90
91 With this feature enabled, there is a very small chance your app will see
92 stale session data. For details, see
93 http://code.google.com/appengine/docs/java/config/appconfig.html#Enabling_Sessions
94 -->
95
96</appengine-web-app>
97
98<dependency> <!-- Local -->
99 <groupId>mysql</groupId>
100 <artifactId>mysql-connector-java</artifactId>
101 <version>5.1.40</version> <!-- v5.x.x is Java 7, v6.x.x is Java 8 -->
102</dependency>
103<dependency> <!-- Cloud Sql -->
104 <groupId>com.google.cloud.sql</groupId>
105 <artifactId>mysql-socket-factory</artifactId>
106 <version>1.0.2</version>
107</dependency>