XML expression of JSP syntax

Source: Internet
Author: User
Tags date format expression final include return string trim
js|xml| syntax People always dislike to see <% and%> on the JSP page, in fact we can use the more elegant XML way to express
1, the implied annotation
JSP Syntax: <%--content--%>
XML syntax: none
2, the declaration of a variable or function
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, an expression
JSP Syntax:<%=expression%>
For example:
<%=math.round(math.random* -)%>
XML Syntax:
<jsp:expression>
   expression
</jsp:expression>
For example:
<jsp:expression>
   Math.Round(math.random* -)
</jsp:expression>

4, Code Snippets
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, directives: include
JSP Syntax: <% @include file="Relativeurl"%>
For example: <% @include file="Static_head.html"%>
XML Syntax: <jsp:directive.include file="Relativeurl"/>
For example: <jsp:directive.include file="Static_head.html"/>
6, Directive: page
JSP Syntax: <% @page attribtes%>
For example: <% @page Import="Java.util.HashMap"%>
XML Syntax: <jsp:directive.page attribute/>
For example: <jsp:directive.page Import="Java.util.HashMap"/>
7, directive: Taglib
JSP Syntax: <% @taglib uri="Uriforlibrary"prefix="TagPrefix"%>
XML syntax: none
for JSP operations, this is an XML format.
use XML expressions to write a JSP page below

example.jsp

<jsp:directive.page contenttype="TEXT/HTML;CHARSET=GBK"/>
<jsp:directive.page Import="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=(Date)Request.getattribute("Date");
    String datestr=getcustomdate(date,"Yyyy-mm-dd,hh:mm:ss");
</jsp:scriptlet>
<div style="font-size:11pt">
  Show Time:<jsp:expression>datestr</jsp:expression>
</div>

Related Article

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.