Insert Data of multiple from in MYSQL using JSP and Servlet

How to Insert data from multiple forms in a single MYSQL table in Java using JSP, Servlet, and MySQL. Let’s assume, We have a long registration form that contains more than one form or page.

Problem Statement:

We have a registration form that contains some input from and submit button. when you click on the submit button there will be another form with some input fields and a final submit button. now, when the user fills all these input fields and clicks to submit button. complete data should be inserted into a single MySQL table.

Insert Data of multiple from in MYSQL using JSP and Servlet

  • Create an MYSQL Database with the name “mydb” and a “user_profile” table with the column name: name, last_name, email, mobile, address, zip.
  • Create a simple HTML form into “Page1.jsp” with the fields name: name, last_name
  • Create second HTML form into “Page2.jsp” with the fields name: email, mobile
  • Create third HTML form into “Page3.jsp” with the fields name: address, zip
  • Create a servlet “InserData.java” that will get data of all the forms in the form of request and send it to MYSQL and send the response back.

First HTML form into JSP

Page1.jsp contains an HTML form with two input fields Name and LastName and a submit button. when the user fills the input fields and clicks to submit button the request will be sent to “Page2.jsp” in the form of a request.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="Page2.jsp" method="post">
<pre>

Name :     <input type="text" name="name">

LastName : <input type="text" name="lastname">

<input type="submit" value="next">

</pre>
</form>

</body>
</html>

Second HTML form into JSP

Page2.jsp will get the request from Page1.jsp and create a session to temporary store the values of Page1.jsp. Page2.jsp contains an HTML form with two input fields Email and Mobile and a submit button. when the user fills the input fields and clicks to submit button the request will be sent to “Page3.jsp” in the form of a request.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String name = request.getParameter("name");
String lastname = request.getParameter("lastname");
out.println(name+ " "+lastname);
session.setAttribute("name",name);
session.setAttribute("lastname", lastname);


%>
<form action="Page3.jsp" method="post">
<pre>

Email :  <input type="text" name="email">

Mobile : <input type="text" name="mobile">

<input type="submit" value="next">

</pre>
</form>


</body>
</html>

Third HTML form into JSP

Page3.jsp will get the request from Page2.jsp and create a session to temporary store the values of Page2.jsp and session. Page3.jsp contains an HTML form with two input fields Address and Zip and a submit button. when the user fills the input fields and clicks to submit button the request will be sent to “InserData.java(Servlet)” in the form of a request.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%


String email = request.getParameter("email");
String mobile = request.getParameter("mobile");
out.println(email+ " "+mobile);
session.setAttribute("email",email);
session.setAttribute( "mobile",mobile);


%>
 
<form action="http://localhost:8080/Testing/InserData" method="post">
<pre>

Address : <input type="text" name="address">

Zip :     <input type="text" name="zip">

<input type="submit" value="Submit">

</pre>
</form>

</body>
</html>

InserData.java(Servlet)

This servlet, will create a MySQL connection and get the request of all forms and sessions and insert the data into MYSQL and send a response back to view.

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class InserData
 */
@WebServlet("/InserData")
public class InserData extends HttpServlet {
  private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public InserData() {
        super();
        // TODO Auto-generated constructor stub
    }

  /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    response.getWriter().append("Served at: ").append(request.getContextPath());
  }

  /**
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
    PrintWriter out = response.getWriter();
    HttpSession session = request.getSession();
    String name = (String)session.getAttribute("name");
    String lastname = (String)session.getAttribute("lastname");
    String email = (String)session.getAttribute("email");
    String mobile = (String)session.getAttribute("mobile");
    
    
    
    String address  = request.getParameter("address");
    String zip  = request.getParameter("zip");
    try {
      Class.forName("com.mysql.jdbc.Driver");
      Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","root");
      Statement stmt = con.createStatement();
      stmt.executeUpdate("insert into user_profile(name,lastname,email,mobile,address,zip) values('"+name+"','"+lastname+"','"+email+"','"+mobile+"','"+address+"','"+zip+"')");
      out.println("Data is inserted ");
      
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    
    
  }

}

Insert Data of multiple from in MYSQL using JSP and Servlet(Video tutorial)

Practise Task in JSP