JSP Tutorial - JSP Introduction: Setting Up Java Development Kit
JSP Tutorial - JSP Introduction: Setting Up Java Development Kit
When using JSP we can access the entire Java APIs, including the JDBC API to
access databases.
JavaServer Pages can render web pages with dynamic content. We can use Java code
in HTML pages by making use of special JSP tags, <% %>.
A JavaServer Pages are a type of Java servlet that is designed to create user interface
for a Java web application.
We create JSP pages in text files that combine HTML or XHTML code, XML
elements, and embedded JSP actions and commands.
In JavaServer Pages, we can get user input through web forms(<form> tag), present
records from a database, and create web pages dynamically.
When using JSP we create our own custom tags or use the existing tag library. A JSP
tag looks like a normal HTML tag.
JSP HelloWorld
Setting up Java Development Kit
First, download Java Software Development Kit (JDK) from Oracle's Java site and
setting up PATH environment variable appropriately.
Install and configure the JDK and set PATH and JAVA_HOME environment
variables to refer to the directory that contains java and javac, typically
java_install_dir/bin and java_install_dir respectively.
For example,
set PATH=C:\jdk1.8\bin;%PATH%
set JAVA_HOME=C:\jdk1.8
C:\apache-tomcat-8\bin\startup.bat
After a successful startup, the default web applications will be available by visiting
http://localhost:8080/.
C:\apache-tomcat-8\bin\shutdown
Compilation
Initialization
Execution
Cleanup
When a browser requests a JSP from the JSP engine, the JSP engine first checks to
see whether it needs to compile the page.
If the page has never been compiled, or if the JSP has been modified since it was last
compiled, the JSP engine compiles the page.
Example
Save the code below in JSP file hello.jsp and put this file in C:\apache-
tomcat\webapps\ROOT directory and try to browse it by giving URL
http://localhost:8080/hello.jsp in your browser address bar.
<html>
<head><title>Hello World</title></head>
<body>
Hello World!<br/>
<%
out.println("Your IP address is " + request.getRemoteAddr());
%>
</body>
</html>
Date Formatting using SimpleDateFormat
JSP Syntax
Scriptlet
For example,
<html>
<head><title>Hello World</title></head>
<body>
Hello World!<br/>
<%
out.println("Your IP address is " + request.getRemoteAddr());
%>
</body>
</html>
<jsp:scriptlet>
code fragment
</jsp:scriptlet>
Scriptlet is like a Java code island in the JavaServer Pages. Inside the island we can
write Java code.
JSP Declarations
A declaration in JSP declares one or more variables or methods that we can use in
Java code later in the JSP file.
We must declare the variable or method before using it in the JSP file, like in Java
code.
<jsp:declaration>
code fragment
</jsp:declaration>
In the following example of JSP declaration tag, we are declaring the field and
printing the value of the declared field using the jsp expression tag.
<html>
<body>
</body>
</html>
The following example of JSP declaration tag defines the method which returns the
cube of given number and calls this method from the jsp expression tag.
<html>
<body>
<%!
int cube(int n){
return n*n*n*;
}
%>
</body>
</html>
JSP Expression
The expression element can have any valid Java expression without a semicolon at the
end.
<html>
<body>
<%= "welcome to jsp" %>
</body>
</html>
<jsp:expression>
expression
</jsp:expression>
<html>
<body>
<p>
Today's date: <%= (new java.util.Date()).toLocaleString()%>
</p>
</body>
</html>
JSP Comments
<html>
<body>
<%-- This is a comment in the page.--%>
</body>
</html>
JSP Directives
A JSP directive sets the overall structure of the servlet class. It usually has the
following form:
JSP Actions
JSP actions use commands in XML syntax to control the behavior of the servlet
engine which controls how to generate the JavaServer Pages.
We can insert a file, use JavaBeans code, forward the user to another page, or
generate HTML for the Java plugin by using the JSP Actions.
JSP supports nine implicit objects. They are listed in the following table.
JSP Implicit Objects
JSP supports nine implicit objects. They are listed in the following table.
JSP Flow Control Statement
JSP if...else
We need to use Scriptlet at each line with HTML text included between Scriptlet tags.
The following code shows how to use the if...else statement with Scriptlet tags.
JSP Switch...case
JSP Directives
JSP directives provide instructions to the JSP engine on how to handle JSP.
A JSP directive affects the structure of the servlet class. It usually has the following
form:
<%@ directive attribute="value" %>
Directives can have a list of attributes defined in key-value pairs and separated by
commas.
The page directive instructs JSP engine on how to process the current JSP page.
By convention, page directives are listed at the top of the JSP page.