What's wrong with my form?
Dear expert,
I'm having trouble with my enquiry form.
Hope you can tell me where my mistake lies.
Thanks.
Here's my jsp code:
<title>Enquiries</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css" media="screen,projection" />
<link rel="stylesheet" style type ="text/css" href ="style.css">
<script type="javascript">
var objForm = new Object();
var strName = new Object();
var strEmail = new Object();
var strContact = new Objec();
var strEnquiry = new Object();
objForm = document.getElementById("frmData");
objName = document.getElementById("name")
objEmail = document.getElementById("e-mail");
objContact = document.getElementById("contact");
objEnquiry = document.getElementById("enquiry");
strName = objName.value;
strEmail = objEmail.value;
strEnquiry = objEnquiry.value;
</script>
<div id="container">
<div id="content">
<form id="frmData" name="frmData" action="ProcessEnquiry" method="post" >
<h2><strong>Enquiry Form</strong></h2>
<fieldset>
<p><label for="name">Name:</label></br> <input type="text" name="name" id="name" /></p>
<p><label for="e-mail">E-mail:</label></br> <input type="text" name="e-mail" id="e-mail" /></p>
<p><label for="contact">Contact No:</label></br> <input type="text" name="contact" id="contact" /></p>
<h3>Your Enquiry:</h3>
<br />
<textarea name="enquiry" rows="8" cols="65"></textarea>
<br/>
<input type="submit" value="Submit" />
</form>
</div>
</fieldset>
</div>
And here's my servlet codes:-
public class ProcessEnquiry extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
Connection con = null;
ResultSet rs = null;
PreparedStatement ps = null;
Statement stmt = null;
String strName = "";
String strEmail = "";
String strContact = "";
String strEnquiry = "";
String strMessage = "";
String strCreateRecordSQL = "";
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/tangara_member", "tangara_karen", "123");
strCreateRecordSQL = "INSERT INTO enquiry";
strCreateRecordSQL += "(strName, strEmail, strContact, strEnquiry + VALUES (?,?,?,?)";
ps.executeUpdate();
out.println("Your mail is sent");
strMessage= "<a href=\"index.jsp\">Click here to return to Home Page";
} catch (SQLException e) {
throw new ServletException("Servlet could not display records", e);
} catch (ClassNotFoundException e) {
throw new ServletException("JDBC Driver Not Found", e);
} finally {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (con != null) {
con.close();
con = null;
}
} catch (SQLException e) {
throw new ServletException("Servlet could not display records", e);
}
}
out.close();
}
}
My database :
strName text
strEmail text
strContact text
strEnquiry BLOB
Many thanks
View Answers
July 21, 2010 at 1:29 PM
Hi Friend,
Try the following code:
1)index.jsp:
<html>
<title>Enquiries</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css" media="screen,projection" />
<link rel="stylesheet" style type ="text/css" href ="style.css">
<div style="width:680px; margin:0 auto; height:320px; padding:10px 0; border:solid; position:relative;">
<form id="frmData" name="frmData" action="../ProcessEnquiry" method="post" >
<h2><strong>Enquiry Form</strong></h2>
<table>
<tr><td>Name:</td><td><input type="text" name="name" id="name" /></td></tr>
<tr><td>E-mail:</td><td><input type="text" name="email" id="email" /></td></tr>
<tr><td>Contact No:</td><td><input type="text" name="contact" id="contact" /></td></tr>
<tr><td>Your Enquiry:</td><td><textarea name="enquiry" rows="8" cols="65"></textarea></td></tr>
<tr><td></td><td><input type="submit" value="Submit" /></td></tr>
</table>
</form>
</div>
</html>
2)ProcessEnquiry.java:
import java.io.*;
import java.sql.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ProcessEnquiry extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String strName = request.getParameter("name");
String strEmail =request.getParameter("email");
String strContact = request.getParameter("contact");
String strEnquiry = request.getParameter("enquiry");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:
mysql://localhost/mysql";, "root", "root");
PreparedStatement pst=con.prepareStatement("insert into enquiry(name,email,contact,enquiry)values(?,?,?,?)");
pst.setString(1,strName);
pst.setString(2,strEmail);
pst.setString(3,strContact);
pst.setString(4,strEnquiry);
pst.executeUpdate();
out.println("Your mail is sent");
out.println("<a href=\"index.jsp\">Click here to return to Home Page");
} catch (Exception e) {
System.out.println(e);
}
}
}
Thanks