Skripsi <%
Apa sing bisa dilebokake menyang file JSP?
Kaping pisanan, kode Jawa. Yen sampeyan pengin nglebokake kode Java menyang JSP, banjur format umum diwenehake dening cithakan:
<%
Java code
%>
Sampeyan bisa ngilangi kode iki dadi sawetara bagean:
<%
Beginning of Java Code
%>
HTML-code
<%
End of Java Code
%>
Tuladha:
<html>
<body>
<%
double num = Math.random();
if (num > 0.95) {
%>
<h2> You are lucky, user!</h2><p>(<%= num %>)</p>
<%
} else {
%>
<h2> Today is not your day, user!</h2><p>(<%= num %>)</p>
<%
}
%>
</body>
</html>
Ekspresi <%=
Sampeyan uga bisa nglebokake ekspresi sing diwilang menyang file JSP. Ing wektu sing padha, parser JSP dhewe bakal nggawe manawa ora mung diwilang, nanging uga ditugasake yen perlu. Ekspresi ing njero kode diwenehi cithakan:
<%= expression %>
Elinga yen titik koma ora dibutuhake ing kene.
Conto servlet JSP kanthi pirang-pirang ekspresi:
<p>root of 10 equals <%= Math.sqrt(10) %></p>
<h5><%= item[10] %></h5>
<p>current time: <%= new java.util.Date() %></p>
Kode iki bakal diowahi dadi kode Jawa:
out.write("<p>");
out.write("The root of 10 is ");
out.print( Math.sqrt(10) );
out.write("</p>");
out.write("<h5>");
out.print( item[10] );
out.write("</h5>");
out.write("<p> Current time: ");
out.print( new java.util.Date() );
out.write("</p>");
Penting! Ing kode lan ekspresi Jawa, sampeyan bisa nggunakake variabel sing wis ditemtokake kayatapanyuwunan,wangsulan,sesi,metulan liya-liyane.
GO TO FULL VERSION