JSP CALLING SERVLET
Form.jsp
<html>
<form action="InsertServlet" method="post">
<table align=center border=0 cellspacing=0 cellpadding=10 width=100 height=0
bgcolor=#faebd7>
<tr>
<td>Name</td>
<td><input type=text name="name" size=20></td>
</tr>
<tr>
<td>Address</td>
<td><input type=text name="address" size=30></td>
</tr>
<tr>
<td>Tel no</td>
<td><input type=text name="telno" size=20></td>
</tr>
</table>
<br>
<br>
<center>
<input type=submit name=submit value=submit>
</form></html>
InsertServlet.java
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class InsertServlet extends HttpServlet {
public void doPost(HttpServletRequest req,HttpServletResponse res) throws
ServletException,IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String name=req.getParameter("name");
String address=req.getParameter("address");
String telno=req.getParameter("telno");
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test",
"root", "root");
Statement st=con.createStatement();
int i=st.executeUpdate("insert into data1(name,address,telno)
values('"+name+"','"+address+"','"+telno+"')");
out.println("Data is inserted successfully");
con.close();
}
catch(Exception e){
System.out.println(e); } } }
Web.xml
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<!-- General description of your web application -->
<display-name>InsertServlet</display-name>
<description>
JSP CALLING SERVLET
</description>
<!-- define servlets and mapping -->
<servlet>
<servlet-name>InsertServlet</servlet-name>
<servlet-class>InsertServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>InsertServlet</servlet-name>
<url-pattern>/InsertServlet</url-pattern>
</servlet-mapping>
</web-app>