2. JSP Scripting elements & Directives

 

JSP Scriptlet Tag (Scripting elements)

JSP scriptlet tag also known as Scriptlets are nothing but java code enclosed within <% and %> tags. 

The main purpose of scriptlet tag is to add java code into a JSP page.

In JavaServer Pages (JSP), scripting elements are used to embed Java code directly into the HTML content of a JSP page. Scripting elements allow developers to write dynamic content and execute Java code within the JSP file. There are three types of scripting elements in JSP: 


Syntax :

<% Executable java code %>   or  <jsp:scriptlet>  java code </jsp:scriptlet>


1. Declarations: <%! Declaration %>

   - Declarations are used to declare variables or methods in the JSP page.

   - They are enclosed between `<%!` and `%>` tags.  

   - Declarations are typically used for defining variables that can be accessed throughout the entire JSP page.

   - Example:

     

     <%!

        int count = 0;

        String message = "Hello, JSP!";

        // Declare methods or other variables here 

     %>

     


2. Scriptlets : <% scriptlet %>

   - Scriptlets contain Java code that is executed each time the page is requested.

   - They are enclosed between `<%` and `%>` tags.

   - The code inside scriptlets is placed within the service method of the generated servlet.

   - Example:

          <%

        for (int i = 0; i < 5; i++) {

            out.println("Count: " + i + "<br>");

        }

     %>

     


3. Expressions: <%= expression %>

   - Expressions are used to output data onto the client's web browser.

   - They are enclosed between `<%=` and `%>` tags.

   - The result of the expression is converted to a string and included in the HTML output.

   - Example:

     

     <p>

        The current date and time is: <%= new java.util.Date() %>

     </p>

     

Remember that while scripting elements offer a convenient way to embed Java code in JSP, it's generally recommended to use JavaBeans or custom tags for more complex logic, to promote better code organization and maintainability.



JSP Directive 

JSP Directives control the processing of an entire JSP page. It gives directions to the server regarding processing of a page. 

There are three types of Directives in JSP:

1) Page Directive

2) Include Directive 

3) TagLib Directive


1. Page Directive

In JavaServer Pages (JSP), page directives are used to provide global information about the characteristics of a JSP page. They are typically placed at the beginning of a JSP file and are enclosed within `<%@ %>` tags. Here are detailed notes on various page directives in JSP:


1. autoFlush:

   - Description: Specifies whether the output buffer should be automatically flushed.

   - Values: "true" or "false".

   - Default: "true".

   - Example: `<%@ page autoFlush="false" %>`


2. buffer:

   - Description: Defines the buffer size for the output stream in kilobytes.

   - Values: Any positive integer.

   - Default: Platform-dependent.

   - Example: `<%@ page buffer="8kb" %>`


3. contentType:

   - Description: Sets the MIME type of the response.

   - Values: Any valid MIME type (e.g., "text/html", "text/plain").

   - Default: "text/html".

   - Example: `<%@ page contentType="text/xml" %>`


4. errorPage:

   -Description: Specifies the URL of the page to redirect to in case of an unhandled exception.

   - Values: A relative or absolute URL.

   - Example: `<%@ page errorPage="/error.jsp" %>`


5. extends:

   - Description: Specifies the superclass that the generated servlet should extend.

   - Values: A fully qualified class name.

   - Default: `HttpServlet`.

   - Example: `<%@ page extends="com.example.MyCustomServlet" %>`


6. import:

   - Description: Declares a list of Java classes to be imported.

   - Values: A comma-separated list of fully qualified class names.

   - Example: `<%@ page import="java.util.*, java.io.*" %>`


7. info:

   - Description: Provides descriptive information about the JSP page.

   - Values: Any string.

   - Example: `<%@ page info="This is a sample JSP page" %>`


8. isELIgnored:

   - Description: Specifies whether Expression Language (EL) expressions are ignored.

   - Values: "true" or "false".

   - Default: "false".

   - Example: `<%@ page isELIgnored="true" %>`


9. isErrorPage:

   - Description: Indicates whether the page is designed to handle errors.

   - Values: "true" or "false".

   - Default: "false".

   - Example: `<%@ page isErrorPage="true" %>`


10. isThreadSafe:

    - Description: Specifies whether the servlet generated from the JSP page is thread-safe.

    - Values: "true" or "false".

    - Default: "true".

    - Example: `<%@ page isThreadSafe="false" %>`


11. language:

    - Description: Specifies the scripting language used in the JSP page.

    - Values: "java" or other supported scripting languages.

    - Default: "java".

    - Example: `<%@ page language="groovy" %>`


12. pageEncoding:

    - Description: Sets the character encoding for the JSP page.

    - Values: Any valid character encoding (e.g., "UTF-8", "ISO-8859-1").

    - Default: The default character encoding of the server.

    - Example: `<%@ page pageEncoding="UTF-8" %>`


13. session:

    - Description: Specifies whether the page participates in session tracking.

    - Values: "true" or "false".

    - Default: "true".

    - Example: `<%@ page session="false" %>`



2. Include Directive

Include directive is used to copy the content of one JSP page to another. It’s like including the code of one file into another.


Syntax of Include Directive:

<%@include file ="value"%>

here value is the JSP file name which needs to be included. If the file is in the same directory then just specify the file name otherwise complete URL(or path) needs to be mentioned in the value field. 

Example : 

<%@include file ="footer.jsp"%>


3. Taglib Directive

This directive basically allows user to use Custom tags in JSP. 


Syntax of Taglib Directive:

<%@taglib uri ="taglibURI" prefix="tag prefix"%>

Where URI is uniform resource locator, which is used to identify the location of custom tag and tag prefix is a string which can identify the custom tag in the location identified by uri.

Comments

Popular posts from this blog

1. JSP Introduction And Life-Cycle

3. JSP Action Tag and Expression Language