..
Page A JSP is translated systematically into a normal Servlet from the JSP engine, before being passed to the java compiler. This generation process, involves the 'use of different types of syntactic elements, each of which is associated with a certain type of processing. We will see in this article of these three elements: the static content, the script, and directives.
Static content is just the HTML / Javascript / CSS to insert the page and requires no additional markup, but only use normal HTML tags. Instead we see in detail the other two elements that have some particularities that should be deepened.
Scripts (Scripting Elements)
The script is the java code that "coexists amicably" on the same page with the 'HTML, but unlike the latter is generated dynamically.
JSP technology provides Inventory report three types of scripts to insert the code, where everyone enjoys a well-defined position to 'inside of the generated servlet:
The declarations allow us to declare variables and methods in the page via the syntax:
<%! declaration%>The JSP engine translates the statements as fields and methods of the Servlet class generated. An example can be:
<%!
String name = "Antonio"%>
/ / Or we can declare an entire method:
<%!
public java.util.Date getData () {
return new java.util.Date ();
}
%>
The expressions are elements of the script that are evaluated logically, mathematically, or literally, the result of which is converted in java.lang. String.
The syntax is as follows:
<% = Expression%>When the JSP engine encounters an 'expression during the process of translating it directly generates the result and inserts it in the output stream. A possible expression can be:
<% = System.currentTimeMillis ()%>In this case, is placed in the output stream the result of the function that the timestamp Riton system.
The Scriptles finally allow us to place all 'inside of the JSP page portions of Java code that is inserted into the Servlet at compile time. The tag syntax is:
<% Java code%>The translator puts it all when he meets a scriptlet in the method _jspService.
Here's an example that uses all types of tags:
<! - Declaration ->
<%!
String message;
public java.util.Calendar getData () {
java.util.GregorianCalendar return new ();
}
%>
<html>
<head>
<title> JSP Tags </ title>
</ Head>
<body>
<! - Scriptlet ->
<%
java.util.Calendar date = getData ();
int hour = data.get (java.util.Calendar.HOUR);
int minutes = data.get (java.util.Calendar.MINUTE);
int seconds = data.get (java.util.Calendar.SECOND);
String time = "! Time:" + hours +":"+ +":"+ minutes seconds;
if (data.get (java.util.Calendar.AM_PM) == 0) message = "Hello <b> </ b>" + zone + "AM";
else message = "Good evening <b> </ b>" + zone + "PM";
%>
<! - Expression ->
<% = Message%> <br/>
</ Body>
</ Html>
The 'example generates an HTML page which displays a Hello or Good evening (depends on' time) with the time on the drawing.
| |
Flash MX (Advanced)
Become a designer of Web sites from 29 €. |
| |
Ruby and Ruby on Rails (Course)
Create software and Web applications with Ruby and RoR. From 39 €. |
| |
XML (Course)
Creation of XML structures, XSL and other languages extensible. Starting from 29 €. |