blob: 39b0c70ea61394553cd6c7fe6488b3f6ecb6889b [file] [log] [blame]
<%@ page language="java" import="java.sql.*"%>
<title>Sample Application Database JSP Page</title>
<body bgcolor=white>
<table border="0">
<td align=center>
<img src="images/bridge-small.png">
<h1>Sample Brooklyn Deployed WebApp (Database JSP)</h1>
This is the output of a JSP page that is part of the Hello, World application,
deployed by brooklyn, to show <b>SQL database interactivity</b>.
String url=System.getProperty("brooklyn.example.db.url");
//URL should be supplied e.g. ""-Dbrooklyn.example.db.url=jdbc:mysql://localhost/visitors?user=brooklyn&password=br00k11n"
//(note quoting needed due to ampersand)
if (url==null) {
<p>(There is no database included as part of this example.)</p>
<% } else { /* begin database-enabled block */ %>
Connection con=null;
ResultSet rst=null;
Statement stmt=null;
int i=0;
try {
String DRIVER = "com.mysql.jdbc.Driver";
if (request.getParameter("name")!=null) {
//add a message
stmt.execute("INSERT INTO MESSAGES values (default, '"+
//better escaping and security desired...
//this essentially does StringEscapeUtils.escapeSql (either brooklyn.util or apache commons)
request.getParameter("name").replaceAll("'", "''")+
"', '"+
request.getParameter("message").replaceAll("'", "''")+
rst=stmt.executeQuery("select * from MESSAGES");
while ( {
<li> <b><%= rst.getString(2) %></b>: <%= rst.getString(3) %> </li>
} catch (Exception e) {
<li> <b>The database does not appear to be connected.</b> </li>
<li> ERROR: <%= e %> </li>
} finally {
if (rst!=null) rst.close();
if (stmt!=null) stmt.close();
if (con!=null) con.close();
if (i==0) {
<li> <i>None.</i> </li>
<p>Please enter a message:</p>
<form action="db.jsp" method="GET">
<tr><td>Name: </td><td><input type="text" name="name"></td></tr>
<tr><td>Message: </td><td><input type="text" name="message"></td></tr>
<input type="submit" value="Submit"/>
<% } /* end of database-enabled block */ %>
<p>Click <a href="index.html">here</a> to go back to the main page.</p>