02. Scripting elements

1. Scripting elements

These are the 5 scripting elements 

  1. Comment <%-- Set of comment statements --%>
  2. Directive <%@ directive %>

  3. Declaration <%! declarations %>

  4. Scriptlet <% scriplets %>

  5. Expression <%= expression %> 



2. Directives


There are 3 directives :
  • Page: <%@ page attribute = "attribute_value %> 
  • Include: <%@ include ... %>
  • Taglib: <%@ taglib ... %>

* For the page directive, that has this equivalent in XML: 
<jsp:directive.page attribute = "attribute_value" /> 
these attributes are used:

1. buffer (size)
2. contenType (document's MIME in the HTTP response header)
3. autoflush (decides if the buffer output should be flushed automatically)
4. errorPage (to handle JSP errors)
5. import (list of classes and packages used)
6. isErrorPage (if this is an error page)
7. info (information that can be obtained using getServletInfo() from the servlet)
8. isThreadSafe (for defining the thread model)
9. Language (default java)
10. Session (if page participates in the current HTTP session) 
11. isELIgnored (for ignoring the expression language (EL))
12. isScriptingEnabled (for allowing scripting elements)

* For the include directive, that has the XML equivalent :
<jsp:directive.include file = "relative url" />
HTML, JSP, text and other files can be included 

* For the taglib directive that is used for defining a tag library with a "taglib" as prefix

Here is an example:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding = "ISO-8859-1"%>
<%@ include file="directive_header_code.jsp" %>
<%@ taglib uri = "http://www.example.com/custlib" prefix = "w3tag" %>
  
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
        <title>Include Directive Example</title>
    </head>
    <body>
        <p>This file includes a header file named directive_header_code.jsp</p>
    </body>
</html>


3. Declaration of methods and variables


<%! declaration; [ declaration; ]+ ... %>

Example:

<!DOCTYPE html>
<html>
    <body>
        <%! int variable_value=62; %>
        <%= " Your integer data is :"+ variable_value %>
    </body>
</html>

4. Scriptlets


<% JAVA CODE %>

Example:

<!DOCTYPE html>
<html>
    <head>
        <title>Scriptlet Tag</title>
    </head>
    
    <body>
	<table border="1">
	    <% for ( int g = 1; g <= cnt; g++ ) { %>
	    <tr>
        	<td>Number</td>
         	<td><% = g+1 %></td>
    	    </tr>
    	    <% } %>
	</table>
    </body>
</html>

5. Expression "to print"


<%= "Text to print" %>

Avoids using "out.print()" for writing data
Example:

<!DOCTYPE html>
<html>
    <head>
        <title>Scriptlet Tag</title>
    </head>
    
    <body>
	<table border="1">
	    <% for ( int g = 1; g <= cnt; g++ ) { %>
	    <tr>
        	<td>Number</td>
         	<td><% = g+1 %></td>
    	    </tr>
    	    <% } %>
	</table>
    </body>
</html>






Comentarios

Entradas populares de este blog

15 Typescript (3) json ..

10. Some Javascript & JQuery concepts

14 Typescript (2) with jquery, bootstrap and other module stuff