JSP基础语法
一、注释
1.显式注释,客户端可见
<!--注释-->
2.隐式注释,客户端不可见
//注释单行
/*注释多行*/
<%--JSP注释-->
二、Scriptlet
1.第一种 Scriptlet: <%%>
可以用于定义局部变量、编写语句等
<%
int x = 10;
String info ="www.126.com";
out.println("<h2>x="+x+ "</h2>");
out.println("<h2>info="+info+"</h2>");
%>2.第二种Scriptlet: <%!%>
可以定义全局变量、方法、类
<%!
public static finalString INFO = "www.126.com";
int x = 10;
%>
<%!
public int add(int x,int y){
return x+y;
}
%>
<%!
class Person{
private Stringname;
private int age;
publicPerson(String name, int age){
this.name = name;
this.age = age;
}
public StringtoString(){
return"name=" + this.name +"; age=" + this.age;
}
}
%>
<%
out.println("<h3>x="+ x++ + "</h3>"); //输出全局变量,每次刷新时都会变化
out.println("<h3>INFO="+ INFO + "</h3>"); //输出全局变量
out.println("<h3>3+5="+ add(3, 5) + "</h3>"); //调用方法
out.println("<h3>"+ new Person("udbful", 30) + "</h3>"); //生成对象
%>3.第三种Scriptlet: <%=%>,也称其为表达式输出
主要功能是输出一个变量或是一个具体的常量
<% String info ="www.126.com"; int temp = 30; %> <%--使用表达式输出变量与常量--%> <h3>info=<%=info%></h3> <h3>temp=<%=temp%></h3> <h3>name=<%="udbfl"%></h3>
输出一般用表达式输出而不用out.println()
例1:
<html>
<head>
<title>scriptlet</title>
</head>
<body>
<table border="1" width="100%">
<%
int rows = 10;
int cols = 10;
for(int x=0; x<rows; x++){
%>
<tr>
<%
for(int y=0; y<cols; y++){
%>
<td><%=(x*y)%></td>
<%
}
%>
</tr>
<%
}
%>
</table>
</body>
</html>例2略
三、scriptlet标签
提供<jsp:script></jsp:script>,与<%%>功能相同
<jsp:script>
Java scriptelet代码
</jsp:scipt>
以上内容参考JAVAWEB开发实战经典(名师讲坛)
本文出自 “走出地平线” 博客,请务必保留此出处http://udbful.blog.51cto.com/10601869/1683013
原文地址:http://udbful.blog.51cto.com/10601869/1683013