JSP Elements Part 2 - Scripting elements

With the scripting support you can write your own java logic in a JSP page.

There are mainly two forms of scripting in a JSP

  1. Newer Expression Language (EL)

  2. Traditional Scripting


Traditional language-based scripting elements

Traditional language-based scripting elements in a JSP are further divided into:

  1. Expressions

    1. Expressions use the result of evaluating a piece of Java code directly in the page output.

    2. An expression must begin with <%= and conclude with %>.

    3. The evaluated value is passed as the parameter to the print method of a JspWriter, and hence we should not end the statement with a semicolon.

    4. We can also use a function, but it should return something (and no semicolon).

      1. if we provide a function that return void inside an expression, JSP page will fail the translation stage. The expression result will be used as parameter code inside a method call, which will not accept a void.

    5. Example: <%= new Date() %>

      1. When this is generated into servlet code, the resulting Java statement will probably look like this: out.print(new java.util.Date());

  2. Scriptlets

    1. Scriptlets allow you to include different sets of Java statements inside the _jspService() method, and are executed on every request to the page.

    2. Here, the Java statements are incorporated “as is,” so you must terminate each with a semicolon.

    3. A scriptlet begins with a <% and ends with a %>.

    4. We can mix both template text and scriptlets. All code within scriptlets will go directly to the _jspService() method and the template text (such as <tr><td><font color="red">) is incorporated as out.write() statements in the _jspService() method, and expressions (such as <%= planets[i] %>) as out.print() statements.

    5. The template text, expressions, and scriptlets are all translated and generated into _jspService() in order of their appearance in the JSP page source.

    6. You can declare a local variable in one scriptlet and use that in another scriptlet or expression, as long as both are in same page. This is because both of them will be added in order to the _jspService() method.

    7. Example: <% System.out.println("Normal Java Code"); %>

  3. Declarations

    1. If you want to place code in the generated servlet outside of the _jspService() method (e.g. declaring another method), you can use declarations.

    2. It begins with <%! and end with %>.

      1. You can place in your declaration any code that can legally appear in a servlet source file: instance methods, static methods, static initialization code, static or instance data members, inner classes.

    3. Example:

      1. <%! public void jspInit() {

      2. System.out.println("TRIAL INITIALIZED");

      3. } %>

  4. Comments (starts with <%-- and ends with %>

    1. Whatever you give inside the comment element are ignored by the compiler.

    2. Starts with <%-- and end with --%>

    3. If you want comments sent within the web page output, you can use regular HTML comment syntax: Open the comment with <!-- and close with -->.

      1. An HTML comment is treated exactly like other HTML template text. So it’s perfectly acceptable to include JSP scriptlets, expressions, expression language, or any other legal JavaServer Page syntax within the comment—and it will be processed at translation time. However you cannot have scriptlet or any other traditional scripting element inside a JSP comment.


Expression Language (EL)

Expression language (EL) allows us to write scriptless JSP components by replacing the JSP expression scripting element.

EL expressions are represented in code as ${expression}.




$ { body_content }



EL only replaces expressions and we can also replace other traditional scripting elements with tag libraries, servlets or helper classes, allowing us to better divide tasks between java developers and html.xml developers.

EL can also be used along with standard actions and custom tags.

We will see expression language (EL) in detail later.

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)