A Developer Gateway To IT World...

Techie Uncle Software Testing Core Java Java Spring C Programming Operating System HTML 5 Java 8 ES6 Project

Send name in jsp field element and submit via send button and fetch data from database table in jsp

The JSP project based on complete JSP page Concept:
If you want to be good in jsp then you should understand this project. How does JSP page use in development.

Let's consider the following java2ee Project:
In this following project, MySQL is Back-end used as database storage and JSP pages are used as Front-end.

Consider the following Database and tables, which is used in the project:

Lets start coding Guys!

This is the code of index page:

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

//here we are going to add header tag which is just a bootstrap page for header panel 
<%@include file="header.jsp"%>

<h1>Menu 1:go to new.jsp page for retrieve data from database</h1>

<form method="post" action="new.jsp">
 Enter name:<input type="text" name="user">
<input type="submit" value="send">


When, we run this project, this index.jsp page is looking as front-end page as follow:

when we take any name and click on send button, it will fetch data from database table:


//this is jstl tag
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<title>inserting record in jsp</title>
//adding headers
<%@include file="header.jsp"%>

//code of jdbc in jsp

<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
     url="jdbc:mysql://localhost/mydatabase"  user="root"  password="root"/>

//sql insert commands, when user send any name and use send button, then this record will //executed automatically

<sql:update dataSource="${snapshot}" var="result">
INSERT INTO employees VALUES (109, 12, 'Heera', 'Babu');

//select command of SQL in jstl tag to fetch record from table employee

<sql:query dataSource="${snapshot}" var="result">
SELECT * from Employees;

//table tag used for tabular data-representation 
<table border="1" >
   <th>Emp ID</th>
   <th>First Name</th>
   <th>Last Name</th>

//foreach loop for data-retrival in repeatation form:

<c:forEach var="row" items="${result.rows}">
   <td><c:out value="${row.eid}"/></td>
   <td><c:out value="${row.serial}"/></td>
   <td><c:out value="${row.fname}"/></td>
   <td><c:out value="${row.lname}"/></td>


The output, after the page new.jsp reload and it will fetch data from database table is as follow: