A JSP directive is a global definition sent to the JSP engine that remains valid regardless of any specific requests made to the JSP page. A directive always appears at the top of the JSP file, before any other JSP tags. This is due to the way the JSP parsing engine produces servlet code from the JSP file.
The syntax of a directive is:
<%@ directive directive_attr_name = value %>
Directives are grouped as follows:
Directives provide instruction to the JSP container regarding the entire JSP page. This information is used in translating or executing the page. The basic syntax is as follows:
<%@ directive attribute1=”value1″ attribute2=”value2″… %>
Attributes of the page directive includes:
The following example shows you how errorPage directive works.
Step 1 Create new Dynamic Web Project
Step 1.1 Create New Project
Go to File->New->Others… Select Dynamic Web Project under Web category then click Next. TypeServletDemo or whatever name you want in the Project name field.
Step 1.2 Configure Target Runtime
- Click New Runtime… button under Target Runtime.
- Select Apache Tomcat v7.0. Click Next.
- Click Browse button to specify Tomcat Server installation directory. The select the folder then click Ok.
- Click Installed JREs… button to add/remove or edit JRE definition. Remove existing one; then click Add button to configure new one. Select Standard VM; then click Next. Click Directory button to select the root folder ofJDKinstallation. Select the folder then click Ok. Click Finish.
- Check the JDK then click Ok.
- Change JRE to the JDK then click Finish.
- Click Finish the finish the project creation.
Step 2 Create The First JSP
This JSP contains a runtime error – the division by zero.
- Right click to the project or WebContent folder, select New -> JSP File from the menu.
- You can choose any name, for example pageDirectiveDemo.jsp;
- Then click Finish.
- Eclipse IDE will generate some boilerplate code. We just clear all its content and replace with the followings code:
<%@ page errorPage="errorPage.jsp" %>
<% int i = 1/0; %>
Step 3 Create The Second JSP
This JSP will be forwarded to and display the error.
Similarly, create new JSP and name it errorPage.jsp.
<%@ page isErrorPage="true"%>
Step 3 Configure Tomcat
Select Servers window; in case it’s not there, go to Windows -> Show View-> Others… Select Server -> Servers; then click Ok.
If this is the first time you are creating a server, in the Servers area you will see a link as No servers are available click this link to create a new server. Click the link.
Define a New Server window will be opened, In Select the server type. Select Apache -> Tomcat v7.0 Server. Then click Finish.
Step 4 Deploy The Application
- Right click to the Tomcat Server select Add and Remove.
- Move the application (JSPDemo) from Available column to Configured. Then click on Finish to deploy it to tomcat server.
Step 5 Start Tomcat Server
Right click on the tomcat server and choose Start or simply click on the green start icon to start the server.
Step 6 Run The JSP
We assume that you install tomcat on default port (8080) and project name is JSPDemo. Open web browser and type:
java.lang.ArithmeticException: / by zero
The include directive allows substitution of text or code to occur at translation time. You can use the include directive to provide a standard header on each JSP page, for example:
<%@ include file=”test.html” %>
The following example shows you how include directive work.
Step 1 Create New JSP File
Create a new JSP file named includeDemo.jsp. This page embeds includedContent.html content into it.
<h1>This is header</h1>
<%@ include file="includedContent.html" %>
<h1>This is footer</h1>
Step 2 Create The HTML Page
- Right click to the project or WebContent folder, select New -> HTML File from the menu.
- Name it includedContent.html.
- Click Finish.
- Eclipse IDE will generate some boilerplate code. We just clear all content of the file and replace by the code as below:
This is included content
Note: The included page can be JSP or HTML…
Step 3 Run The JSP
Open web browser and run the following URL:
This is header
This is included content
This is footer
Note: for JSP, you don’t need to Clean & Build the project or restart the tomcat server before running. You just change the code, save the changes and run.
For java classes, you need to Clean & Build the project to re-compile the code. In case register new servlet, change settings in web.xml or server.xml… you need to Clean & Build the project and restart tomcat server.