Building a web service endpoint in Java

Ok, so what is a web service?

I provide an overview of what Web Services (WS) are in this post, An introduction to Web Services. Give it a read and pop back here Smile

Seriously, Java? The most insecure software platform ever?

Yes, Java. The platform agnostic software environment that is taught in a lot of University CS courses and has had a consistently high spot in the tiobe programming language popularity chart (Currently #2, with only .2% less market than the number one, C).

Its also worth bearing in mind that the security issues that have plagued Java in recent months are usually related to the use of a web plug in. This plug in causes issues as Java is a powerful, fully featured platform which when combined with a web plugin introduces a large number of attack vectors. From a Desktop/Server application perspective Java is quite safe and in many ways safer than native code due to its Bytecode interpreting sandboxed process virtual machine.

Which Web Service technology will be used here?

In this example SOAP based web services will be used to provide a quick example. For large production systems you may prefer to look at JSON (Message format) and REST (communication strategy) based WS.  This example will be built using Netbeans.

Plug-in and communicate

What??? I’m quite I read a lot so I’m plugged in but I‘m a stereotypical Socially Awkward Penguin, I don’t like the talky talk. Not fair!!!

Not that type of plugged in and communicate, to develop WS in Java we need to install a  development plugin. To do this go to the Tools –> Plugins menu.


In the plugin interface go to Available plugins and install the Java web and EE option. After installation restart Netbeans.


Once the  WS support is enabled a hosting server is needed, this can be specified or isntaleld from the Tools-> Servers menu. Click Add Server


Next choose a server, I have chosen the Glassfish server below.


If you have the installed already you may specify the path or download the server. To download tick the agreement then select Download Now. Then go through the Installation process. Follow the screen shots below





Now that your environment is ready you can now create a project.

Time to make a project.

Create a New ‘Web Application’ Project as shown below.



Yes, I am aware I forgot the ‘l’, I have another Example Application in the same folder and am a little lazy.



Do not click any of these frameworks as they are not needed for this tutorial.


Codey Time!

Under web services click New –> Web Service and then specify customization.




You should now be presented with a source file which contains some boiler plate code. To this code a small math operation should be added. image

The code in question will go into a class called MathFun and will calculate the Pearson product-movement coefficient.  A walkthrough of calculating this is shown here. This code accepts 2 arrays containing Doubles and returns a double as a reply.


Once the code has been added to a class it can then be called by the service endpoint code by specifying a @WebMethod.

This code follows this form.

   1: private MathFun mathFun = new MathFun();


   3: @WebMethod(operationName = "CalculatePearson")

   4: public Double CalculatePearson(Double  a1[], Double  a2[]) {

   5:     return mathFun.CalcPearsonCorrelation(a1,a2);

   6: }


Once the code is complete and tested it is then possible to test the web service with a useful Open Source tool called SoapUI.


And we are done!!

Yes, it is that easy to set up a WS. The next step involves consuming it from a device/web browser. This is simple enough as most development environments support SOAP.

Manipulating / Resizing Images using Classic ASP

By Classic ASP do you mean the COBOL of early 21st Century web development that hasn’t expanded functionality in what seems like forever?

Yes that Classic ASP, the one that you may encounter when maintaining enterprise codebases.

What magic incantation do I have to use?

Classic ASP supports 3 main languages VBScript, JScript and PERLScript which although they are not known for their strong OO they can access COM/OLE/System objects and this is how we are going to manipulate images.

Standing on the shoulders of giants

There are a few components that we can leverage for this end but this tutorial will cover the legendary and reliable Image Magick suite, download the version that you desire (I used the q16 winx64 static version) , restart your server and then fire up your notepad.

The code

In the below code we will be resizing the height of an image while preserving the aspect ratio


   1:  'Declare the variables
   2:  dim srcImg, fs, imageMagick, tmpVar,targImgPath, imgPath,image_width, image_height, newHeight
   4:  'New image height
   5:  newHeight = 400             
   7:  'Create a File System Object for manipulating files
   8:  Set fso= CreateObject("Scripting.FileSystemObject")
  10:  'Declare the path of the image we wish to manipulate
  11:  targImgPath = "c:\\image.jpg"
  13:  'Get the path of the image taht we wish to manipulate by using the Server.MapPath function
  14:  imgPath = Server.MapPath(targImgPath)
  16:  'Get image dimensions using the load picture operation
  17:  'Get the image contentthat we wish to manipulate by using the fs.GetFile object
  19:  set srcImg = loadpicture(fs.GetFile(imgPath))
  21:  'Get the current image height and width        
  22:  image_width = srcImg.width
  23:  image_height = srcImg.height
  25:  'Calcualate the aspect ratio of the image
  26:  tempVar =  image_width / image_height 
  28:  'Calculate the new width of the image 
  29:  image_width = Round(newHeight * tempVar) 
  31:  'Create an Image Magick object to manipulate the image
  32:  Set imageMagick = Server.CreateObject("ImageMagickObject.MagickImage.1")
  34:  'Resize the image to the desired width and height and output the image to where desired
  35:  imageMagick.Convert Server.MapPath(imgPath, "-resize" , image_width&"x"&image_height, "C:\\resizedimage.jpg")

Simple! and only the tip of the iceberg,  Image Magick has lots of functions that you can invoke in a similar manner Smile