从语法和对象比较看ASP与JSP功能的可转化性

我们将通过ASP和JSP语法和对象之间的比较来说明,从ASP到JSP之间的转换是可以实现的。

ASP与JSP的隐含对象

Application Object: 在所有用户间分享当前应用信息。

 
 
  1. < % Dim ls_write  
  2. For Each Key in Application.Contents  
  3.  ls_write = Key + ":"+Application(Key)  
  4.  Next  
  5. %> 
  6.          

Config Object: 存储servlet的配置信息,但是很少使用。

功能ASPJSP
对象名称ASP没有类似对象config
对象类型N/Ajavax.servlet.ServletConfig
Servlet名称N/AgetServletName
返回servlet初始化参数的名称N/AgetInitParameterNames()
得到初始参数的值N/AgetInitParameter(String name)

Error Object: 包含在脚本中发生的任何错误信息

功能ASPJSP
对象名称ASPErrorException
对象类型N/Ajava.lang.Throwable
特别注意在最新的ASP3.0/IIS5.0对象,可以使用Server.GetLastError方法来获得ASPError对象只有把页面定义为error页面的时候才能够得到它。可以使用如下声明:
< %@ page isErrorPage="true" %>
错误消息Description ()getMessage()
得到全部错误ASPDescription()toString()
错误跟踪N/AprintStackTrace(out)
错误位置LineColumnN/A

Out: 用来写和控制从服务器到浏览器的输出缓存

功能ASPJSP
对象名称Responseout
对象类型N/Ajavax.servlet.jsp.JspWriter
将数据写入output 缓存中Write variantprint(object or primitive data type)
写二进制数据BinaryWrite data必须通过java OutputStream类来使用这个方法。
ServletOutputStream Output = response.getOutputStream();Output.write(Btye[] buffer);
清空out缓存ClearclearBuffer()
发送当前的缓存到客户端Flushflush()
停止处理当前页面Endclose()
这与end不同,它关闭了当前的输出流,而JSP页面仍然将完成它的处理

Request Object: 从客户端(浏览器)接受信息。

功能ASPJSP
对象名称Requestrequest
对象类型N/A是javax.servlet.ServletRequest的子类
通常是javax.servlet.HttpServletRequest
Cookie细节Cookies(cookie)[(key).attribute]cookie[]=getCookies()
获取表格数据string = Form(element)[(index)]For example:mydata= Request.Form("date")string = getParameter(Name)Enum = getParameterNames()string[]= getParameterValues(name)For example:ls_form = request.getParameter("date");
获取查询数据QueryString(element)[(index)|.Count]getParameter(Name)getQueryString() (entire query string)
由客户端发送的HTTP头ServerVariables (server environment var)For example:
ServerVariables (ALL_RAW) returns to you all the headers in raw format
getHeaderNames()getHeader(name)getHeaders(name)getIntHeader(name)getDateHeader(name)

Response Object:发送信息到浏览器。ASP和JSP对待response对象有一些不同。ASP只使用Response对象来控制到浏览器的输出。JSP将这个功能分离到两个对象中。在JSP中Response是被发送到客户端的实际对象。JSP也使用out对象来实现向输出缓存写功能。

功能ASPJSP
对象名称Responseresponse
对象类型N/A是javax.servlet.ServletResponse的子类,通常用:javax.servlet.HttpServletResponse
缓存页面输出Buffer = True/FalseJSP的页面缓存通常为8K。可以设置缓存大小,下面的例子是关闭缓存 < %@ page buffer= "none" %>
启用/不启用代理服务器缓存CacheControl =Private/PublicsetHeader("Pragma","no-cache")setHeader("Cache-Control","no-cache")
添加CookieCookies(cookie)[(key).attribute] = valueaddCookie(cookie)
添加Http头AddHeader Name,ValuesetHeader(Name,Value)
使客户端连接到另外一个页面Redirect URLsendRedirect(Absolute URL)
发送错误信息到客户端N/AsendError(int code,String msg)
设置输出MIME类型ContentType = "MIME TYPE"setContentType("MIME TYPE")

Server Object: 提供到服务器端的方法和属性的连接。

功能ASPJSP
对象名称ServerJSP没有服务器对象,在ASP Server对象中的功能已经被分配到了别的页面中
对象类型N/AN/A
在服务器端创建一个对象CreateObject(Object id)N/A
对一个String进行HTML编码HTMLEncode(String)N/A
通过绝对路径寻找文件MapPath( Path )N/A
对URL进行编码URLEncode(String)N/A
设置超时ScriptTimeout = SecondsN/A

Session Object:为一个用户在多页面间共享信息。

功能ASPJSP
对象名称Sessionsession
对象类型N/Ajavax.servlet.http.HttpSession
注意ASP通过使用cookie使用sessionJSP有两种session管理方法:
1.使用cookie
2.使用URL重写
关闭session并释放其资源Abandoninvalidate()
存储一个session变量Session (String name) ="Your Data"setAttribute(String name,Object object)*
存储一个session对象Set Session (String name) = Server.CreateObject(String name)同上
获取一个session变量My_Variable = Session(String name)getAttribute (String name)*
   
获取一个session对象Set My_Object = Session(String name)同上
删除一个session的对象或变量Contents.Remove(String name)removeAttribute(String name)
收集内容ContentsgetAttributeNames()
Session IDSessionIDstring =getId()
设置超时Timeout(Minutes)setMaxInactiveInterval(int interval in seconds)
得到超时设置N/Aint =getMaxInactiveInterval()
禁用session< %@ EnableSessionState = False%>< %@ page session="false"%>

ASP与JSP的脚本

脚本声明:怎么将服务器端的脚本从客户端的脚本中分离开。

ASPJSP
< % Your Server Side Script %>< % Your Server Side Script %>

表达式:将数据直接送到输出缓存中。

ASPJSP
< %= Your_Variable %>< %= Your_Variable %>

申明:申明变量和方法,使他们可以在本页面内使用。

ASPJSP
< % Your Function %>< %!Your Function %>

标识:告诉容器怎样处理这个页面,

ASPJSP
< %@ Your Directive %>< %@ Your Directive %>
例如设置脚本语言:
< %@ LANGUAGE="VBSCRIPT" %>
再如:
< %response.buffer=true%>
标识发送信息给容器
标识不向输出缓存发送信息
当JSP页面初始化时,标识被处理
例如设置脚本语言:
< %@ page language= "java"%>
再如:
< %@ pagebuffer="64k"autoFlush= "true"%>

脚本注释:

注释类型ASPJSP
一般注释< % 'Your Comment %>Java的注释:
< %//my comment %> 或< % /* my comment */ %>
特定注释N/AJSP的注释:
< %-- your comment --%>
这类注释不在容器中处理,也不放入servlet中

ASP与JSP的引入文件

引入:在页面没有处理前,引入文件

ASPJSP
< !--#include file="Your File.asp" -->
< !--#include virtual ="/Your File.asp"-->
< %@ include file="Your File" %>
< jsp:directive.include file="Your File" %>

ASP与JSP的转文件

ASPJSP
response.redirect("to_File.asp")response.sendRedirect("to_File.asp")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值