See the Given JSP Example Code
-------------------------------
Servletform.html
<html>
<title>registration form</title>
<head>
<script language="JavaScript">
function isProper(string) {
if (!string) return false;
var iChars = "*|,\":<>[]{}`\';()@&$#%";
for (var i = 0; i < string.length; i++) {
if (iChars.indexOf(string.charAt(i)) != -1)
return false;
}
return true;
}
function validateForm(theForm){
if(theForm.firstname.value==""){
//Please enter accountholder
alert("Enter the First Name.");
theForm.firstname.focus();
return false;
}
if (isProper(theForm.firstname.value) == false) {
alert("Please enter a valid First Name.");
theForm.firstname.focus();
return false;
}
if(theForm.firstname.value.length > 20) {
alert("Maximum 20 characters allowed for 'First Name'.")
theForm.firstname.focus() ;
return false;
}
if(theForm.address.value==""){
//Please enter accountholder
alert("Enter the address.");
theForm.address.focus();
return false;
}
if(theForm.email.value==""){
//Please enter accountholder
alert("Enter the E-mail address.");
theForm.email.focus();
return false;
}
if(theForm.contect.value==""){
//Please enter accountholder
alert("Enter the contect number.");
theForm.contect.focus();
return false;
}
return true;
}
</script>
</head>
<body>
<table border="1" width="50%" cellpadding="0" cellspacing="0">
<tr>
<td width="100%">
<h2>Registration form</h2>
<form method="GET" action="DataInsertServletExam" onsubmit="return validateForm(this);">
<table border="1" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td><b>Name:</b></td>
<td><input type="text" name="firstname" size="30"></td>
</tr>
<tr><td><b>Address:</b></td>
<td><input type="text" name="address" size="50"></td>
</tr>
<tr>
<td><b>E-mail:<b></td>
<td><input type="text" name="email" size="30"></td>
</tr>
<tr><td><b>Contect No:</b></td>
<td><input type="text" name="contect" size="30"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Submit" name="submit">
<input type="reset" value="Reset" name="reset">
</td>
</tr>
</table>
</form>
<p> </p>
</tr>
</td>
</table>
</body>
</html>
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="
http://java.sun.com/xml/ns/j2ee";
xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance";
xsi:schemaLocation="
http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd";
version="2.4">
<servlet>
<servlet-name>InsertDataExample</servlet-name>
<servlet-class>DataInsertServletExam</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>InsertDataExample</servlet-name>
<url-pattern>/DataInsertServletExam</url-pattern>
</servlet-mapping>
</web-app>
DataInsertServletExam.java
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DataInsertServletExam extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)throws
ServletException, IOException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Connection con = null;
String url = "jdbc:
mysql://localhost:3306/";;
String db = "register";
String driver = "com.mysql.jdbc.Driver";
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db,"root","root");
try{
Statement st = con.createStatement();
String firstname=request.getParameter("firstname");
String address=request.getParameter("address");
String email=request.getParameter("email");
String contect=request.getParameter("contect");
int val = st.executeUpdate("insert regiform values('"+firstname+"','"+address+"','"+email+"','"+contect+"')");
con.close();
out.println("Successfully insert data in database");
}
catch (SQLException ex){
System.out.println("SQL statement is not executed!");
}
}
catch (Exception e){
e.printStackTrace();
}
}
}