Ex No: 14 UPLOAD DATA FROM CLIENT USING JSP
Aim: To create a JSP page to upload data from client.
Algorithm
Step1: Create a folder “Ex14” to place the HTML and JSP files.
Step2: Create a database “test” in MySQL DBMS.
Step3: Create a table “stud_tbl” with 4 columns (studname,regno,dept,yr).
Step5: Create files “index.html” and “ex13.jsp”.
Source Code

index.html


<!DOCTYPE html>
<html>
 <head>
  <title>JSP Data Upload</title> 
   <meta charset="UTF-8"> 
 </head>
 <style>
  header {
   text-align:center;
   height: 100px;
   background-color: Teal;
   padding: 10px; 
   border-radius: 10px 10px 0px 0px;
  }
  footer {
   text-align:center;
   font-size: 12px;
   height: 50px;
   line-height: 50px;
   background-color: Teal;
   padding: 10px; 
   border-radius: 10px;  
  }
  section{
   width: auto; 
   border: 2px solid gray;
   padding: 10px; 
   border-radius: 10px;
   margin:5px;
  }
  input{
   border-radius: 5px;
   margin: 5px;
  }
 </style>
 <body>  
  <header>
   <h1>JSP Data Upload</h1>  
  </header>  
  <article>
    <section>  
     <form method="POST" action="ex14.jsp">
       <label>Enter Reg No:</label>
       <input type="text" name="txtreg" /><br>
       <label>Enter Name:</label>
       <input type="text" name="txtname" /><br>
       <label>Enter Dept:</label>
       <input type="text" name="txtdept" /><br>
       <label>Enter Year:</label>
       <input type="text" name="txtyr" /><br>
       <input type="submit" value="Submit"/>
     </form> 
    </section>
  </article>
  <footer>
   Developed by K. Anbarasan
  </footer>
 </body>
</html>

ex14.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" %>
<%@ page import="java.lang.String" %>
<%@ page import="java.lang.*" %>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
 <head>
  <title>JSP Data Upload</title> 
   <meta charset="UTF-8"> 
 </head>
 <style>
  header {
   text-align:center;
   height: 100px;
   background-color: DarkCyan;
   padding: 10px; 
   border-radius: 10px 10px 0px 0px;
  }
  footer {
   text-align:center;
   font-size: 12px;
   height: 50px;
   line-height: 50px;
   background-color: DarkCyan;
   padding: 10px; 
   border-radius: 10px;  
  }
  section{
   width: auto; 
   border: 2px solid gray;
   padding: 10px; 
   border-radius: 10px;
   margin:5px;
  }
  input{
   border-radius: 5px;
   margin: 5px;
  }
 </style>
 <body>  
  <header>
   <h1>JSP Data Upload</h1>  
  </header>  
  <article>
    <section>    
    <%
      String driverName = "com.mysql.jdbc.Driver";
      String connectionUrl = "jdbc:mysql://localhost:3306/";
      String dbName = "test";
      String userId = "root";
      String password = "";
      String regno, name, dept, yr;
  
      regno = request.getParameter("txtreg");
      name = request.getParameter("txtname");
      dept = request.getParameter("txtdept");
      yr = request.getParameter("txtyr");

      Connection con = null;
      Statement stmt = null;
      int numrows;

      Class.forName(driverName);
      try 
      {
        con = DriverManager.getConnection(connectionUrl + dbName, userId, password);
        stmt = con.createStatement();
        String sql = "insert into stud_tbl values('"+name+"',"+regno+",'"+dept+"','"+yr+"')";

        numrows = stmt.executeUpdate(sql);
   
    %>
    <h4>Data Uploaded</h4>
    <%
        stmt.close();
        con.close();
      } 
      catch (Exception e) 
      { 
        e.printStackTrace();
      }
    %>
    </section>
  </article>
  <footer>
   Developed by K. Anbarasan
  </footer>
 </body>
</html>
Result: Thus JSP page is written upload data from client.