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>