XML expression of JSP syntax

Source: Internet
Author: User

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.