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!
index.jsp:
This is the code of index page:
<%@ 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>my jsp page</title>
</head>
<body>
//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">
</form>
</body>
</html>
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:
new.jsp
//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"%>
<html>
<head>
<title>inserting record in jsp</title>
</head>
<body>
//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');
</sql:update>
//select command of SQL in jstl tag to fetch record from table employee
<sql:query dataSource="${snapshot}"
var="result">
SELECT * from Employees;
</sql:query>
//table tag used for tabular data-representation
<table border="1"
>
<tr>
<th>Emp ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
</tr>
//foreach loop for data-retrival in repeatation form:
<c:forEach var="row"
items="${result.rows}">
<tr>
<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>
</tr>
</c:forEach>
</table>
</body>
</html>
The output, after the page new.jsp reload and it will fetch data from database table is as follow: