JSP Page and Include Directives

JSP Page and Include Directives

In the previous tutorial, we have learned that What a JSP? is and how tomcat container converts it into a servlet and executes it, In this tutorial, you will learn JSP in a little bit of more detail. To explain the concept of directives, we will use some examples.

JSP Display Time Example:

We will create a new JSP and try to simply display a Time when the user accesses a JSP. To create a JSP as  we have learned in our previous tutorial, Go to Navigation Tab on the left side of Eclipse, Find JEE project and expand the project, that we are using you can also create a new JEE project, find “WebContent” Folder  Right click on it click “New” go to “Others” then find “Web” folder, in this folder click JSP File and then Click Next, Finally Name your new JSP and follow the wizard, this will create a new JSP file Like this.

Import in JSP

Now we will use the import  attribute in the top most line of JSP file

This line (<%@ page language=“java” contentType=“text/html; charset=ISO-8859-1”

pageEncoding=“ISO-8859-1” %>) Will become (<%@ page language=“java” contentType=“text/html; charset=ISO-8859-1”

pageEncoding=“ISO-8859-1” import=“java.util.Date”%>)

finaly add this line in the body of our code (The Time is : <%=new Date()%>)

our source will look like this.

We will also see what other attributes are, but first, let’s run this example.Right, Click on File Select “RunAs” then “Run on Server”.

JSP Date Time Output
JSP Date Time Output

JSP Page Directive

In any JSP File the First line is the “Page Directive” defined like this (<%@ page language=“java” contentType=“text/html; charset=ISO-8859-1” pageEncoding=“ISO-8859-1” %>) In it we can set different properties for JSP page attribute (language=“java”) tells us that all the script tag inside this file contain the java code. In one of the servlet tutorials, we have seen that in doGet(request, response) method we write something like this response.setContentType(“text/html”)

JSP Content Type

Here in JSP we use (contentType=“text/html) attribute to set the page format to text/html. There are other attributes also that can be used in the page directive; we will discuss them gradually. If we have a lot of attributes to set we can also use multiple page directive tags (<%@ page %>) as shown in the below source.

Include directive Example:

We have another directive other than the page directive(<%@ include file=“../OtherJSPFile.jsp” %>) and it is called include directive, for explanation lets create a new JSP file with name “HeloJSP.jsp”. Like this

Now edit previously created Date Time JSP like this

Try to run this source this will give the output of both the JSPs on the same Page as shown below.

JSP include directive Example
JSP include directive Example

In the Same way, we can add complex JSP inside another JSP.

Summary:

In this tutorial, we have accomplished JSP include directives and how to display date and time in JSP by using include directives.

Leave a Reply

Your email address will not be published. Required fields are marked *