XML expression of JSP syntax

Source: Internet
Author: User
Tags date format expression final include 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:
<%!
   int i=0;int a,b,c;
   Color red=New Color(255,0,0);
   private static Final String ok="OK";
    PublicString getanswer(){
        returnOK;
   }
%>
XML Syntax:
<jsp:declaration>
    declartion;[declaration;]+...
</jsp:declaration>
For example:
<jsp:declaration>
   int i=0;int a,b,c;
   Color red=New Color(255,0,0);
   private static Final String 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=New SimpleDateFormat();
        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

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.