Sunday, September 30, 2012


Invoke a java webservice from browser using javascript ( xmlhttp)

Have you felt tired of seeing pages of matter on invoking a webservice from browser without much understanding . Let us try our way.

The tutorial will have two sections :
1. A simple java method exposed as a webservice ( using java embedded webserver. No need of axis, tomcat etc.,)
2. A simple html page which will invoke the webservice ( no need of php , jsp etc., ) with a simple xmlhttp request / reposnse.

Two steps for Creating and Publishing Webservice using jax-ws:


1) create a java class and add methods which will be exposed as webservices. To make normal java methods as webservices we have to annotate the methods using@WebMethod annotaiion. Java class must be annotated with @WebServiceannotation. To Customize the parameter names in the request use @WebParamannotation
Example: This example exposes the webservices to add the two numbers and multiply the two numbers

package com.sree.myservice;

import javax.jws.WebMethod;

import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;

@WebService

public class ArthimaticOperations {

@WebMethod

public int addNumbers(@WebParam(name="first") int first, @WebParam(name="second") int second){

return first + second;

}

@WebMethod
public int mutiplyNumbers(@WebParam(name="first") int first, @WebParam(name="second") int second){

return first * second;

}

public static void main(String[] args) {

ArthimaticOperations aop = new ArthimaticOperations();
String url = "http://localhost:9090/arthimatic"; // end point of webservice.
System.out.println(url+"?wsdl");
Endpoint.publish(url, aop); // publishing the webservice

}


}


2) Run the below command from source location
apt -d . com\sree\myservice\ArthimaticOperations.java
This command will generate the required wrapper classes one correspoding to request and other corresponding to response.

Now your webservice is ready to use. Just run the class com.sree.myservice.ArthimaticOperations
you can view the wsdl of the service using url http://localhost:9090/arthimatic?wsdl
Hope you are getting it running.

Calling webservice from browser using XMLHttpRequest
The webserive exposed can be accessed using soap request sending from browser

<html>
<head>
<script language="javascript">
function callMyService()
{
var req = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://myservice.sree.com/\"><soapenv:Body><web:addNumbers><first>" + document.getElementById("firstnum").value + "</first><second>" + document.getElementById("secondnum").value + "</second></web:addNumbers></soapenv:Body></soapenv:Envelope>";
if(window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var response = xmlhttp.responseXML;
document.getElementById("resultDIV").innerHTML = response.selectSingleNode(".//return").text;
}
}
var soapaction = "http://myservice.sree.com/addNumbers";
xmlhttp.open("POST","http://localhost:9090/arthimatic?wsdl",true);
xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlhttp.setRequestHeader("SOAPAction", soapaction);
xmlhttp.send(req);
}
</script>
</head>
<body>
<div><label>Enter First Number:</label><input type="text" id="firstnum"></input></div>
<div><label>Enter Second Number:</label><input type="text" id="secondnum"></input></div>
<div><button onclick="callMyService()">Add Numbers</button></div>
<div id="resultDIV"></div>
</body>
</html>



Donot forget to give your comments /suggestions and refer to your friends.

Friday, September 21, 2012

IIS 5.1 Configuration Using VBScript with Command Line


IIS 5.1 Configuration Guide
Q). How to Creating Virtual Directory Using VBScript with Command line?
A). You can use mkwebdir.vbs at Inetpub\AdminScripts directory.

 mkwebdir [--computer|-c COMPUTER1, COMPUTER2, COMPUTER3]
         <--website|-w WEBSITE>
         <--virtualdir|-v NAME1,PATH1,NAME2,PATH2,...>
         [--help|-?]
cscript mkwebdir.vbs -c localhost -w “Default Web Site” –v “Virtual Dir1,c:\inetpub\wwwroot\dir1,Virtual Dir2,c:\inetpub\wwwroot\dir2”  (OR)
cscript mkwebdir.vbs -c localhost -w 1  –v “Virtual Dir1,c:\inetpub\wwwroot\dir1,Virtual Dir2,c:\inetpub\wwwroot\dir2”  (OR)
You can use adsutil.vbs at Inetpub\AdminScripts directory.
adsutil.vbs create  w3svc/1/root/vdName "IISWebVirtualDir"
adsutil.vbs  set  w3svc/1/root/vdName/path "C:\MyFolder"
The first command create a virtual directory under w3svc/1/root. The w3svc/1/root is a IIS metabase path. 1 here is the default website's identifier. The second command specify the virtual directory's physical path.

