Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <%@ page import="java.io.*, java.sql.*" %>
- <html>
- <head>
- <title>Post Comments: Write to me!</title>
- <link href="style.css" rel="stylesheet" type="text/css" />
- <script type="text/javascript">
- function vRequired(field) {
- if (field.value == null || field.value == "") {
- return false
- } else { return true }
- }
- function vEmail(field) {
- apos=field.value.indexOf("@")
- dotpos=field.value.lastIndexOf(".")
- if (apos < 1 || dotpos - apos < 2) {
- return false
- } else { return true }
- }
- function vForm(thisForm) {
- if (vRequired(thisForm.name)==false) {
- alert("Name is required");
- thisForm.name.focus(); return false
- } else if (vEmail(thisForm.email)==false) {
- alert("Email is incorrect");
- thisForm.email.focus(); return false
- }
- }
- </script>
- </head>
- <body>
- <% //Check if form has been submitted
- String submit = request.getParameter("submit");
- if(submit!=null){ //form submitted
- String name = request.getParameter("name");
- String email = request.getParameter("email");
- String comments = request.getParameter("comments");
- Connection conn = null;
- Statement stmt = null;
- try {
- // 1. Load the JDBC driver
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- // 2. Define the connection URL
- //String dbUrl = "jdbc:mysql://10.0.2.60/test?user=test&password=password";
- String dbUrl = "jdbc:mysql://localhost/guestbook?user=test2&password=test";
- // 3. Establish the connection
- conn = DriverManager.getConnection(dbUrl);
- // 4. Create a Statement object
- stmt = conn.createStatement();
- // 5. Execute the insert
- String statement = "INSERT INTO comments (name, email, comments) VALUES ('" + name + "', '" + email + "', '" + comments + "')";
- boolean res = stmt.execute(statement);
- // 7. Close connection
- if (stmt != null) {
- stmt.close(); }
- if (conn != null) {
- conn.close(); }
- } catch (SQLException ex) {
- %>
- <%=ex.getMessage()%> <br>
- <%
- }
- %>
- <div class="Announcement">Form Submited!</div>
- <%
- }
- %>
- <br><br><br>
- <div id=content>
- <jsp:include page="header.txt" />
- <h2>Post your comments</h2>
- <small>Your comments are well appreciated.</small>
- <br><br>
- <form action="postcomment.jsp" onsubmit="return vForm(this)">
- <table align=center>
- <tr>
- <td class="alt"><strong>Name:</strong></td>
- <td> <input type="text" id="name" name="name" size="30" maxlength="30" class="textfield" value="" /></td>
- </tr>
- <tr>
- <td class="alt"><strong>Email: </strong></td>
- <td> <input type="text" id="email" name="email" size="50" maxlength="30" class="textfield" value="" /> </td>
- </tr>
- <tr>
- <td class="alt"><strong>Comments: </strong></td>
- <td>
- <textarea id=comments name=comments rows=10 cols=50></textarea>
- </td>
- </tr>
- <tr colspan=2>
- <td align=center colspan=2><input type="submit" value="submit" name="submit"/></td>
- </tr>
- </table>
- </form>
- </div>
- </body>
- </html>
Add Comment
Please, Sign In to add comment