Welcome

首页 / 网页编程 / JSP / jsp基础语法 一 scriptlet

jsp基础语法 一 scriptlet2013-05-13 赵玉强 jsp作为WEB的开发基础,有其重要的地位,那么熟练掌握JSP的语法及应用就成了重中之重。

首先我们一起先从JSP的基本语法学起:(以下内容来自李兴华视频手稿整理)

scriptlet简介

script表示的是脚本小程序,像之前out.println()这个语句是缩写在<%%>之中的,很明显,这里面 缩写的语句就是一个script.

在jsp中最重要的部分就是Scriptlet(脚本小程序),所有嵌入在HTML代码中的java程序都必须使用

Scriplet标记出来,在jsp中一共有三种scriplet代码

第一种:<% %>,在此scriplet中可以定义局部变量、编写语句;

第二种:<% ! %>, 在此scriplet中可以定义全局变量、方法、类;

第三种:<% = %>,用于输出一个变脸或一个具体内容。

第一种script<%%>

<html><head><body><% int x=10; String info="www.baidu.com";out.println("<h2>x="+x++ +"</h2>");out.println("<h2>info="+info+"</h2>"); %></body></head></html>
将纯java代码插入到jsp页面中时要放在<%%>中

第二种script<%!%>

主要的功能是定义全局变量、方法、类,假设下面定义方法和类,进行验证

<%!public static final String info="www.baidu.com"; %> <%! 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>info="+info+"</h3>"); out.println("<h3>3+5="+add(3,5)+"</h3>"); out.println("<h3>"+new Person("zhengsan",30)+"</h3>"); %>