JSP自学笔记

来源:互联网 发布:淘宝卖包包的网红店 编辑:程序博客网 时间:2024/05/16 05:01
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

JSP注释
<%-- comments --%>
注意<%--   --%>之间的 --%>应写为 --%>

JSP声明
<%! 声明语句序列 %>

类和变量的说明可以在<%! %>中间声明,也可以直接声明一个方法
<%!
   public void foo(){
       out.println("the foo function");
   }
%>

JSP表达式
<%=Java表达式%>
用来直接在页面中显示该表达式的值,效果与
<%


一致

JSP程序段
<% Java代码%>
JSP声明中被声明的变量在所有运行这个JSP程序代码的线程中有效
而在JSP程序段中被声明的变量在每次新的请求线程产生的时候都会重新创建和
重新初始化


JSP编译指令
基本格式<%@directive {attr="value"}%>
现在为止只有三种JSP编译指令include,page,taglib

include 编译指令
可以将一个外部文件嵌入到当前JSP程序中,同时解析这个文件中的JSP语句
(如果有的话)
格式:<%@include file="relativeURLSpec"%>
例:<%@include file="copyright.html"%>
如果被嵌入的文件经常改变的话,建议使用<JSP:include>操作指令

page编译指令
<%@page
   [language="Java"]
   [extends="package.class"]
   [import="package.class│package.*},…"]
   [session="true│false"]
   [buffer="none│8kb│size kb"]


   [autoFlush="true│false"]
   [isThreadSafe="true│false"]
   [info="text"]
   [errorPage="relativeURL"]

[contentType="mimeType[;charset=characterSet]"│"text/html;charset=ISO8859.1"]

   [isErrorPage="true│false"]
%>
说明:
language:声明当前JSP程序所使用的教本语言的种类
extends:标明该JSP程序编译时所产生的Java类需要继承的class或者需要实现的
      interface的全名
import:用来导入包,下面几个包时默认自动导入的,不需要显式表示
        java.lang.*;javax.servlet.*;javax.servlet.JSP*;javax.servlet.http.*
例:<%@page import="java.util.Vector"%>
session: 设定这个JSP程序是否需要HTTP session
buffer: 指定输出缓冲区的大小。输出缓冲区被JSP内部对象-Out对象(out)用于缓存
       执行的JSP程序对客户浏览器的输出,缺省值式8kb,可以设置为none,也可以
       设置为其他的值,单位为kb
autoFlush:指定如果输出缓冲区溢出时是否需要强制输出缓冲区的内容。设置为true时
       为正常。设置为false时,会在buffer溢出时产生一个Exception。


例:<%@page buffer="5kb" autoFlush="false"%>
info:设置该JSP程序的信息,也可以看作时其说明,可以通过
   servletContext.getServletInfo()方法取回
errorPage:指定错误处理程序。如果本程序产生了异常或者错误,而该JSP程序又没有相
       应的处理代码。则会自动调用该指令所指定的外部处理程序
例:<%@page errorPage="../error/errorPage.JSP"%>
isErroePage:设置本JSP程序是否为错误处理程序
contentType:用于设定传回网页的文件格式和编码样式,即MIME类型和页面字符集类型
       缺省的MIME类型是text/html;缺省的字符集为ISO-8859.1
例:<%@page contentType="text/html;charset=gb2312"%>

taglib编译指令
格式:<%@taglib uri="taglibraryURI" perfix="tagPrefix"%>
这个和xml有关,先略过

JSP操作指令
不同于JSP编译指令,JSP操作指令是在客户端请求时期动态被执行的
有以下几个:
<JSP:forward>,<JSP:param>,<JSP:include>,<JSP:plugin>,<JSP:useBean>
<JSP:setProperty>,<JSP:getProperty>

1,<JSP:forward>
作用:使得当前程序重定向别的HTML文件,或者是别的动态程序
语法形式:
   <JSP:forward page="{relativeURL│<%=expression%>}"/> (JSP1.0)
或者
   <JSP:forward page="{relativeURL│<%=expression%>}"/> (JSP1.1/1.2)
       {<JSP:param.../>}
   </JSP:forward>

page="{relativeURL│<%=expression%>}"指定目标文件的URL
<JSP:param name="parameterName" value="{parameterValue│<%=expression%>}"/>
    设置参数



例子:<JSP:forward page="/servlet/sendmail?email=grin@grin.com"/>
例子:
<JSP:forward page="/servlet/sendmail>
   <JSP:param name="email" value="grin@grin.com"/>
</JSP:forward>

2,<JSP:param>
作用:指定某个参数的值,必须和<JSP:forward>,<JSP:include>,<JSP:plugin>
       等一起协同使用

3,<JSP:include>指令
作用:在JSP程序中宝含一个静态或动态文件
语法形式:
   <JSP:include page="{relativeURL│<%=expression%>}" flush="true"/>
或者
   <JSP:include page="{relativeURL│<%=expression%>}" flush="true">
       <JSP:param name="parameterName" value="patameterValue"/>
   </JSP:include>

flush="true":指定是否将被宝含的文件的执行结果输出到客户端,在JSP1.1规范中
           flush="false"是不允许的


   <JSP:include page="scripts/login.JSP"/>
