0% found this document useful (0 votes)
185 views

JSP Tutorial - JSP Introduction: Setting Up Java Development Kit

The document provides an overview of JavaServer Pages (JSP) including: - JSP allows creating dynamic web pages using Java code embedded in HTML/XML pages. - JSP pages combine standard HTML/XML markup, scripting elements written in Java, and special JSP tags for scripting logic and output. - Key elements of JSP include scriptlets for Java code, expressions for output, comments, directives to control page behavior, and implicit objects for accessing page information and requests.

Uploaded by

A'del Jumma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
185 views

JSP Tutorial - JSP Introduction: Setting Up Java Development Kit

The document provides an overview of JavaServer Pages (JSP) including: - JSP allows creating dynamic web pages using Java code embedded in HTML/XML pages. - JSP pages combine standard HTML/XML markup, scripting elements written in Java, and special JSP tags for scripting logic and output. - Key elements of JSP include scriptlets for Java code, expressions for output, comments, directives to control page behavior, and implicit objects for accessing page information and requests.

Uploaded by

A'del Jumma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

JSP Tutorial - JSP Introduction

JavaServer Pages, or JSP, is a server-side programming technology that we can use to


create of dynamic web pages.

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

Setting up Web Server: Tomcat

Apache Tomcat is an open source software implementation of the JavaServer Pages


and Servlet technologies.
Download latest version of Tomcat from http://tomcat.apache.org/

Unpack the binary distribution.

Tomcat can be started by executing the following commands on windows machine:

C:\apache-tomcat-8\bin\startup.bat

After a successful startup, the default web applications will be available by visiting
http://localhost:8080/.

Tomcat can be stopped by executing the following commands on windows machine:

C:\apache-tomcat-8\bin\shutdown

JSP Life Cycle

A JSP life cycle is listed as follows.

 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

Suppose your Apache Tomcat is installed in C:\apache-tomcat.

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

SimpleDateFormat can format and parse dates in a locale-sensitive manner.

The following code shows how to output current time.

<%@ page import="java.io.*,java.util.*" %>


<%@ page import="javax.servlet.*,java.text.*" %>
<html>
<body>
<%
Date dNow = new Date();
SimpleDateFormat ft = new SimpleDateFormat ("E yyyy.MM.dd 'at'
hh:mm:ss a zzz");
out.print( "<h2 align=\"center\">" + ft.format(dNow) + "</h2>");
%>
</body>
</html>

JSP Syntax
Scriptlet

A JSP Scriptlet can contain Java language statements, variable or method


declarations, or expressions that are valid in the page scripting language.

We can use the following syntax to include Scriptlet in JSP.

<% code fragment %>

For example,

<html>
<head><title>Hello World</title></head>
<body>
Hello World!<br/>
<%
out.println("Your IP address is " + request.getRemoteAddr());
%>
</body>
</html>

You can write XML equivalent of the above syntax as follows:

<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.

Following is the syntax of JSP Declarations:

<%! declaration; ... %>

You can write XML equivalent of the above syntax as follows:

<jsp:declaration>
code fragment
</jsp:declaration>

Following is the simple example for JSP Declarations:


<%! int i = 0; %>
<%! int a, b, c; %>
<%! double money; %>
<%! YourClass a = new YourClass(); %>

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>

<%! int data=50; %>


<%= "Value of the variable is:"+data %>

</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*;
}
%>

<%= "Cube of 3 is:"+cube(3) %>

</body>
</html>
JSP Expression

A JSP expression element contains a scripting language expression that is evaluated,


converted to a String.

The expression element can have any valid Java expression without a semicolon at the
end.

The following code shows the syntax of JSP Expression:

<%= expression %>


In the following example of jsp expression tag, we display a welcome message.

<html>
<body>
<%= "welcome to jsp" %>
</body>
</html>

The XML equivalent of the above syntax is as follows:

<jsp:expression>
expression
</jsp:expression>

The following code shows how to use JSP Expression.

<html>
<body>
<p>
Today's date: <%= (new java.util.Date()).toLocaleString()%>
</p>
</body>
</html>

This would generate following result:


<p> Today's date: 11-Jan-2015 17:02:25 </p>

JSP Comments

The following code shows the syntax of JSP comments:

<%-- This is JSP comment --%>


The following JSP has a JSP Comment.

<html>
<body>
<%-- This is a comment in the page.--%>
</body>
</html>

<!-- comment --> is an HTML comment. Ignored by the browser.

JSP Directives

A JSP directive sets the overall structure of the servlet class. It usually has the
following form:

<%@ directive attribute="value" %>

There are three types of directive tag:

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 Actions use the following syntax:

<jsp:action_name attribute="value" />

JSP Implicit Objects

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 can use if statement in JavaServer Pages.

The if...else block works as normal if...else statement.

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.

<%! int day = 7; %>


<html>
<body>
<% if (day == 1) { %>
<p> is one </p>
<% } else { %>
<p> not one</p>
<% } %>
</body>
</html>

JSP Switch...case

The following code shows how to use switch...case block

<%! int day = 3; %>


<html>
<body>
<%
switch(day) {
case 0:
out.println("It\'s Sunday.");
break;
case 1:
out.println("It\'s Monday.");
break;
case 2:
out.println("It\'s Tuesday.");
break;
case 3:
out.println("It\'s Wednesday.");
break;
case 4:
out.println("It\'s Thursday.");
break;
case 5:
out.println("It\'s Friday.");
break;
default:
out.println("It's Saturday.");
}
%>
</body>
</html>

JSP for loop

The following code shows how to use for loop.

<%! int fontSize; %>


<html>
<body>
<%for ( fontSize = 1; fontSize <= 3; fontSize++){ %>
<font color="red" size="<%= fontSize %>">
JSP Tutorial
</font><br />
<%}%>
</body>
</html>

JSP while loop

The following code shows how to use while loop.

<%! int fontSize; %>


<html>
<head><title>WHILE LOOP Example</title></head>
<body>
<%while ( fontSize <= 3){ %>
<font color="green" size="<%= fontSize %>">
JSP Tutorial
</font><br />
<%fontSize++;%>
<%}%>
</body>
</html>

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

The page directive instructs JSP engine on how to process the current JSP page.

We can add page directives anywhere in the JSP page.

By convention, page directives are listed at the top of the JSP page.

The following example shows the basic syntax of page directive:

<%@ page attribute="value" %>

You can write XML equivalent of the above syntax as follows:

<jsp:directive.page attribute="value" />

You might also like