NOTE: w3svc/1/ means “Default Web Site”.
Q). How to Delete Virtual Directory Using VBScript with Command line?
A). You can use adsutil.vbs at Inetpub\AdminScripts directory.
cscript adsutil.vbs DELETE W3SVC\1\ROOT\YourVDirName
Q). How to Change Access to Virtual Directory Using VBScript with Command line?
A). You can use chaccess.vbs at Inetpub\AdminScripts directory.
chaccess <--ADSPath|-a relative config store path>
                          [--computer|-c COMPUTER1[,COMPUTER2...]]
                          [+read|-read]
                          [+write|-write]
                          [+script|-script]
                          [+execute|-execute]
                          [+browse|-browse]
                        [--verbose|-v]
                           [--help|-?]
cscript chaccess.vbs -a w3svc/1/ROOT/<Name Of Virtual Directory> +script +execute +read
cscript chaccess.vbs -a w3svc/1/ROOT/<Name Of Virtual Directory> -script -execute -read
Q). How to Convert Virtual Directory To ASP.NET Application Using VBScript with Command line?
A). You can use adsutil.vbs at Inetpub\AdminScripts directory.
cscript adsutil.vbs APPCREATEPOOLPROC w3svc/1/root/VDirName.
Q). What is the difference between wscript and cscript?
A). wscript is used while executing vbscripts through command line get the result in window based.
Cscript is used while executing vbscripts through command line get the result in console.
NOTE: More details to see vbscripts at inetpub\adminscripts.
Q). Why below error came? And what should I do?
ERROR:
Server Error in '/Events' Application.
--------------------------------------------------------------------------------
Failed to access IIS metabase.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.Web.Hosting.HostingEnvironmentException: Failed to
access IIS metabase.

The process account used to run ASP.NET must have read access to the IIS
metabase (e.g. IIS://servername/W3SVC). For information on modifying
metabase permissions, please see http://support.microsoft.com/?kbid=267904.

Source Error:

An unhandled exception was generated during the execution of the current web
request. Information regarding the origin and location of the exception can
be identified using the exception stack trace below.

Stack Trace:

[HostingEnvironmentException: Failed to access IIS metabase.]
System.Web.Configuration.MetabaseServerConfig.MapP athCaching(String
siteID, VirtualPath path) +3591442
System.Web.Configuration.MetabaseServerConfig.Syst em.Web.Configuration.IConfigMapPath2.MapPath(Strin g
siteID, VirtualPath vpath) +9
System.Web.Hosting.HostingEnvironment.MapPathActua l(VirtualPath
virtualPath, Boolean permitNull) +169
System.Web.CachedPathData.GetConfigPathData(String configPath) +389
System.Web.CachedPathData.GetConfigPathData(String configPath) +243
System.Web.CachedPathData.GetApplicationPathData() +68
System.Web.CachedPathData.GetVirtualPathData(Virtu alPath virtualPath,
Boolean permitPathsOutsideApp) +3536567
System.Web.Configuration.RuntimeConfig.GetLKGRunti meConfig(VirtualPath
path) +189
A).
  1. Go to start àRun àtype cmd press Enter
  2. Change the directory to C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 (Note: Your application web.config existing version folder i.e 1.0, 2.0, 3.0, 3.5, 4.0.)
  3. iisreset /stop
  4. aspnet_regiis –i
  5. aspnet_regiis –c
  6. aspnet_regiis -ga ASPNET (Note:If you're using XP, the account is "ASPNET".
If you're using Server 2003, the account is "Network Service".
If you're using Vista, the account is "Network Service".)

Donot forget to give your comments /suggestions and refer to your friends.