Java Server Pages in Java EE 7

Contents

keyboard_tab

The course is part of this learning path

Java Server Pages (JSP) Part 1
Difficulty
Intermediate
Duration
50m
Students
3
Description

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

Prerequisites

  • Have at least 2 years of Java development experience 
Transcript

Hello there. In this video, we will start to examine JSP. So, let's start. A Java Server Page, JSP, is a Java servlet turned inside out. In other words, a JSP is equivalent to a servlet in the same sense that almost everything you can do in a Java servlet, you can also do in a JSP. It is how you do it that is different. In the last lesson on the Java servlet API, you learned that a Java servlet is a Java component that analyzes an HTTP request and writes out response content either to an output stream or to a writer. As you write more Java servlet, you'll observe that much of the content that you write to the response object is static and only a relatively small amount of the code is dynamically generating the data to write to the output. This is particularly true for Java servlets that write HTML content. Much of the Java servlet code is of the form, out.println(), and in brackets, you will write your HTML tags. The inspiration for JSPs came from the observation that  embedding HTML code inside out.println() statements not only make the HTML code difficult to maintain, it is counterintuitive to web developers who are used to editing HTML code directly in an HTML friendly editor. Thus, in JSP programming, instead of the static content of the web component being embedded inside Java code, the dynamic fragments of the JSP are embedded as Java code within the static content, as shown in this figure. This feature makes JSPs especially well suited to the creation of web components that contain a great deal of static content that is frequently tweaked and adjusted in a publishing tool together with small amounts of embedded dynamic content. In other words, JSPs are well suited to creating content for a typical dynamic HTML website. The key to understanding JSP technology is understanding what Java environment you have available when you use Java code to embed dynamic content into a JSP and the relationship between the content language you are using and the dynamic data you wish to present. Much becomes clear about the Java environment available in a JSP when you understand what happens to a Java Server Page when you deploy it to the Java EE web container. After the JSP has been deployed as part of a WAR file to the Java EE web container and sometime before the web container attempts to process the first request to that JSP, the web container reads the JSP you deployed. From that information, it generates the Java code for a Java servlet that fulfills the objectives of the JSP. It compiles the generated Java servlet and invokes it with the request that the client intended for the JSP. In this way, the JSP never really deals with the HTTP request or the response. The HTTP request intended for the JSP is, in fact, handled by the generated Java servlet that the Java EE web container created for it. This process is shown in this figure. This provides a large clue as to which parts of the Java EE web container environment are available to the JSP. It is largely the same environment we have already seen in the Java servlet API. The JSP technology at its essence uses syntax to allow Java code to be embedded with static content. The first and most basic form of this syntax is <% %>. You can write regular Java code inside the <% %> braces and have access to the Java servlet and JSP APIs in such a context. When the Java EE web container comes to create the generated Java servlet that will implement the JSP, it takes any static content lines in the JSP and wraps them in an out.println() statement. The web container also takes any Java code in the JSP embedded within the %% braces and writes it directly in sequence in the generated Java servlet, as shown in this figure. In this view of a JSP, the basic syntax for embedded code fragments and their equivalent generated form is only the beginning of the story. JSP provides a rich environment instead of mechanisms for streamlining the boundaries between static and dynamic content built on top of the Java servlet model. Nevertheless, the notion of JSP as being fundamentally the same as a Java servlet but with a different programming model consisting of a sea of static content containing islands of Java code in various syntactical disguises is the key to understanding the many aspects of JSP technology. Here's a code snippet. Let's examine it. First, we start by noticing the %@page, % lines at the top of the JSP page. These statements are called JSP directives. The JSP @page directive is used to set up properties that are used by the whole JSP page. In this example, we have three @page directives. The first defines the mime type and encoding for this HTML page and the second and third define that the JSP page is going to use two Java classes: java.util.Date and java.text.SimpleDateFormat. Reading beyond the page directives, we encounter the HTML code that we would expect for this page until we get to the line, "It's been milliseconds". The text, system.currentTimeMillis() is called a JSP scriptlet. When we enter it, we leave the world of static HTML code and rejoin the world of dynamic Java code. There are two kinds of JSP scriptlets. equal Java expression, where Java expression evaluates to a value. This value is then used when the page is called and the value becomes part of the page output and Java expression where Java expression is a collection of Java statements that perform some calculations and take some action. We see the second form in our example page when we come to calculate and display the current day of the week. Notice that in this Java statement block, the JSP page is relying on the @page imports it declared at the top in order for the Java classes, Date and SimpleDateFormat to be resolved. Second, look at the variable, out.  What is it?

In this line of code, out refers to the output of the underlying HTTP response that carries the content back to the calling client and is a type of java.io.Writer from the JSP API, specifically javax.servlet.jsp.JspWriter. Notice that this variable has not been declared anywhere else in the JSP. That is because the Java environment within JSP expressions comes preconfigured with a number of objects called implicit objects. Other examples include the session variable, the current HTTP session, and the response variable, the current HTTP servlet response. So, the line out.println(today.trim()); writes the day string to the response writer and that is how it appears on the page when loaded in the browser. More complicated JSPs that make further use of embedded Java code, i.e., in the form of a scriptlet, as we see in this simple example, tend to suffer some of the same maintainability issues that Java servlet can, except, in reverse. Ideally, static markup content and dynamic Java content are separated into different files. The JSP techniques and technology for doing this will lead us to the topics of JSP beans, tag libraries, and expression language. But first, we need to more fully understand the core JSP syntax that this simple example has introduced. By convention, JSP file should have the file ending .jsp. This indicates to the Java EE web container that it should attempt to interpret the file as a JSP. The URL for JSP is just like the URL for a regular static content file in a web application, i.e., for a JSP file located at /mysite/hello.jsp. In a WAR file, the complete URL to the JSP at runtime is http://<hostname:port>/<web-app-context-root>/mysite/hello.jsp. JSP syntax falls into two categories: JSP directives and JSP actions. JSP directives are statements in the JSP syntax that govern properties that are global to the JSP. For example, the mime type of the content that the JSP will produce, as we saw in the clock example, or to which page the browser should be directed should the JSP encounter an error. JSP actions are statements in the JSP syntax that control some aspect of how a portion of the JSP output is created, such as including the output of another web component within the JSP or including a property value from a Java bean in a JSP. Now, let's take a short break. In the following video, we will begin examining the syntax of JSP directives in Eclipse. See you in the following video.

 

About the Author
Students
1300
Courses
64
Learning Paths
4

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