例子:
   <JSP:include page="copyright.html"/>
例子:
   <JSP:include page="/index.html"/>
例子:
   <JSP:include page="scripts/login.JSP">
       <JSP:param name="username" value="grin"/>
   </JSP:include>
4,<JSP:plugin>
作用:动态的下载服务器端的JavaBean或者Java Applet程序到客户端的浏览器上执行
如果需要的话,还要下载一个Java插件
语法形式:
   <JSP:plugin
       type="bean│applet"
       code="classFileName"
       codebase="classFileDiretoryName"
       [name="instanceName"]
       [archive="URLtoArchive"]
`       [align="bottom│top│middle│left│right"]
       [heigh="displayPixels"]
       [width="displayPixels"]
       [hspace="leftRightPixels"]
       [vspace="topBottomPiexels"]
       [jreversion="JREVersionNumber│1.2"]
       [nspluginurl="URLToPlugin"]
       [iepluginurl="URLToPlugin"]>


       [<JSP:parames>
           [JSP:param name="parameterName" value="parameterValue"/>]
       </JSP:params>]
       [<JSP:fallback>text message for user that can no see the plugin
       </JSP:fallback> ]
       </JSP:plugin>
说明:
   type指定被执行的java程序的类型
   code 指定被执行的文件名,该文件必须以".class"扩展名结尾
   codebase指定被执行文件所在的目录
   name 给该程序岂一个名字用来标志该程序
   archive指向一些要预先载入的将要使用到的类的路径
   hspace,vspace:显示时,左右,上下的留白
   jreversion  能正确运行时所需要的JRE版本,缺省值是1.2
   nsplugin,ieplugin:Netscape Navigator,Internet Exploer能使用的JRE的下载地
                       址
   <JSP:fallback>指令:用于不能显示时的说明信息

例子:和女友吃饭去了回来再打
女友回家了。我理了个头发,现在帅帅的
继续写



例子
<JSP:plugin type="applet" code="MyPlugin.class" codebase="/classes">
   <JSP:params>
       <JSP:param name="myPlugin" value="Greetings"/>
   </JSP:params>
   <JSP:fallback>
       <p>unable to load plugin</p>
   </JSP:fallback>
</JSP:plugin>
例子:
<JSP:plugin
   type="applet"
   code="clock2.class"
   codebase="/examples/JSP/plugin/applet"
   jreversion="1.2"
   width="160"
   hight="150">
   <JSP:fallback>
       plugin tag DBJECT or EMBED not supported by browser
   </JSP:fallback>
</JSP:plugin>
<JSP:useBean><JSP:setProperty><JSP:getProperty>
作用:<JSP:useBean>用来在JSP页面中实例化一个或若干个Javabean
<JSP:setProperty>用于指定Javabean的某个属性的值
<JSP:getProperty>用于输出JavaBean中某个属性的值
格式:
<JSP:useBean id="name" class="classname"
   scope="page│request│session│application" beanDetails/>
</JSP:useBean>
其中id为该JavaBean的名字class为该Bean的类名scope为该实例对象的生命周期
beanDetails:=class="className"│
           class="className" type="typeName"│
           beanName="beanName" type="typeName"│
           type="typeName"

<JSP:setProperty name="BeanName" propertyDetails/>

其中
propertyDetails:=property="*"│


                 proterty="propertyName" param="parameterName"
如果property的值为"*",这说明如果表单数据的属性名和JavaBean的属性名相同
那么JavaBean的相应属性将会被自动赋值

<JSP:getProperty name="BeanName" property="propertyName"/>

JSP自己自动的定义了9个可以直接使用而不用说明的对象
他们分别是
out,applicatin,config,exception,page,pagecontext,request
response,session

简单说明:

out:
一个Javax.servlet.JSP.JSPWriter对象
主要用来向客户端输出各种格式的数据,并且管理服务器上的输出缓冲区

application
javax.servlet.ServletContext
包括在特定的应用中的被所有servlet共享的数据

config
javax.servlet.ServletConfig
JSP的配置信息



excepltion
javax.lang.Throwable
引起该错误页被调用的错误或者异常

page
java.lang.object
当前的Java Server Page。在Java中,这和this变量一样。

pageContext
javax.servlet.JSP.PageContext
包括了JSP可能需要的对大多数对象的引用,页有类似于include和forward
的功能

request
javax.servlet.http.HttpServletRequest
关于输入请求的存储信息,包括表单变量

response
javax.servlet.http.HttpservletResponse
关于响应的信息,包括报头变量,cookies和内容类型



javax.servlet.http.HttpSession
包括与一个特定浏览器session相关的数据

具体的介绍可以看看sun的说明文档中对相应的对象和类的描述

后记:
总算是把笔记本上的东西敲完了                                                    
用一个星期的时间看了一本JSP入门书                                              
不过没有时间可以写几个程序自己试试……                                          
也没有时间参考一些自己验证该书的正确性                                          
课程,考研……                                                                  
想学的东西几乎没有时间学                                                        
sigh
————————————————————————————————

转载请注明:

紫风@紫风论坛
http://www.cnpoems.net/bbs





<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击