JSP基础----Scriptlet
来源:互联网 发布:淘宝网乒乓球桌多少钱 编辑:程序博客网 时间:2024/05/16 06:14
在JSP中最重要的部分就是Sciptlet(脚本小程序),所有嵌入在HTML代码中的Java程序都必须使用Scriptlet标记出来。
在JSP一共有三种Scriptlet代码:
1.第一种:<%%>:在此Scriptlet中可以定义局部变量、编写语句
2.第二种:<%!%>:在此Scriptlet中可以定义全局变量、方法、类
3.第三种:<%=%>:用于输出一个变量或者一个具体内容
在<%!%>中定义的是变量就是全局变量,一般而言,使用<%!%>的操作都是声明全局常量较多,而所谓的定义方法或定义类基本上是不使用的。
样例1:
<%!public static final String INFO = "www.huangmeixi.cn";int x = 10;%><%!public int add(int x, int y){return x + y;}%><%!class Person{private String name;private int age;public Person(String name, int age){this.name = name;this.age = age;}public String toString(){return "name = " + this.name + ";age = " + this.age;}}%><%out.println("<h3>x = " + x++ + "</h3>");out.println("<h3>" + new Person("Joywy", 23) + "</h3>");out.println("<h3>3 + 5 = " +add(3, 5) + "</h3>");%>
样例2:
<%String info = "www.huangmeixi.cn";//局部变量int temp = 30;%><h3>info = <%=info%></h3><h3>temp = <%=temp%></h3><h3>name = <%="Joywy"%></h3>
样例3:
<html><head><title>Insert title here</title></head><body><%int rows = 10;int cols = 10;%><table border="1" width="100%"><% for(int x = 0; x < rows; x++){%><tr><% for(int y = 0; y < cols; y++){%><td><%=x*y%></td><% }%></tr><% }%></table></body></html>以上代码达到了HTML代码和JAVA代码相分离,以后在使用输出的时候坚决不再使用out.println,而使用<%=%>
样例4:(通过交互性打印表格)
index.html
<html><head><title>交互式打印表格</title></head><body><form action="print_table.jsp" method="post"><table><tr><td>请输入要显示表格的行数:</td><td><input type="text" name="row"></td></tr><tr><td>请输入要显示表格的列数:</td><td><input type="text" name="col"></td></tr><tr><input type="submit" value="显示"><input type="reset" value="重置"></tr></table></form></body></html>
print_table.jsp
<html><head><title>Insert title here</title></head><body><%int rows = 0;int cols = 0;try{rows = Integer.parseInt(request.getParameter("row"));cols = Integer.parseInt(request.getParameter("col"));}catch(Exception e){}%><table border="1" width="100%"><% for(int x = 0; x < rows; x++){%><tr><% for(int y = 0; y < cols; y++){%><td><%=x*y%></td><% }%></tr><% }%></table></body></html>
Scriptlet标签:
JSP中专门提供了一种Scriptlet的标签,语法如下:
<jsp:scriptlet>
Java的scriptlet代码
</jsp:scriptlet>
样例5:
<html><head><title>交互式打印表格</title></head><body><jsp:scriptlet>String url = "www.huangmeixi.cn";</jsp:scriptlet><h2><%=url%></h2></body></html>
- JSP基础----Scriptlet
- JSP基础语法:注释、Scriptlet、编译指令
- JSP-Scriptlet
- JSP语法(5)Scriptlet
- JSP语法 Scriptlet
- JSP 全局变量 scriptlet变量
- jsp中的scriptlet
- JSP中的Scriptlet
- 3.JavaWeb基础 Scriptlet
- JSP基础语法之一:Scriptlet使用、简单的JSP获取表单数据再输出表格
- JSP中的scriptlet和注释
- JSP语法基础2——Scriptlet、Page指令和Taglib指令
- Scriptlet
- jsp参考(四) 脚本元素[scriptlet]
- http://localhost:8080/jsptech/scriptlet.jsp
- JSP注释、声明、指令、scriptlet、表达式
- JSP中的注释操作及JSP中的三种Scriptlet
- 01---jsp基本语法01(注释与scriptlet)
- redis学习总结之主从复制
- 第二部分——第十一章/泛型算法
- Linux远程编译
- 对象内存空间分配与初始化
- JavaScript前端开发框架
- JSP基础----Scriptlet
- Linux网络协议栈 -- socket bind 地址绑定
- Mysql 维护的一点整理
- Android关于SD卡中多层目录的创建
- Java基础回顾(四)
- JRuby 1.7 Preview 1发布:默认使用Ruby 1.9,通过Invokedynamic改进了性能
- bochs调试指令
- ISO 20000之业务关系管理
- JUnit的使用