spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column112


JScript .NET, Part VI: Creating IE Web Services

Developer News
Mandrake Linux Founder Back, Virtually
Amazon: We're a Technology Company
Sun Expands MySQL With Closed Source

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 <= n; i+=2) {if (n % i == 0) return 0};
    return 1;
  }
}

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:

  • Import the System.Web.Service namespace
  • Add a directive line, specifying the class's language and namespace
  • Derive the class from the .NET Class Library's WebService class
  • Add the attribute WebMethodAttribute to each method you want to expose as a Web service method
  • Put your code in a file with the extension .asmx and save in your Web server area.

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:

http://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 <= n; i+=2) {if (n % i == 0) return 0};
	return 1;
  }
}

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

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Microsoft PDF: Top 10 Reasons to Move to Server Virtualization with Hyper-V
Microsoft PDF: Six Reasons Why Microsoft's Hyper-V Will Overtake Vmware
Microsoft Step-by-Step Guide: Hyper-V and Failover Clustering
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Microsoft PDF: Top 11 Reasons to Upgrade to Windows Server 2008
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
  PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
HP eBook: Guide to Storage Networking
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Crucial Triples Up With New Three-Channel DDR3 Kits · Meet the Finalists: Excellence in Technology Awards · Tealeaf Offers Insight to Mobile Customer Behavior


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

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