JScript .NET, Part VI: Creating IE Web Services: Creating Web Services - Doc JavaScript | WebReference

JScript .NET, Part VI: Creating IE Web Services: Creating Web Services - Doc JavaScript


JScript .NET, Part VI: Creating IE Web Services

Creating Web Services

Creating a Web service is very simple, especially when only consumed by the Internet Explorer browser. A Web service is a class method that accomplishes the Web service task. The following example checks if a number is a prime number. It includes the class PrimeNumbers and the method IsPrime():

public class PrimeNumbers {
  public function IsPrime(n:int) : int {
    var i:int;
    if (n % 2 == 0) return (n == 2);
    if (n % 3 == 0) return (n == 3);
    if (n % 5 == 0) return (n == 5);
    for (i=7; i*i 

At the moment, this class does not represent a Web service. Here is what you need to do to convert the class to a Web service:

The directive line is the first line of the file and looks like this:

<%@ WebService Language="JScript" class="PrimeNumbers" %>

You derive the class from the WebService class by extending it. The WebService class includes a lot of methods that are needed for deploying a Web service. Basing your class on the WebService class provides you with these methods for free. You need focus only on your unique value-adding business logic. Here is how you extend the class above:

public class PrimeNumbers extends WebService {

The location of your Web service is on your Web server. If your Web server is IIS and your file is checkIsPrime.asmx for example, you can save it in a directory under c:\inetpub\wwwroot. For example:

c:\inetpub\wwwroot\Webreference\checkIsPrime.asmx

From now on, you can refer to this Web service as:

https://localhost/Webreference/checkIsPrime.asmx

Here is a complete listing of the Web service:

<%@ WebService Language="JScript" class="PrimeNumbers" %>
import System;
import System.Web;
import System.Web.Services;
public class PrimeNumbers extends WebService {
  WebMethodAttribute public function IsPrime(n:int) : int {
    var i:int;
	if (n % 2 == 0) return (n == 2);
	if (n % 3 == 0) return (n == 3);
	if (n % 5 == 0) return (n == 5);
	for (i=7; i*i 

We are not going into the algorithm of checking for prime numbers, implemented in the above Web service. There are many algorithms around, and a lot of research has already been conducted on the subject. A prime number is a number which is divisible only by itself and by 1.

Also notice that we have imported more namespaces than absolutely required (System.Web.Services). The two other namespaces above (System and System.Web) will be required in future columns.

Next: How to interact with the add Web service


Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: June 17, 2002
Revised: June 17, 2002

URL: https://www.webreference.com/js/column112/2.html