Java Server Pages Part 3


The course is part of this learning path

Start course

In this course, we will learn the concepts of Java EE 7 with a focus on Java Server Pages.

Learning Objectives

  • Java Server Pages

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to improve Java 7 EE knowledge
  • Java developers


  • Have at least 2 years of Java development experience 

Hello, dear friends. In this video, we will continue examining directives. Let's begin with the tag directive. The JSP taglib directive allows you to import custom tags that are implemented in one or more Java classes in a JSP tag library. We will examine JSP tag library shortly. But for now, you use this directive to specify the descriptor file ending in .tld that describes all the available tags and the prefix that the JSP will use when it incorporates the tags. As you remember in JSF, we defined some tags and then we can use them. Like that, we will make the same operation. First, taglib uri. Now, the URI must be In JSF, we defined JSF libraries but for JSP taglib, we have to define the JSTL core. Now, the prefix will be our tag. For example, "oak". Now, we can use this "oak" to define new variables. For example, I want to hold names oak:set. Now, we will determine our tag. var="name" value. Now, we can set our name as John. Let's add scope. I also want to use the session. Now, this value will be held in our session with a name tag. Let's show it on the screen. Run. Look, John is here. Now, let's change the value.

We can change the name value with the set attribute of the session. For example, Albert. Now, show again. Run. And look, now we have two names. John comes from the first line. After the change, we can also see Albert. That's all I have for taglib right now. Let's continue with include. The JSP include directive allows you to pull in static content into your JSP page. This could be used to add a standard header or footer to your web page or to insert content at any point. The syntax is simple. The file attribute allows you to define the relative URL of the file you wish to incorporate. Include is like a face lit in JSF. First, I want to define a new JSP file. Now, in this, I will only get names. Or let's define a new tag oak:set var=sport, value will be basketball. Now, go back to the new JSP file. First, get from session. Now, set the new value as handball. Now, show again. Now, we will import this page to the other page using include. Go back to the other page. Now, we only need to include tag, include=file. Now, we have to set the new JSPs name. Done. Let's try. And look, we can see basketball and handball with the help of include. Done.

Now that we have explored the JSP syntax and some detail, it is time we turned our attention to the Java environment available to Java code embedded in a JSP. The Java environment in which JSPs can operate can be described in two different ways. The first is to describe the Java objects that the JSP implementation makes available in the form of named variables from a script plane. The second approach is to tour the JSP Java API classes. We will start with the first approach as it leads naturally to the second. Java objects in the form of named variables that are available in the Java environment for a JSP are called the JSPs implicit objects. They are considered implicit because the JSP doesn't know where they come from. Like the next meal to a fortunate child, they simply appear when needed. We already encountered an implicit object in the clock example, the Java dot io dot writer object with the name out that we used to print the formatted date in the script tag. This object is already familiar because it is based on the Java servlet API, Java dot io dot writer that you can obtain from the HTTP servlet response object passed into a Java servlet when servicing a client request.

This table shows the most frequently used implicit objects available from a JSP. We have already learned some of them like servlet context, servlet request, or servlet response. A few more implicit objects, all of which can be derived from those in the tables, are available to access cookies, headers, and request parameters. As a Java servlet developer, all of the JSP implicit objects will be very familiar. They are mostly the same objects that are available to you inside a Java servlet service method. This is no surprise given that a JSP is turned into a Java servlet service implementation at runtime. Now, let's go on with JSP Standard tags. Standard with the Java EE platform, JSP defines a collection of very useful tags that can be freely used in any JSP. They fall into a handful of categories. JSP standard tags exist for general JSP programming, some of which we are about to explore. Additionally, there are tags for accessing SQL data, XML, and text processing and formatting and a collection of functions for string manipulation.

When looking for ways to reduce the amount of embedded code in a JSP file, we frequently encounter Java control statements within the embedded code, such as the for-loop and the if-then-else statement. The JSP standard tag library contains tags that allow you to replace those control statements with tags c:forEach and c:if. c:forEach has two main variants. One for iterating through a sequence of numbers with a step value and one for iterating over an array or Java collection. This is particularly useful when iterating over the properties of a Java bean. For the first variant, the form is forEach var indexname. After that, we have to determine the start and stop index. Begin=0 and end-10, step increment, and the var value may be used anywhere within the c:forEach element using dollar sign index name, and for the second where the variable may be referenced anywhere inside the c:forEach loop using dollar sign variable name. Let's look at an example of how to use forEach. In this example, we use a Java bean called MyWallet. First, we need a class. The name will be MyWallet. Now, I will define some string arrays.

The first one is coins. Add coins. The second one will be currency. Let's add currencies. The last one is receipts, Add elements. Done. For these arrays, I will define get methods; getCoins, getCurrencies, and getReceipts. Done. Add new JSP. Now, we need taglib as we learned. The URI is the well-known URI for the standard tag library and is always the same. The prefix may be chosen according to taste but the conventional prefix for tags in the standard tag library is c. Now, let's use loops. First, we have to use the useBean tag to add MyWallet bean to our page. In class, we have to give MyWallets path along with the package and project name. First, I want to use for each with begin and end properties for odd numbers. Also, from 1 to 20, I want to set a step. For example, three. Let's add a text over the forEach. Let's try. Run server and Run page. And look our forEach is working, and we can see the numbers 1, 4, 7, etc. Let's add another for coins. forEach. I want to set bean to forEach tag MyWallet.coins. Let's try again. And look, here are our coins. Let's continue with currencies. Add the last one as receipts.

Let's look at the result. As you can see, we now have the four arrays that we were looking for. I think it's clear now how to use forEach. Notice that this JSP uses both variants of the forEach tag, iteration through a sequence of numbers and iteration through a collection or array. It's useful to know that in the former case, if the step attribute is omitted, the default value is one. In the next video, we will examine web services. See you in the next video.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics