JSP标准语法

 
1、JSP的注释语法:
 
      <%--comments--%>
 
     其中,comments是你可以添加的任意文本注释,但是不能使用“--%>”,
     如果非使用不可,请用“--%\>”
 
     实例:
     <%--
     FileName:helloworld.jsp
     Author:rossini
     
Date:2004-1-29
     Note:显示一个
"Hello World!"信息
     
--
%>
    
<html> 
    
<head> 
    
<title>JavaServer Pages Sample-Hello World!</title> 
    
</head> 
    
<body>
    
<% 
           out.print(
"Hello World!"); 
    
%> 
    
</body>
    
</html> 
2、JSP的声明语法:
   
     <%!declarations;... ;%>
 
     (a)在JSP中使用到的变量和函数必须事先声明,并以分号“;”结尾
     (b)在JSP声明中允许一次声明多个变量
     (c)在JSP声明中,不但可以声明变量,还可以声明函数或者自定义类
     (d)你可以直接使用在include编译指令中被包含进来的已经声明的变量
         和方法,不能在当前的JSP程序中对它们进行重新进行声明;JSP声
         明的作用范围是页面层的,一个声明只在一个页面有效;如果想在
         每个页面都使用一些声明,最好把他们写在一个单独文件里面,然
         后用include编译指令或者是<jsp:include>操作指令包含进来
 
    实例:
    <html> 
    
<head> 
    
<title>JavaServer Pages Sample-Declarations</title> 
    
</head> 
    
<body>
    
<%!String msg="变量声明";%>
    
<H1><%=msg%></H1> 
    
</body>
    
</html>
3、JSP表达式语法:
 
      <%=JAVA表达式%>
 
      JAVA表达式是一个值,转换成字符串后插入到页面中,不能用分号(;)
      来作为JAVA表达式的结束符。一个表达式可以变的很复杂,它可能由
      多个合法的JAVA表达式组成,这些表达式执行顺序是从左到右。如果
      一个表达式的结果不能转化为String类型,将会导致错误发生。
 
    实例:
    <html> 
    
<head> 
    
<title>JavaServer Pages Sample-Declarations</title> 
    
</head> 
    
<body>
    
<%!String msg="变量声明";%>
    
<H1><%=msg%></H1> 
    
</body>
    
</html>
4、JSP程序段语法:
 
     <% 程序段%>
 
     (a)程序段中只能包含合法的JAVA语法的代码,不允许出现HTML标记,
         JSP标记,JSP指令等元素。但是可以使用“<%”,“%>”标记,交错
         使用JAVA代码、HTML标记。
     (b)程序段中对变量的声明最好进行初始化,否则有些服务器可能会出错。
 
    实例:
   <html> 
    
<head> 
    
<title>JavaServer Pages Sample-dribs and drabs</title>
    
</head> 
    
<body>
    
<%
    
String words="welcome!";
    
int font_size=0;
    
for(int i=0;i<8;i++){
    
%>
     
<FONT SIZE=<%=++font_size%>><%=words.charAt(i)%></FONT>
    
<%
    }
    
%>
    
</body>
    
</html>
5、JSP编译指令PAGE:
 
     <%@ page
     language="java"  //指出使用的语言是JAVA,这既是缺省的又是唯一合法选项
     import="package.class1,...,package.classN" // 用于引入程序中需要使用的JAVA
                                                                        //程序包,多个包之间用逗号分割
     contentType="text/html;charset=gb2312"    //制定输出的MIME类型
     errorPage="URL"  //指定一个JSP页面来处理任何当前页面并未处理的意外错误
     isErrorPage="true | false"  //该属性指示当前页面是否可以作为其他页面的错误处理
                                               //页面,默认值false
     isThreadSafe="true | false"   //值为false表示Serverlet以单线程模式工作,值为true表示
                                                  //多个请求被一个Serverlet并行处理,默认值true
     session="true | false"   //true表示session可用,false表示session不可用
     buffer="size | none"  //确定out对象输出缓冲大小,默认大小为8KB
     autoflash="true | false"   //默认值为true,表示当缓冲满时将自动清空
     extends="package.class"  //指出将要生成的serverlet使用哪个超类
     info="message"   //定义当前JSP文件的一些相关信息,如作者,功能描述等
     %>
 
    实例:
   <%@ page language="java"
             import
="java.util.*"
             contentType
="text/html;charset=gb2312"
    
%>
    
<html> 
    
<head> 
    
<title>JavaServer Pages Sample-Page你好</title>
    
</head> 
    
<body>
    
<%=(new Date()).toLocaleString()%>
    
</body>
    
</html> 
6、JSP编译指令INCLUDE:
 
     <%@ include file="relativeURL" %>
 
     include指令用于在JSP文件转换成Serverlet时引入一个静态文件,这个文件可以是JSP
     和HTML以及文本文件等。许多网站的每个页面都有一个同样的导航条,为了避免重复
    ,可以单独编写一个页面来描述这个导航条,然后利用include指令把这个文件包含在
    每个主页面里。不过这种方法有一个缺点,因为这个导航条是在页面被编译时插入的
    ,如果导航条改变了,那么每个包含这个导航条的页面都必须重新编译。因此,如果
    导航条经常改变,应该使用jsp:include动作来实现。
 
实例:
 
主文件(index.jsp)
<html> 
<head> 
<title>JavaServer Pages Sample-include</title>
</head>
<%@ page language="java"
   contentType
="text/html;charset=gb2312"
%>
<body>
<H1>当前时间</H1>
<%@ include file="time.html"%>
</body>
</html> 
引入文件(time.html)
<%=(new java.util.Date().toLocaleString())%>
7、JSP操作指令INCLUDE(此操作指令的中文参数传入问题还没有解决):
 
     <jsp:include page="relativeURL | <%=expression%>" flush="true"/> 或者
 
     <jsp:include page="relativeURL | <%=expression%>" flush="true">
          <jsp:param name="parameterName" value="parameterValue | <%=expression%>"/>
     </jsp:include>
 
include动作的目的是把其他文件的正文插入到这个程序中来,在一定程度上重用代码,过度使
用会降低执行效率。前面已经介绍过include指令,它是在jsp文件被编译时引入文件,当被引入
文件做过修改,也不需要重新编译主jsp文件。
 
page="relativeURL | <%=expression%>"  指定需要包含文件的相对路径,可以使用jsp表达式
动态生成需要包含文件的相对路径。
 
flush="true"  此属性的值必须为true。
 
<jsp:param name="parameterName" value="parameterValue | <%=expression%>"/>  能为
引入文件传递参数。
 
实例:
 
主页面
<%@ page language="java"
   contentType
="text/html;charset=gb2312"
%>
<html> 
<head> 
<title>Include Actions</title>
</head>
<body>
<H1>带参数引入页面</H1>
<jsp:include page="11.jsp" flush="true">
 
<jsp:param name="gr1" value="aaabbb"/>
 
<jsp:param name="gr2" value="rossini"/>
</jsp:include>
</body>
</html> 
引入页面
<%
String gr1=request.getParameter("gr1");
String gr2=request.getParameter("gr2");
out.print(gr1);
out.print(gr2);
%>
8、JSP操作指令FORWARD:
 
<jsp:forward page="relativeURL | <%=expression%>"/> 或者
 
<jsp:forward page="relativeURL | <%=expression%>">
        <jsp:param name="parameterName" value="parameterValue | <%=expression%>"/>
</jsp:forward>
 
改动作把请求转到另外的页面,</jsp:forward>后面的代码将不能执行,因为已经转到其他
页面上去了,所以后面的代码不会被执行的。
 
实例:
 
主页面
<%@ page language="java"
   contentType
="text/html;charset=gb2312"
%>
<html> 
<head> 
<title>Forward Actions</title>
</head>
<body>
<jsp:forward page="11.jsp">
 
<jsp:param name="gr1" value="rossini"/>
 
<jsp:param name="gr2" value="126263"/>
</jsp:forward>
</body>
</html> 
转到页面
<%
String gr1=request.getParameter("gr1");
String gr2=request.getParameter("gr2");
out.print(gr1
+"#");
out.print(gr2);
%>
9、JSP操作指令PLUGIN
 
<jsp:plugin
type="bean | applet"
code="classFileName"
codebase="classFileDirectoryName"
 
name="instanceName"
archive="URIToArchive,..."
align="bottom | top | middle | left | right"
height="displayPixels"
width="displayPixels"
hspace="leftRightPixels"
vspace="bottomTopPixels"
jreversion="JREVersionNumber | 1.1"
nspluginurl="URLToPlugin"
iepluginurl="URLToPlugin" >
 
<jsp:params>
      <jsp:param name="parameterName" value="paramValue | <%=expression%>"/>
</jsp:params>
 
<jsp:fallback> text massage for user </jsp:fallback>
 
</jsp:plugin>
 
jsp:plugin动作用来根据浏览器的类型,插入通过java插件运行Applet所必须的OBJECT
或embed元素。
 
type="bean | applet"插件将执行的对象类型,用户必须在Bean和Aplet中指定一个。
code="classFileName"插件将执行的java类文件名称,在名称中必须包含扩展名。
codebase="classFileDirectoryName"插件将执行的java类文件的路径,默认与JSP文件
在同一个文件夹下
name="instanceName"Bean或Applet的实例名称,使得被同一个JSP文件调用的Bean或
Applet之间的通信成为可能
archive="URIToArchive,..."以逗号分隔的路径名列表,这些路径名用于预装一些将要使用
的class,这会提高applet的性能。
 
实例:
<%@ page 
 language
="java"
 contentType
="text/html;charset=gb2312"
%>
<html>
<title>Plugin 实例</title>
<body bgcolor="white">
<h3> 当前的时间是 : </h3>
<jsp:plugin
  
type="applet"
 code
="Clock2.class"
 codebase
="/guorui" 
 name
="time1"
 jreversion
="1.2" 
 width
="160" 
 height
="150" >
      
<jsp:fallback>
          Plugin tag OBJECT or EMBED not supported by browser.
      
</jsp:fallback>
</jsp:plugin>
</body>
</html>
10、jsp:useBean操作指令:
 
<jsp:useBean id="beanInstanceName" scope="page | request | session | application" class="package.class"/>
 
该动作用来装载一个将在JSP页面中使用的JavaBean,其含义是创建一个package.class的
实例,然后把它绑定到变量ID上,并使用scope定义Bean的作用范围。
 
获得Bean的实例之后,要修改或获取Bean的属性就变得很重要
 
<jsp:setProperty name="beanInstanceName"
       property="*"  | property="propertyName" | property="propertyName" param="paramName" |
       property="propertyName" value="string | <%=expression%>"/>
 
如果property的值是"*",表示所有名字和Bean属性名字匹配的请求中的参数都将传递给相应属性的set()
方法。
如果property的值是"propertyName",表示所有名字和Bean的propertyName属性名字匹配的请求中的参数都将传递给propertyName属性的set()方法。
如果使用property="propertyName" param="paramName"方式,则用指定的请求参数作为Bean指定属性
的值。
 
<jsp:getProperty name="beanInstanceName" property="propertyName"/>
 
这个动作提取指定bean属性的值,转换成字符串然后输出。
 
实例:
 
主页面:
 
<%@page language="java"
        contentType
="text/html;charset=gb2312"
%>
<%!private int aa=0;%>
<HTML>
<HEAD>
<TITLE> JavaBean使用实例 </TITLE>
</HEAD>
<BODY>
<jsp:useBean id="test" scope="page" class="test.testBean"/>
1输入变量值:
<jsp:setProperty name="test" property="test1"/>
<form method="get">
<input type="text" name="test1"  size="25">
<input type="submit" value="确定">
<input type="reset" value="清除">
<P>
你刚才输入的内容是:
<jsp:getProperty name="test" property="test1"/>
<P>
2输入变量值:
<jsp:setProperty name="test" property="*"/>
<form method="get">
<input type="text" name="test2"  size="25">
<input type="submit" value="确定">
<input type="reset" value="清除">
<P>
你刚才输入的内容是:
<jsp:getProperty name="test" property="test2"/>
<P>
3输入变量值:
<jsp:setProperty name="test" property="test3" param="guorui"/>
<form method="get">
<input type="text" name="guorui"  size="25">
<input type="submit" value="确定">
<input type="reset" value="清除">
<P>
你刚才输入的内容是:
<jsp:getProperty name="test" property="test3"/>
<P>
<jsp:setProperty name="test" property="count" value="<%=++aa%>"/>
你登陆的次数是:
<jsp:getProperty name="test" property="count"/>
</BODY>
</HTML>
JavaBean源码:
package test;
 
public class testBean{
 
private String test1="呵呵,还没有输入内容呢!!";
 
private String test2="嘻嘻,还没有输入内容呢!!";
 
private String test3="嘿嘿,还没有输入内容呢!!";
 
private String test4="哈哈,还没有输入内容呢!!";
 
private int count=0;
 
 
public testBean(){}
 
 
public void setCount(int s){
  
this.count=s;
 }

 
public int getCount(){
  
return this.count;
 }

 
 
public void setTest1(String s){
  
this.test1=s;
 }

 
public String getTest1(){
  
return this.test1;
 }

 
 
public void setTest2(String s){
  
this.test2=s;
 }

 
public String getTest2(){
  
return this.test2;
 }

 
 
public void setTest3(String s){
  
this.test3=s;
 }

 
public String getTest3(){
  
return this.test3;
 }

 
 
public void setTest4(String s){
  
this.test4=s;
 }

 
public String getTest4(){
  
return this.test4;
 }

}

http://guorui.blogdriver.com/guorui/30632.html
posted @ 2008-01-18 14:03  moonsnow  阅读(213)  评论(0编辑  收藏  举报