Guest User

Untitled

a guest
Mar 9th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. <%@ page import="java.io.*, java.sql.*" %>
  2. <html>
  3. <head>
  4.  
  5. <title>Post Comments: Write to me!</title>
  6. <link href="style.css" rel="stylesheet" type="text/css" />
  7.  
  8.  
  9.  
  10.  
  11. <script type="text/javascript">
  12. function vRequired(field) {
  13. if (field.value == null || field.value == "") {
  14. return false
  15. } else { return true }
  16. }
  17. function vEmail(field) {
  18. apos=field.value.indexOf("@")
  19. dotpos=field.value.lastIndexOf(".")
  20. if (apos < 1 || dotpos - apos < 2) {
  21. return false
  22. } else { return true }
  23. }
  24. function vForm(thisForm) {
  25. if (vRequired(thisForm.name)==false) {
  26. alert("Name is required");
  27. thisForm.name.focus(); return false
  28. } else if (vEmail(thisForm.email)==false) {
  29. alert("Email is incorrect");
  30. thisForm.email.focus(); return false
  31. }
  32. }
  33. </script>
  34. </head>
  35.  
  36.  
  37. <body>
  38. <% //Check if form has been submitted
  39. String submit = request.getParameter("submit");
  40. if(submit!=null){ //form submitted
  41.  
  42. String name = request.getParameter("name");
  43. String email = request.getParameter("email");
  44. String comments = request.getParameter("comments");
  45.  
  46. Connection conn = null;
  47. Statement stmt = null;
  48. try {
  49. // 1. Load the JDBC driver
  50. Class.forName("com.mysql.jdbc.Driver").newInstance();
  51.  
  52. // 2. Define the connection URL
  53. //String dbUrl = "jdbc:mysql://10.0.2.60/test?user=test&password=password";
  54. String dbUrl = "jdbc:mysql://localhost/guestbook?user=test2&password=test";
  55.  
  56. // 3. Establish the connection
  57. conn = DriverManager.getConnection(dbUrl);
  58.  
  59. // 4. Create a Statement object
  60. stmt = conn.createStatement();
  61.  
  62.  
  63.  
  64. // 5. Execute the insert
  65. String statement = "INSERT INTO comments (name, email, comments) VALUES ('" + name + "', '" + email + "', '" + comments + "')";
  66. boolean res = stmt.execute(statement);
  67.  
  68. // 7. Close connection
  69. if (stmt != null) {
  70. stmt.close(); }
  71. if (conn != null) {
  72. conn.close(); }
  73.  
  74. } catch (SQLException ex) {
  75.  
  76. %>
  77. <%=ex.getMessage()%> <br>
  78. <%
  79. }
  80.  
  81. %>
  82. <div class="Announcement">Form Submited!</div>
  83. <%
  84. }
  85. %>
  86. <br><br><br>
  87.  
  88. <div id=content>
  89.  
  90. <jsp:include page="header.txt" />
  91.  
  92.  
  93. <h2>Post your comments</h2>
  94. <small>Your comments are well appreciated.</small>
  95. <br><br>
  96.  
  97. <form action="postcomment.jsp" onsubmit="return vForm(this)">
  98.  
  99. <table align=center>
  100.  
  101. <tr>
  102. <td class="alt"><strong>Name:</strong></td>
  103. <td> <input type="text" id="name" name="name" size="30" maxlength="30" class="textfield" value="" /></td>
  104.  
  105. </tr>
  106.  
  107.  
  108. <tr>
  109. <td class="alt"><strong>Email:&nbsp;&nbsp;</strong></td>
  110. <td> <input type="text" id="email" name="email" size="50" maxlength="30" class="textfield" value="" /> </td>
  111.  
  112. </tr>
  113.  
  114. <tr>
  115. <td class="alt"><strong>Comments:&nbsp;&nbsp;</strong></td>
  116. <td>
  117. <textarea id=comments name=comments rows=10 cols=50></textarea>
  118. </td>
  119.  
  120. </tr>
  121.  
  122.  
  123. <tr colspan=2>
  124. <td align=center colspan=2><input type="submit" value="submit" name="submit"/></td>
  125. </tr>
  126.  
  127. </table>
  128. </form>
  129.  
  130. </div>
  131.  
  132. </body>
  133.  
  134.  
  135.  
  136.  
  137.  
  138. </html>
Add Comment
Please, Sign In to add comment