|
In this article, a series of annotated reference charts are provided to help programmers move efficiently between ASP and JSP development environments. Version 1.0 of this living document covers the following topics:
Version 2.0 will expand the implicit objects section to include a complete listing of functions and methods. Then reference charts for VBScript and Java will be added in version 3.0. And, finally, in version 4.0, reference charts for ADO and JDBC will complete the set. Each major revision will be announced in the JSP Buzz, so stay tuned.
I am considering producing another set of these charts containing just the JSP information. If you would find such a resource helpful, let me know at Casey@JSPInsider.com.
General Notes and Definitions:
- Version 1.0 charts use ASP 2.0/IIS 4.0 and JSP 1.1/Tomcat. Where possible, exceptions and additions for ASP 3.0/IIS 5.0 are included.
- A container is the object processing the server page. For ASP, this container is called the ASP.DLL. For JSP, the container may be one of several possibilities. I currently use Tomcat as my reference container for JSP.
- Scriptlets are free-standing, server side, code snippets placed throughout a server page.
- JSP is case sensitive, so pay close attention to capitalization.
- For implicit objects, only specific usage notes are provided. These notes are not necessarily in the documentation. To find documentation for a particular function or attribute, look up the name of that function or attribute (eg. removeAttribute(String name)) at JSP Product Information.
|
Implicit Objects
Objects which must exist and form the core of ASP / JSP |
|
Note: Due to spacing reasons I usually don't predicate functions and properties with their object. So, for example, in your code you would use application.setAttribute(String name,Object object) to set an application variable. Also for Version 1.0 of this chart I will only cover commonly used methods and properties of the implicit objects.
|
Application Object: An object to share information among all users of a currently active application. |
|
Functionality
|
ASP |
JSP |
|
Name of object
|
Application
|
application
|
|
Object type
|
N/A
|
javax.servlet.ServletContext
|
|
Storing an application variable
|
Application(String name) ="Your Data"
|
setAttribute(String name,Object object)*
|
|
Storing an application object
|
Set Application(String name) =Server.CreateObject(String name)
|
As Above
|
|
Retrieving an application variable
|
My_Variable = Application(String name)
|
getAttribute (String name)*
|
|
Retrieving an application object
|
Set My_Object =Application(String name)
|
As Above
|
|
Removing an application variable or object
|
Contents.Remove(String name)
|
removeAttribute(String name)
|
|
*
|
|
* Note: You must use one of the object containers for primitive data types, for example Integer rather than int. |
Contents collection
Note:
This is the way to track of all the items added to the application object |
Contents
Code Example:
<%
Dim ls_write
For Each Key in Application.Contents
ls_write = Key + " : "+ Application(Key)
Next
%>
|
getAttributeNames()
This returns an enumeration of string objects containing the names of attributes stored in the application object.
|
| Lock and Unlock |
Lock prevents other users from changing the application object's properties. Unlock releases the application to be free again. |
There are no directly comparable methods in JSP's application object. However, JSP has great threading support, and with proper thread control / making your page thread safe, you can achieve similar results. |
| Determining which container you are using. |
N/A
|
getServerInfo()
|
| Determining servlet API version numbers |
N/A
|
getMajorVersion()
getMinorVersion()
|
| Writing to the container's log file |
N/A
See Response.AppendToLog(string)
|
log(String msg)
|
| Determining a file's MIME type |
N/A
|
String= getMimeType(String file)
|
| Finding a file's real path |
N/A
See ASP Server.MapPath(Path)
|
String =getRealPath(String virtualpath)
|
| Finding a URL to a resource/file |
N/A
|
URL = getResource(String path)
|
| Config Object: This object stores servlet configuration data, but is rarely used. |
|
Functionality
|
ASP |
JSP |
|
Name of object
|
ASP doesn't have a similar object.
|
config
|
|
Object type
|
N/A
|
javax.servlet.ServletConfig
|
|
Name of servlet
|
N/A
|
getServletName
|
|
Reference to Servlet context
|
N/A
|
getServletContext()
|
|
Returns the names of the servlet's initialization parameters
|
N/A
|
getInitParameterNames()
|
|
Get value of initialization parameter
|
N/A
|
getInitParameter(String name)
|
|
Error Object: This object contains data about any error that has occurred in the script.
|
|
Functionality
|
ASP |
JSP |
|
Name of object
|
ASPError
|
exception
|
|
Object type
|
N/A
|
java.lang.Throwable
|
|
Special notes
|
A new ASP 3.0 / IIS 5.0 object.
You call Server.GetLastError method to receive the ASPError object.
|
You only have access to this object if you declare your JSP page as an error page. To do this you use the declaration:
<%@ page isErrorPage="true" %>
|
|
Error message
|
Description ()
|
getMessage()
|
|
Full error
|
ASPDescription()
|
toString()
|
|
Error trace
|
N/A
|
printStackTrace(out)
|
|
Error position
|
Line
Column
|
N/A
|
|
Out: An object used to write and control the output buffer from the server to the browser.
|
| Functionality |
ASP |
JSP |
|
Name of object
|
Response
|
out
|
|
Object type
|
N/A
|
javax.servlet.jsp.JspWriter
|
|
Special notes
|
ASP uses the Response Object to perform most of these functions
|
|
|
Writing data to output buffer
|
Write variant
|
print(object or primitive data type)
|
|
Writing binary data
|
BinaryWrite data
|
You need to access the Java OutputStream class and use its binary write methods.
Quick Example:
ServletOutputStream Output = response.getOutputStream();
Output.write(Btye[] buffer);
|
|
Clear out buffer
|
Clear
|
clearBuffer()
|
|
Send current buffer to client
|
Flush
|
flush()
|
|
Stop processing the current page
|
End
|
close()
This is different than end. It closes off the current output stream. The JSP page is still free to finish its processing.
|
|
Page Object: The current servlet page representation of the JSP page being executed.
|
|
Functionality
|
ASP |
JSP |
|
Name of object
|
ASP doesn't have an object like this one.
|
page
|
|
Object type
|
N/A
|
java.lang.Object
|
|
Special notes
|
|
This object is rarely directly used, since when using Java as your scripting language, you already have access to the whole page object through the "this" keyword. This object is present for giving other scripting languages access to your JSP pages methods and attributes.
|
|
PageContext Object:This object provides access to all the other implicit objects and their attributes. In the processing a single JSP page, you might call this object the glue that holds everything together. It is also the object ultimately responsible for managing scope and transferring control from one page to another page. This object will be more fully explained in Version 2.0 of this document.
|
|
Functionality
|
ASP |
JSP |
|
Name of object
|
ASP doesn't have an object like this one.
|
pageContext
|
|
Object type
|
N/A
|
javax.servlet.jsp.PageContext
|
| Request Object: An object to receive information from the client (browser). |
|
Functionality
|
ASP |
JSP |
|
Name of object
|
Request
|
request
|
|
Object type
|
N/A
|
Subclass of:
javax.servlet.ServletRequest
Usually:
javax.servlet.HttpServletRequest
|
| Certification details |
ClientCertificate(Key[Field])
|
N/A
|
| Cookie details |
Cookies(cookie)[(key).attribute]
|
cookie[]=getCookies()
|
| Getting form data |
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");
|
| Getting query data |
QueryString(element)[(index)|.Count]
|
getParameter(Name)
getQueryString() (entire query string)
|
| HTTP headers sent by the client |
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: An object to send information to the browser. ASP and JSP treat the response objects slightly differently. ASP only has the Response object for controlling the output to the client. JSP splits the functionality into two objects. The Response object in JSP is the actually object being sent back to the client. JSP also uses the out object for the functionality to write to the output buffer. Most of the calls you will use as a JSP programmer will be found in the out implicit object. |
|
Functionality
|
ASP |
JSP |
|
Name of object
|
Response
|
response
|
|
Object type
|
N/A
|
Subclass of: javax.servlet.ServletResponse
Usually:
javax.servlet.HttpServletResponse
|
| Buffer page output |
Buffer = True/False
|
By default, JSP buffers the first 8k of output. After this limit has been reached, it flushes the output. You use the page directive to change the buffer size and state. For example, to turn buffering off:
<%@ page buffer= "none" %>
|
|
Enable/Disable proxy server caching
|
CacheControl =Private/Public
|
setHeader("Pragma","no-cache")
setHeader("Cache-Control","no-cache")
|
|
Adding cookies
|
Cookies(cookie)[(key).attribute] = value
|
addCookie(cookie)
|
|
Adding an HTML header
|
AddHeader Name,Value
|
setHeader(Name,Value)
|
|
Redirect client to a new page
|
Redirect URL
|
sendRedirect(Absolute URL)
(see also encodeRedirectURL() if you are using URL rewriting as your session management.)
|
|
Send error to client
|
N/A
You can use VbScript to raise an error, but it isn't as clean.
|
sendError(int code,String msg)
|
|
Encode an URL
|
N/A
Function of the server object.
See Server.URLencode.
|
encodeURL(name)
Note, if you are using URL rewriting as your session management, then this function also appends the session ID for this purpose.
|
|
Set the output MIME type
|
ContentType = "MIME TYPE"
|
setContentType("MIME TYPE")
|
| Server Object: This object provides access to methods and properties on the server. |
|
Functionality
|
ASP |
JSP |
|
Name of object
|
Server
|
JSP doesn't have a Server object. The functions found in the ASP server object are distributed across the other JSP implicit objects.
|
|
Object type
|
N/A
|
N/A
|
|
Creating an object on the server.
|
CreateObject(Object id)
|
Use standard Java syntax to create objects.
|
|
HTML encode a string
|
HTMLEncode(String)
|
N/A
|
|
Finding a files real path
|
MapPath( Path )
|
N/A
see JSP application.getRealPath(virtualpath)
|
|
encode an URL
|
URLEncode(String)
|
N/A
see JSP response.encodeURL(name)
|
|
Forwarding control to new page
|
(IIS 5.0 / ASP 3.0) Transfer
|
N/A
see file redirection
|
|
Timeout for server side scripts
|
ScriptTimeout = Seconds
|
N/A
This is a feature that would be container specific in JSP. Currently Tomcat doesn't have a timeout feature.
|
| Session Object: An object to share information, for one user, across multiple pages, while visiting a web site. A session object is a method of retaining state for a normally stateless HTTP web site. |
|
Functionality
|
ASP |
JSP |
|
Name of object
|
Session
|
session
|
|
Object type
|
N/A
|
javax.servlet.http.HttpSession
|
|
Special notes
|
ASP manages Session by using cookies only.
|
JSP has two methods of Session management.
- Using cookies
- URL rewriting
|
|
How to close a session and release its resources
|
Abandon
|
invalidate()
|
|
Storing a session variable
|
Session (String name) ="Your Data"
|
setAttribute(String name,Object object)*
|
|
Storing a session object
|
Set Session (String name) = Server.CreateObject(String name)
|
As Above
|
|
Retrieving a session variable
|
My_Variable = Session(String name)
|
getAttribute (String name)*
|
|
Retrieving a session object
|
Set My_Object = Session(String name)
|
As Above
|
|
Removing a session variable or object
|
Contents.Remove(String name)
|
removeAttribute(String name)
|
|
*
|
|
* Note: You must use one of the object containers for primitive data types, for example Integer rather than int. |
Contents collection
|
Contents
|
getAttributeNames()
This returns an Enumeration of String objects containing the names of attributes stored in the application object.
|
|
The session ID
|
SessionID
|
string =getId()
|
|
Setting the timeout period
|
Timeout(Minutes)
|
setMaxInactiveInterval(int interval in seconds)
|
|
Getting the timeout period
|
N/A
|
int =getMaxInactiveInterval()
|
|
Getting a location identifier
|
LCID(Locale ID)
|
see Request Object getLocale
|
|
Disabling the session
|
A Directive Command
<%@ EnableSessionState = False%>
|
A Directive Command
<%@ page session="false"%>
|
| Scriptlet declaration: How the server side script is separated from client side script. |
| ASP |
JSP |
|
<% Your Server Side Script %>
|
<% Your Server Side Script %>
|
| Expression: A shortcut method to put data straight into the output buffer. |
| ASP |
JSP |
|
<%= Your_Variable %>
|
<%= Your_Variable %>
|
| Declaration: How to declare variables and functions/methods which can then be used by any scriptlet or expression on this page. |
| ASP |
JSP |
|
<% Your Function %>
|
<%! Your Function %>
|
Note: This means your variable or function is positional. That is you must place your function or variable code in the script before its first use.
Also, ASP doesn't have a similar concept of the class variable. |
- A declaration is a complete logical unit of work.
- Declarations don't produce any results into the output buffer.
- Declarations are initialized when the JSP page is initialized.
- Code within declarations is made available to other declarations, expressions and scriptlets.
- Variables created within a declaration block become instance variables. Since JSP Containers tend to only make one instance of a page to share requests against, this can mean that all current instances of the JSP page have access to the same variable. In other words, if your page is currently being accessed at the same time by 10 users, those 10 users are sharing one variable. For all practical purposes you are creating a class variable. If you want to avoid this, you can create variables within your scriptlet block. Variables declared within a scriptlet block are local to that script block. You can also set
<%@ page isThreadSafe="false" %> to get around this issue. However, using the isThreadSafe property has serious performance issues on high traffic sites. If you want to use your variable as a class variable we recommend that you declare it as such and don't count on this behavior as being the rule.
- You can also declare class variables and functions in a declaration block.
|
|
Directive: How do you tell the container how to perform special processing of the page. In effect directives provide information for the compilation/translation phase of the server page.
|
| ASP |
JSP |
|
<%@ Your Directive %>
|
<%@ Your Directive %>
|
|
Example of setting the scripting language:
<%@ LANGUAGE="VBSCRIPT" %>
Please note many of the directives found in JSP are not directives in ASP. Rather you will find what you might think of a directive is actually a property of one of the ASP objects. Setting these properties will have global effects on the page.
For example to turn page buffering on you would write the following code:
<%response.buffer=true%>
|
- Directives are messages to the JSP container.
- Directives don't produce any results into the output buffer.
- Directives are processed when the JSP page is initialized.
Example of setting the scripting language:
<%@ page language= "java"%>
Example to turn page buffering on :
<%@ page buffer="64k"
autoFlush= "true" %> |
|
Actions : Actions are XML based tags. These tags make it possible to build simple HTML like tags for web developers to use to perform complicated or repetitive tasks.
For example: Instead of using a scriptlet to build a report table, you can build special Java classes to handle building the report. You then build a simple action tag to interface with these report classes. So, all your web site designer needs is a one line call to your custom tag. A tag the user interfaces with might look like this :
<report:build SQL="select * from employee" style="simple"/>
This style of code would be easier to maintain than a complicated scriptlet.
|
| ASP |
JSP |
|
N/A
|
<jsp:Action> or <yourtag:YourAction>
|
|
N/A
|
Example to access a JavaBean
<jsp:useBean id="myBean" class="beans.htmlBean" />
Note, when you hear the term tag library, it refers to a custom built collection of actions.
|
Scripting Comments |
| Comment Type |
ASP |
JSP |
In line script comment.
Note: These comments are based upon the scripting language used. |
VbScript uses:
a single quote '
<% 'Your Comment %>
|
Java uses:
// for a single line
/* */ for multiple lines.
<%//my comment %> or
<% /* my
comment */ %>
|
| Special Comments |
N/A
|
JSP Comments
<%-- your comment --%>
These comments are only visible from the original JSP file. This form of comment is not processed by the container and is not passed onto the servlet. Also this comment type doesn't nest. |
Static Includes: How to include files before the page has been processed.
A fundamental difference exists between the way files are included in ASP and JSP. ASP doesn't directly support include files, rather including files in an ASP application is a web server process. After the web server combines all the files to be included it then sends the file off to be processed by the ASP DLL. In a JSP application the web server doesn't preprocess the JSP page but rather hands the JSP pages straight off to the JSP container. The JSP container then performs the work of including files. |
| ASP |
JSP |
|
Command Format:
<!--#include file="Your File.asp" -->
<!--#include virtual ="/Your File.asp"-->
Usage Notes:
The keyword "Virtual" indicates you are using a full virtual path from a virtual directory in your Web site. When you use the keyword "File", you are using a relative path from the directory containing the document.
|
Command Format:
You can include a file using a directive:
<%@ include file="Your File" %>
or in tag format
<jsp:directive.include file="Your File" %>
|
- You can include HTML or additional ASP scripting elements.
- The files are combined into a single source code and then translated into a result.
- You cannot use scripting conditional logic to make the include statements conditional, since the include directive is preprocessed before scripting code executes to process the conditional.
- The files are combined into a single source code and then ASP logic is performed.
|
The file can be passed as either a relative path or an absolute path.
You can include HTML or additional JSP scripting elements.
You cannot use scripting conditional logic to make the include statements conditional, since the include directive is preprocessed before scripting code executes to process the conditional.
The files are combined into a single source code and then translated into a servlet.
The JSP container will only recombine the include files and the main page when the main page has been modified. So if you change an include file but not the page that references it, no new servlet will be generated.
The included page shares all local information and shares the same pageContext Object.
Faster than using the jsp:include action.This is due to the static include being preprocessed during page translation and compiled into a servlet.
|
| Dynamic Includes: How to include files while the page is being processed. |
| ASP |
JSP |
|
Command Format:
IIS 5.0 / ASP 3.0 Introduces Server.Execute.
Usage Notes:
This function will let you execute an ASP page from within another ASP page
|
Command Format:
<jsp:include page="Your File" flush="true">
If you desire to pass additional information to the included page use the following format:
<jsp:include page="Your File" flush="true">
<jsp: param name = "Name" value ="Data" />
</ jsp:include>
Usage Notes:
You can pass as many parameters as you need using this method.
The flush property is used to tell the page to flush the current output buffer of the current page before the action of the including the new file. Currently you can only set this property to true. Since the previous page's buffer has been flushed and you cannot forward to another page.
|
| |
You may include a text file(HTML), a CGI script, a servlet or another JSP page. You can include dynamic real time content.
The included page receives a new pageContext object.
The include page shares the request and session objects as the original page.
You can dynamically pass additional information to the include page by using parameters.
The processing of the included file happens during the request of the current page this means:
a) you can dynamically change what your include processes.
b) your latest include file will always be processed.
c) you can not only include contents of a file but you can also include dynamic output.
Restricts one from setting response headers and the response code.
|
| Browser Redirection: |
| ASP |
JSP |
|
response.redirect("to_File.asp")
|
response.sendRedirect(ls_Absolute_Url)
|
|
The file may be passed as either a relative path or an absolute path.
Once the header has been sent down to the user, you cannot redirect to a different page.
This command has the server send a HTTP response to the browser asking it to load a new URL
|
- This only works with an absolute URL. (Major Bummer).
- You can only redirect to a new page provided no output of the original page has been sent to the browser.
- I have read that sendRedirect actually, creates a new thread for the new pages redirection, while the old thread and page keeps executing until it finishes at end of the original page. I haven't seen this behavior yet and don't know if it is part of the JSP specs or a by product of a JSP containers implementation.
|
| Server Redirection: |
| ASP |
JSP |
|
In IIS 5.0 / ASP 3.0
You can use Server.Transfer.
Usage Notes:
Server.Transfer ends execution of the current asp page without clearing the output. The new page has access to same set objects (Application, Request, Response, Server, Session) as the starting file. To the browser it will appear you have the originally requested page not the page to which you are transferred.
|
Using an action:
<jsp:forward page="home/Default.jsp" / >
OR with parameters
<jsp:forward page="home/Default.jsp" >
<jsp:param name="source" value="entry"/>
</jsp:forward>
Usage Notes:
You may pass as many parameters as you need using this method by using the param tag.
The forward action ends execution of the current JSP page and removes any existing buffered output. The new page has access to following objects (Application, Request, Session) as the starting file. A new pageContext object is generated for the page. To the browser it will appear you have the originally requested page not the page to which you are transferred.
|
| |
- You can forward to a text file(HTML), a CGI script, a servlet or another JSP page.
- You can only forward to a new page provided no output of the original page has been sent to the browser.
|
Browse all of the JSP Insider source-code.
Questions or comments? Contact support@jspinsider.com.
Copyright © 2002 Amberjack Software LLC.
|