Java is the typical programming language for Java Server Pages (JSPs). For Active Server Pages (ASP), the typical language is VBScript. This document reviews the differences between the two languages and technologies for common constructs.
Here are some useful references on ASP:
JSP and ASP often use the same tags:
Code | <% code in java or VBScript %>
|
Expression | <%= expression %>
|
However, VBScript uses a single quote to start a comment while Java uses two slashes to start a comment on the same line.
Description | JSP | ASP |
---|---|---|
Writing HTML code | out.print("Some HTML code"); |
Response.Write "Some HTML code" |
Obtaining a parameter value | request.getParameter("parameterName") |
Request("parameterName") |
Null value | null |
Nothing |
Also, Request.QueryString("currency").count
returns how many parameter values the "currency" attribute has.
This is useful for determining if the "currency" attribute has a
value by testing whether the count has zero. The
QueryString
method only works for the GET method.
For POST, use the Form
method.
Java is a strongly-typed language and its variables must have their types declared before they are used. For VBScript, declaring variables is strongly advised but not necessary. The types are not specified for the declaration.
int num;
double amount;
int list[] = new int[10]; // holds 10 integers
Dim num, amount;
Dim list(10) 'stores 10 items of any type
Note: If the ASP page has the statement Option
Explicit
near the top of the page, all variables must be
declared.
num = 10;
if (num == 10) {
out.print("Num: " + num);
}
else {
out.print("Answer is not 10");
}
// list is an array of strings
for (int i = 0; i < list.length; i++) {
out.print(list[i]);
}
Set num = 10
If num = 10 Then
Response.Write "Num: " & num
Else
Response.Write "Answer is not 10"
End If
'list is an array of strings
For Each name in list
Response.write name
Next
Normally VBScript statements end at the end of the line (semicolons are not used). If the programmer wants a statement to continue on the next line, a "_" is added at the end of the line to show that the statement continues on the next line.
Statement keywords are capitalized in VBScript.
See DevGuru for list of VBScript statements.
out.print("output");
obj.close();
Response.Write "output"
obj.Close
Note that ASP method calls when used as statements do not use parentheses for their arguments (unless the keyword "Call" precedes the object and method names). Parentheses are used when the method call is used as an expression.
Java throws exceptions when it tries to process invalid methods or data. In contrast, for VBScript, the programmer must explicitly check for invalid data or an invalid result. For example, to check whether a parameter is a valid number, the programmer could create the following if statement:
Dim response
response = Request("amount")
If Not IsNumeric(response) Then
Response.Write "This amount is not valid!"
End If
ASP pages usually use the ADO libraries to access a database. See ShowCurrencies.asp for an example of connecting to a database, issuing a query and displaying the reuslts. DevGuru provides a good reference to the ADO objects and methods.