We do not like to see <% and %> on JSP pages. In fact, we can use a more elegant XML format to express
1. Implicit comments
JSP Syntax: <% -- content -- %>
XML Syntax: None
2. Declaration of variables or functions
JSP Syntax: <%! Declaration; [Declaration;] +... %>
For example:
<%!
IntI = 0;IntA, B, C;
Color red =NewColor (255, 0, 0 );
Private Static finalString OK = "OK ";
PublicString getanswer (){
ReturnOK;
}
%>
XML Syntax:
<JSP: Declaration>
Declartion; [Declaration;] +...
</Jsp: Declaration>
For example:
<JSP: Declaration>
IntI = 0;IntA, B, C;
Color red =NewColor (255, 0, 0 );
Private Static finalString OK = "OK ";
PublicString getanswer (){
ReturnOK;
}
</Jsp: Declaration>
3. Expression
JSP Syntax: <% = expression %>
For example:
<% = Math. Round (math. Random * 100) %>
XML Syntax:
<JSP: expression>
Expression
</Jsp: expression>
For example:
<JSP: expression>
Math. Round (math. randx * 100)
</Jsp: expression>
4. code snippet
JSP Syntax: <% Your Java code %>
For example:
<%
String username = request. getparameter ("username"). Trim ();
String Password = request. getparameter ("password"). Trim ();
%>
XML Syntax:
<JSP: scriptlet>
Your Java code
</Jsp: scriptlet>
For example:
<JSP: scriptlet>
String username = request. getparameter ("username"). Trim ();
String Password = request. getparameter ("password"). Trim ();
</Jsp: scriptlet>
5. Command: Include
JSP Syntax: <% @ include file = "relativeurl" %>
Example: <% @ include file = "static_head.html" %>
XML Syntax: <JSP: Directive. Include File = "relativeurl"/>
Example: <JSP: Directive. Include File = "static_head.html"/>
6. Command: Page
JSP Syntax: <% @ page attribtes %>
Example: <% @ pageImport= "Java. util. hashmap" %>
XML Syntax: <JSP: Directive. Page attribute/>
Example: <JSP: Directive. PageImport= "Java. util. hashmap"/>
7. Command: taglib
JSP Syntax: <% @ taglib uri = "uriforlibrary" prefix = "tagprefix" %>
XML Syntax: None
JSP operations are in XML format.
Below is a JSP page written in XML format
Example. jsp
<JSP: Directive. Page contenttype = "text/html; charset = GBK"/>
<JSP: Directive. PageImport= "Java. util. *, java. Text. *"/>
<JSP: declartion>
PublicString getcustomdate (date, date, string pattern ){
Simpledateformat format =NewSimpledateformat ();
Format. applypattern (pattern );
ReturnFormat. Format (date );
}
</Jsp: declartion>
<JSP: scriptlet>
Date = (date) request. getattribute ("date ");
String datestr = getcustomdate (date, "yyyy-mm-dd, HH: mm: SS ");
</Jsp: scriptlet>
<Div style = "font-size: 11pt">
Display time: <JSP: expression> datestr </jsp: expression>
</Div>