2. Introduction to JSP
It stands for Java Server Pages.
It is a server side technology.
It is used for creating web application.
It is used to create dynamic web content.
In this JSP tags are used to insert JAVA code into
HTML pages.
It is an advanced version of Servlet Technology.
It is a Web based technology helps us to create
dynamic and platform independent web pages.
In this, Java code can be inserted in HTML/ XML
pages or both.
JSP is first converted into servlet by JSP container
before processing the client’s request.
3. JSP pages are more advantageous
than Servlet:
They are easy to maintain.
No recompilation or redeployment is required.
JSP has access to entire API of JAVA .
JSP are extended version of Servlet
4. Features of JSP
Coding in JSP is easy :- As it is just adding JAVA code to
HTML/XML.
Reduction in the length of Code :- In JSP we use action
tags, custom tags etc.
Connection to Database is easier :-It is easier to connect
website to database and allows to read or write data easily
to the database.
Make Interactive websites :- In this we can create
dynamic web pages which helps user to interact in real
time environment.
Portable, Powerful, flexible and easy to maintain :- as
these are browser and server independent.
No Redeployment and No Re-Compilation :- It is
dynamic, secure and platform independent so no need to
re-compilation.
Extension to Servlet :- as it has all features of servlets,
implicit objects and custom tags
5. syntax
Declaration Tag :-It is used to declare variables.
Syntax:- <%! Dec var %>
Example:- <%! int var=10; %>
Syntax:- <%= expression %>
Example:- <% num1 = num1+num2 %>
Syntax:- <% -- JSP Comments %>
6. How to run JSP in Eclipse IDE
using Apache Tomcat Server
Step 1: In order to run JSP in Eclipse IDE, you need to
have Apache tomcat Server configured in Eclipse
IDE.
15. JSP - Standard Tag Library
The JavaServer Pages Standard Tag Library (JSTL) is a
collection of useful JSP tags which encapsulates the core
functionality common to many JSP applications.
JSTL has support for common, structural tasks such as
iteration and conditionals, tags for manipulating XML
documents, internationalization tags, and SQL tags. It
also provides a framework for integrating the existing
custom tags with the JSTL tags.
16. Advantage of JSTL
Fast Development JSTL provides many tags that
simplify the JSP.
Code Reusability We can use the JSTL tags on
various pages.
No need to use scriptlet tag It avoids the use of
scriptlet tag.
17. Install JSTL Library
To begin working with JSP tages you need to first install the JSTL
library. If you are using the Apache Tomcat container, then
follow these two steps −
Step 1 − Download the binary distribution from Apache Standard
Taglib and unpack the compressed file.
Step 2 − To use the Standard Taglib from its Jakarta Taglibs
distribution, simply copy the JAR files in the distribution's 'lib'
directory to your application's webappsROOTWEB-
INFlib directory.
To use any of the libraries, you must include a <taglib> directive
at the top of each JSP that uses the library.
18.
19. Classification of The JSTL
Tags
The JSTL tags can be classified, according to their
functions, into the following JSTL tag library groups that
can be used when creating a JSP page −
Core Tags
Formatting tags
SQL tags
XML tags
JSTL Functions
20. Core Tags
S.No. Tag & Description
1 <c:out>Like <%= ... >, but for expressions.
2 <c:set >Sets the result of an expression evaluation in a 'scope'
3
<c:remove >Removes a scoped variable (from a particular scope, if specified).
4
<c:catch>Catches any Throwable that occurs in its body and optionally exposes it.
5
<c:if>Simple conditional tag which evalutes its body if the supplied condition is true.
6
<c:choose>Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked
by <when> and <otherwise>.
7
<c:when>Subtag of <choose> that includes its body if its condition evalutes to 'true'.
8
<c:otherwise >Subtag of <choose> that follows the <when> tags and runs only if all of the prior conditions evaluated
to 'false'.
9
<c:import>Retrieves an absolute or relative URL and exposes its contents to either the page, a String in 'var', or a
Reader in 'varReader'.
10
<c:forEach >The basic iteration tag, accepting many different collection types and supporting subsetting and other
functionality .
11
<c:forTokens>Iterates over tokens, separated by the supplied delimeters.
12 <c:param>Adds a parameter to a containing 'import' tag's URL.
13 <c:redirect >Redirects to a new URL.
14 <c:url>Creates a URL with optional query parameters
22. Java Servlet and JDBC Example | Insert data in
MySQL
to start with interfacing Java Servlet Program with
JDBC Connection:
Proper JDBC Environment should set-up along with
database creation.
To do so, download the mysql-connector.jar file from
the internet,
As it is downloaded, move the jar file to the apache-
tomcat server folder,
Place the file in lib folder present in the apache-
tomcat directory.
To start with the basic concept of interfacing:
Step 1: Creation of Database and Table in MySQL
24. Step 3: Creation of Java Servlet program with JDBC
Connection
to create a JDBC Connection steps are
1. Import all the packages
2. Register the JDBC Driver
3. Open a connection
4. Execute the query, and retrieve the result
5. Clean up the JDBC Environment
6. Create a separate class to create a connection of
database, as it is a lame process to writing the
same code snippet in all the program. Create a
.java file which returns a Connection object.
25. import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
// This class can be used to initialize the database connection
public class DatabaseConnection {
protected static Connection initializeDatabase()
throws SQLException, ClassNotFoundException
{
// Initialize all the information regarding
// Database Connection
String dbDriver = "com.mysql.jdbc.Driver";
String dbURL = "jdbc:mysql:// localhost:3306/";
// Database name to access
String dbName = "demoprj";
String dbUsername = "root";
String dbPassword = "root";
Class.forName(dbDriver);
Connection con = DriverManager.getConnection(dbURL + dbName,
dbUsername,
dbPassword);
return con;
}
}
26. To use this class method, create an object in Java Servlet programBelow
program shows Servlet Class which create a connection and insert the data in
the demo table,
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
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 Database Connection Class
file
import code.DatabaseConnection;
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
try {
// Initialize the database
Connection con = DatabaseConnection.initializeDatabase();
// Create a SQL query to insert data into demo table
// demo table consists of two columns, so two '?' is used
PreparedStatement st = con
.prepareStatement("insert into demo values(?, ?)");
// For the first parameter,
// get the data using request object
// sets the data to st pointer
st.setInt(1, Integer.valueOf(request.getParameter("id")));
// Same for second parameter
st.setString(2, request.getParameter("string"));
// Execute the insert command using executeUpdate()
// to make changes in database
st.executeUpdate();
// Close all the connections
st.close();
con.close();
// Get a writer pointer
// to display the successful result
PrintWriter out = response.getWriter();
out.println("<html><body><b>Successfully Inserted"
+ "</b></body></html>");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
27. step 5: Get the data from the HTML fileTo get the
data from the HTML file, the request object is used
which calls getParameter() Method to fetch the data
from the channel. After successful insertion, the
writer object is created to display a success
message.
After insertion operation from Servlet, data will be
reflected in MySQL Database