本文标签:ASP与JSP JSP功能
我们将通过ASP和JSP语法和对象之间的比较来说明,从ASP到JSP之间的转换是可以实现的 。
ASP与JSP的隐含对象
Application Object: 在所有用户间分享当前应用信息 。 - < % Dim ls_write
- For Each Key in Application.Contents
- ls_write = Key + ":"+Application(Key)
Next %>
Config Object: 存储servlet的配置信息,但是很少使用 。
功能 |
ASP |
JSP |
对象名称 |
ASP没有类似对象 |
config |
对象类型 |
N/A |
javax.servlet.ServletConfig |
Servlet名称 |
N/A |
getServletName |
返回servlet初始化参数的名称 |
N/A |
getInitParameterNames() |
得到初始参数的值 |
N/A |
getInitParameter(String name) |
Error Object: 包含在脚本中发生的任何错误信息
功能 |
ASP |
JSP |
对象名称 |
ASPError |
Exception |
对象类型 |
N/A |
java.lang.Throwable |
特别注意 |
在最新的ASP3.0/IIS5.0对象,可以使用Server.GetLastError方法来获得ASPError对象 |
只有把页面定义为error页面的时候才能够得到它 。可以使用如下声明: < %@ page isErrorPage="true" %> |
错误消息 |
Description () |
getMessage() |
得到全部错误 |
ASPDescription() |
toString() |
错误跟踪 |
N/A |
printStackTrace(out) |
错误位置 |
LineColumn |
N/A |
Out: 用来写和控制从服务器到浏览器的输出缓存
功能 |
ASP |
JSP |
对象名称 |
Response |
out |
对象类型 |
N/A |
javax.servlet.jsp.JspWriter |
将数据写入output 缓存中 |
Write variant |
print(object or primitive data type) |
写二进制数据 |
BinaryWrite data |
必须通过java OutputStream类来使用这个方法 。 ServletOutputStream Output = response.getOutputStream();Output.write(Btye[] buffer); |
清空out缓存 |
Clear |
clearBuffer() |
发送当前的缓存到客户端 |
Flush |
flush() |
停止处理当前页面 |
End |
close() 这与end不同,它关闭了当前的输出流,而JSP页面仍然将完成它的处理 |
Request Object: 从客户端(浏览器)接受信息 。
功能 |
ASP |
JSP |
对象名称 |
Request |
request |
对象类型 |
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对象来实现向输出缓存写功能 。
功能 |
ASP |
JSP |
对象名称 |
Response |
response |
对象类型 |
N/A |
是javax.servlet.ServletResponse的子类,通常用:javax.servlet.HttpServletResponse |
缓存页面输出 |
Buffer = True/False |
JSP的页面缓存通常为8K 。可以设置缓存大小,下面的例子是关闭缓存 < %@ page buffer= "none" %> |
启用/不启用代理服务器缓存 |
CacheControl =Private/Public |
setHeader("Pragma","no-cache")setHeader("Cache-Control","no-cache") |
添加Cookie |
Cookies(cookie)[(key).attribute] = value |
addCookie(cookie) |
添加Http头 |
AddHeader Name,Value |
setHeader(Name,Value) |
使客户端连接到另外一个页面 |
Redirect URL |
sendRedirect(Absolute URL) |
发送错误信息到客户端 |
N/A |
sendError(int code,String msg) |
设置输出MIME类型 |
ContentType = "MIME TYPE" |
setContentType("MIME TYPE") |
Server Object: 提供到服务器端的方法和属性的连接 。
功能 |
ASP |
JSP |
对象名称 |
Server |
JSP没有服务器对象,在ASP Server对象中的功能已经被分配到了别的页面中 |
对象类型 |
N/A |
N/A |
在服务器端创建一个对象 |
CreateObject(Object id) |
N/A |
对一个String进行HTML编码 |
HTMLEncode(String) |
N/A |
通过绝对路径寻找文件 |
MapPath( Path ) |
N/A |
对URL进行编码 |
URLEncode(String) |
N/A |
设置超时 |
ScriptTimeout = Seconds |
N/A |
Session Object:为一个用户在多页面间共享信息 。
功能 |
ASP |
JSP |
对象名称 |
Session |
session |
对象类型 |
N/A |
javax.servlet.http.HttpSession |
注意 |
ASP通过使用cookie使用session |
JSP有两种session管理方法: 1.使用cookie 2.使用URL重写 |
关闭session并释放其资源 |
Abandon |
invalidate() |
存储一个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) |
收集内容 |
Contents |
getAttributeNames() |
Session ID |
SessionID |
string =getId() |
设置超时 |
Timeout(Minutes) |
setMaxInactiveInterval(int interval in seconds) |
得到超时设置 |
N/A |
int =getMaxInactiveInterval() |
禁用session |
< %@ EnableSessionState = False%> |
< %@ page session="false"%> |
ASP与JSP的脚本
脚本声明:怎么将服务器端的脚本从客户端的脚本中分离开 。
ASP |
JSP |
< % Your Server Side Script %> |
< % Your Server Side Script %> |
表达式:将数据直接送到输出缓存中 。
ASP |
JSP |
< %= Your_Variable %> |
< %= Your_Variable %> |
申明:申明变量和方法,使他们可以在本页面内使用 。
ASP |
JSP |
< % Your Function %> |
< %!Your Function %> |
标识:告诉容器怎样处理这个页面,
ASP |
JSP |
< %@ Your Directive %> |
< %@ Your Directive %> |
例如设置脚本语言: < %@ LANGUAGE="VBSCRIPT" %> 再如: < %response.buffer=true%> |
标识发送信息给容器 标识不向输出缓存发送信息 当JSP页面初始化时,标识被处理 例如设置脚本语言: < %@ page language= "java"%> 再如: < %@ pagebuffer="64k"autoFlush= "true"%> |
脚本注释:
注释类型 |
ASP |
JSP |
一般注释 |
< % Your Comment %> |
Java的注释: < %//my comment %> 或< % /* my comment */ %> |
特定注释 |
N/A |
JSP的注释: < %-- your comment --%> 这类注释不在容器中处理,也不放入servlet中 |
ASP与JSP的引入文件
引入:在页面没有处理前,引入文件
ASP |
JSP |
< !--#include file="Your File.asp" --> < !--#include virtual ="/Your File.asp"--> |
< %@ include file="Your File" %> < jsp:directive.include file="Your File" %> |
ASP与JSP的转文件
ASP |
JSP |
response.redirect("to_File.asp") |
response.sendRedirect("to_File.asp") |
|