JSP Declaration Scripting Element.

JSP Declaration scripting elements

  • To embed Java coding into a JSP page, you need to use scripting elements. There are three scripting elements. available: Declarations, Scriptlets, and Expressions.
  • Declaration scripting elements are used to insert methods, constants, and variable declaration into JSP pages.
  • A programming language variable created within a declaration scriptlet is accessible from anywhere within the JSP page.
  • You can have multiple declaration scripting elements within a single JSP page. The JSP container will merge them into one when processing the page, so it isn’t necessary to separate them.
  • JSP declaration scripting elements are specified either in the form of XML tags, or in the form of a JSP style notations.
    Here is the notation for the Declaration scripting element:
    JSP Style or XML Description
    <%! declaration %> or
    <jsp:declaration > declaration </jsp:declaration>
    The content of declaration scripting is actually code fragments written in Java.

Example of using JSP declaration scripting element.

In the example we use Netbeans IDE and Glassfish Server.

You can download this example here (needed tools can be found in the right menu on this page).

If you like to participate in the review of this example you must first create a Web project in Netbeans (the project name is DeclarationScripting).

In this example, we will add two files, header.jsp and footer.jsp.

  • It is customary to place all include files, which are used several times in the WEB-INF folder. Some would also like the include files should have the extension .jspf, but this is not a requirement.
    Here is the file we want to include at the top of all pages:
    <h1>Java Server Page</h1>
    <hr>

    For those who participate in the review: create a JSP file in Netbeans and replace generated code for the JSP with that shown above (the JSP file name is Header.jsp and folder should be WEB-INF).

    Here is the file we want to include at the bottom of all pages:
    <hr>
    Page generated on <%= (new java.util.Date()).toString() %>

    For those who participate in the review: create a JSP file in Netbeans and replace generated code for the JSP with that shown above (the JSP file name is Footer.jsp and folder should be WEB-INF).

Then we need a JSP file to demonstrate how we use the Declaration Scripting Element.

  • Here is the main JSP file:
    <%@page contentType="text/html" pageEncoding="UTF-8" import="java.util.*" %>
    <!DOCTYPE html >
    <html>
      <head>
        <title>Java FAQ Welcome Page</title>
      </head>
      <body>
        <div style="max-width:400px; text-align: center; ">
          <%@ include file="/WEB-INF/header.jsp" %>
          <%!
            List<String> cars = 
                    Arrays.asList("Audi 2014 model",
                            "BMW 2016 model",
                            "Lada 2015 model", 
                            "Skoda 2009 model");
            String convertToHigh(String inStr) {
              return  inStr.toUpperCase();
            }
          %>
          <h3>In my garage I have the following cars:</h3>
          <%  for (String car : cars) {
          %>
          <p ><%= convertToHigh(car) %></p>
          <%
            }
          %>
          <%@ include file="/WEB-INF/footer.jsp" %>
          
        </div>
      </body>
    </html>

    For those who participate in the review: create a JSP file in Netbeans and replace generated code for the JSP with that shown above (the JSP file name is index).

How does this work?

  • We have created two JSP files, Header.jsp and Footer,jsp, which we have included in the JSP document, index.jsp.
  • In a declaration scripting elements (<%!) in the index.jsp file we have created a List of cars and a method for use in other scripting elements.
  • Typical use of a scriptlet Scripting Element is implementing the programming of the data flow. It is also normal to use established definitions in the declaration scripting elements.
  • With a expression Scripting Element we write out data to the browser.

Creating Deployment descriptor.

  • To run this JSP you have to deploy it to a web-server or a Application server. To deploy means to install the JSP with some instruction to a such server.
  • The instructions are mainly defined to be deployment descriptors. The standard part of the deployment descriptor should be in an XML-file with the name web.xml.

    You may need to create a Deployment descriptor file, web.xml in Netbeans.

  • The contents of the web.xml file should look like this:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.1" 
             xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
     http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
        <servlet>
            <servlet-name>ShowCars</servlet-name>
            <jsp-file>/index.jsp</jsp-file>
        </servlet>
        <servlet-mapping>
            <servlet-name>ShowCars</servlet-name>
            <url-pattern>/JSPCars</url-pattern>
        </servlet-mapping>
        <session-config>
            <session-timeout>
                30
            </session-timeout>
        </session-config>
        <welcome-file-list>
            <welcome-file>JSPCars</welcome-file>
        </welcome-file-list>
    </web-app>
  • This file starts with the normal xml tag for a XML file and the root tag for the deployment descriptor is web-app. Every ting inside the last tag is to tell the server about our application, which in this case is a JSP file.
  • With a servlet tag we give the JSP file a servlet name, which is used in the servlet-mapping tag to specify a url for the JSP file.
  • In this way we can have many urls for the same JSP file.
  • If no session-timeout (the server ends the service of the application after this time) is given a standard timeout for the server is used as timeout for the application.
  • The welcome-file tag specifies the startup for our application, which in this case and our application is the welcome file with url JSPCars. Reorganize the welcome-file-list to what is shown above.

Creating Web-server Deployment descriptor.

  • The context-root (in example /DeclarationScripting) for the application will in most cases be specified by a server vendor deployment descriptor.
    The browser will display:
© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.