From the course: Java EE 8 Essential Training

Parts of a JSP - Java EE Tutorial

From the course: Java EE 8 Essential Training

Start my 1-month free trial

Parts of a JSP

- [Instructor] Within a JSP page there are certain constructs used to build each page. Let's take a closer look at them. This table outlines the constructs found within JSP pages, some of which are used frequently and others that should be avoided. You'll notice that I've provided both the JSP page syntax as well as the XML document syntax. When building a JSP page you will use directives on almost every JSP page because they provide instructions to the JSP container on different attributes about the page, where to insert content and what tag libraries to include. You will also frequently use the expression language to bind to model data within JSP to display dynamic content. The remaining constructs are sort of relics from the past when application logic was embedded within JSPs and should for the most part be avoided. If you find yourself using these directives, you are most likely deviating from the MVC pattern. Here is a JSP example that uses each of the JSP constructs we discussed to print the time to the page. You will notice at the top of the file we have the page directive, which is mandatory. It indicates that our page is using Java and specifies the page in coding. Additionally, we use a tag directive to include the Java standard tag library, or JSTL. This provides useful tags for things like iteration or conditional display of page elements. Then we use declarations, scriplets, expressions, expression language, and the expression language with the JSTL tag to write content dynamically to the page. To adhere to MVC we should avoid embedding Java code directly in the page. It's not necessary and it really clutters it up. So we really want to stress that you should avoid scriplets, expressions, and declarations. But that raises another question. Where does the JSP get access to that date time attribute that we're using in the expression language. Let's take a quick look. The data came from a servlet that set an attribute on the request. Request attributes are a form of server side storage for a particular request. They allow data to be attached to the request for use by other components on the server such as JSPs. Additionally, you will see that we use an object of type RequestDispatcher to forward request processing execution from the servlet to the JSP page. So it's a way to achieve navigation from a servlet to a JSP. So that is how we pass data from a servlet to a JSP. However, there are a few other pieces of data a JSP has access to known as implicit objects. We'll take a look quickly at the implicit objects. They're objects that you can reference using the expression language directly in a JSP page without actually declaring them. You can see the objects available and their descriptions. I'll just point out that the request, session, and page context are the most widely used implicit objects when we're building our JSP pages because they provide access to some of the most common data we need when displaying dynamic content. So that's an overview of the parts of a JSP. Let's get hands on with the technology.

Contents