JavaSercer Pages A.2 Scripting Elements Scripting elements let (Godaddy web hosting)
JavaSercer Pages A.2 Scripting Elements Scripting elements let you add small pieces of code to a JSP page, such as an if statement that generates different HTML depending on some condition. The scripting code must be written in the language defined by the page directive. It is executed when the JSP page is requested. A.2.1 Declaration A declaration starts with <%! and ends with %>. The content between the start and the end characters must be a complete, valid declaration in the scripting language defined by the page directive. The JSP implicit variables are not visible in a declaration element. A declaration can be used to declare a scripting language variable or method. When the scripting language is Java, a variable declared by a declaration element ends up as an instance variable in the JSP page implementation class. It is therefore visible to parallel threads (requests) processing the page, and needs to be handled in a thread-safe manner. A thread-safe alternative is to declare variables within a scriptlet element instead. It then becomes a local variable of the method in the page implementation class used to process each request, and is not shared by parallel threads. Example: <%! int globalCounter = 0; %> A.2.2 Expression An expression starts with <%= and ends with %>. The content between the start and the end characters must be a complete, valid expression in the scripting language defined by the page directive that results in or can be converted to a string. All JSP implicit variables are visible in an expression element. Example: <%= globalCounter++ %> A.2.3 Scriptlet A scriptlet starts with <% and ends with %>. The content between the start and the end characters must be a code fragment in the scripting language defined by the page directive. Scriptlet code fragments are combined with code for sending the template data between them to the browser. The combination of all scriptlets in a page must form valid scripting language statements. All JSP implicit variables are visible in a scripting element. Example: <% java.util Date clock = new java.util.Date( ) %> <% if (clock.getHours( ) < 12) { %> Good morning! <% } else if (clock.getHours( ) < 17) { %> Good day! <% } else { %> Good evening! <% } %> page 262
Quick Hint: If you are looking for cheap and reliable provider to host and run your servlet application check Vision servlet hosting plans