Pr.Pg Next Pg

JSP tutorials of Scripting Elements

  • There are five types of JSP Scripting Elements as shown in below table:

Sr No

Scripting Elements

Scripting Syntax

1

Comment

<%-- JSP Comment --%>

2

Directive

<%@ directive %>

3

Declaration

<%! declaration %>

4

Scriptlet

<% code %>

5

Expression

<%= expression %>

 

JSP Comment

  • JSP specification has describe only one type of comment for JSP.

 

Syntax:

<%-- This is JSP Comment --%>

  • JSP container will ignore the comment part from compilation and that comment part will not be included in content parsed for response.

 

Example:

<%-- This will be ignored --%>

 

JSP Directive

  • There are three main types of directives as below:
    1. page directive
    2. include directive
    3. taglib directive

 

  1. page directive:

  • The page directive is used to specify attributes for the JSP page as a whole.
  • The page directive does not produce any visible output when the page is requested but change the way the JSP Engine processes the page.

 

Syntax:

 

<%@ page [attribute=”value” attribute=”value”………….] %>

  • There are many attributes associated with the page directive some are in below chart:

 

page directive attributes

Description

buffer

This attribute is used for specifying the buffer size for the output stream and default size is 8kb.

autoFlush

True indicates that the buffer should be flushed when it is full and false indicates that an exception should be thrown when the buffer overflows.

contentType

This specifies the MIME type of the output.

errorPage

This indicates whether or not the current page can act as the error page.

isErrorPage

This indicates whether or not the current page can act as the error page.

extends

Indicates the superclass of servlet when jsp translated in servlet.

import

This attribute defines the list of packages, each separated by comma 

info

This defines a string that can be retrieved via the getServletInfomethod

isThreadSafe

True indicates multithreading and false indicates that the servlet should implement SingleThreadModel

language

This tells the server about the language to be used in the JSP file. Presently the only valid value for this attribute is java.

session

true indicates session should be bound to the existing session if one exists, otherwise a new session should be created and bound to it and false indicates that no sessions will be used.

pageEncoding

This defines data type of page encoding.

 

Example:

 

<%@page language=”java”%>

 

 

  1. include directory:

  • The jsp:include action element is like a function call. At runtime, the included file will be ‘executed’ and the result content will be included with the source JSP page.

  • When the included JSP page is called, both the request and response objects are passed as parameters.

  • Typically include files are used for headers, footers, tables and navigation that are common to multiple pages.

  • The included page should not be another dynamic page so the included file does not need to be a complete and valid JSP or servlet.

Syntax:

<%@ include file="file_name"%>

 

  1. taglib directory:

  • The taglib directive makes custom actions available in current page through the use of tag library.

 

Syntax:

<%@ taglib uri="tag Library_path" prefix="tag_prefix"%>

  • In the above syntax uri is the absolute path and prefix is unique identify custom tags from library used in JSP page.

 

JSP Declaration

  • A JSP declaration lets you declare or define variables and methods (fields) that use into the jsp page.
  • Declaration tag start with  <%! and end with %> 
  • The Code placed inside this tag must end with a semicolon (;).

 

Syntax:

<%! Java class or type declaration%>

 

JSP Scriptlet

  • JSP Scriptlet contain the Java code inside JSP pages.

  • Scriptlet starts with <% and end with %>

  • Code inside scriptlet must end with semicolon (;).

 

Syntax:

<% Java Code ; %>

 

JSP Expression

  • A JSP expression is used to insert Java values directly into the output. 

  • JSP Expression tag is use for evaluating any expression and directly displays the output in appropriate web browser.

  • Expression tag start with <%= and end with %>

  • The code placed inside this tag will not end with semicolon (;).

 

Syntax:

<%= Java expression %>

Pr.Pg border                                              Next Pg