Wednesday, 12 June 2013



Direct Web Remoting

DWR is a Java library that enables Java on the server and JavaScript in a browser to interact and call each other as simply as possible.
DWR is Easy Ajax for Java
Example Of DWR Use
DWR Use 
created by Arvind Jaiswal 
phone no : 9910870895/09811924369 
Personal ID: Arvind.jaiswal786@gmail.com 

Step(1) 

SRS: dwr.jar 
 Dwr.xml 
 Engine.js 
 Util.js 
Step(2) 
 Make entry in web.xml for dwr 
 <servlet> 
 <servlet-name>dwr-invoker</servlet-name> 
 <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class> 
 <init-param> 
 <param-name>debug</param-name> 
 <param-value>false</param-value> 
 </init-param> 
 <load-on-startup>1</load-on-startup> 
 </servlet> 
 <servlet-mapping> 
 <servlet-name>dwr-invoker</servlet-name> 
 <url-pattern>/dwr/*</url-pattern> 
 </servlet-mapping> 
Step(3) 
In dwr.xml make entry of your java class and make instance 
Eg. 
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" 
"http://getahead.org/dwr//dwr20.dtd"> 
<dwr> 
 <allow> 
 <create creator="new" javascript="Msg_DWR">  <param name="class" value="com.niit.cliks.df.web.Msg_DWR"/> 
 </create> 
 </allow> 
</dwr> 


Step(4) 

 Make entry of your js in your jsp where you are using dwr ajax call 
There are following three entries: 
<script src='WEB-INF/../../dwr/interface/Msg_DWR.js'></script> 
<script src='WEB-INF/../../dwr/engine.js'></script> 
<script src='WEB-INF/../../dwr/util.js'></script> 

Call javascript function on your component (like button,link etc.) 

Eg. 
<span onclick="msgbodyAjax()"></span> 

--------- 


<script language="JavaScript"> 

function msgbodyAjax() 


Msg_DWR.testMsg({ 

 errorHandler:function(errorString, exception) 
 { 
 alert("error"); 
 }, 
 callback:function(obj) 
 { 
 alert(obj); 
 } 
 }); 




Step(5) 

Create your java class as you given entry in dwr.xml 

Msg_DWR.java 

Your js entry 
Call method of java class package com.niit.cliks.df.web;
public class Msg_DWR {

 public String testMsg() { 

 return "Hi Dwr Demo "; 
 } 

Tuesday, 14 May 2013

Web Services
Creating Web Services has two parts. First is creating Web Service and second is creating WebService Client. 

Creating WebService in eclipse: 

1.) First create a new dynamic web project say Project1. 
2.) Now create a package say pkg in src folder and create a class say Addition.java. This class will be having the methods which the user wants to access remotely. This class is also known as Endpoint. 
3.) Now define a method in this class, say a method named addNumbers(int x,int y) which simply returns the addition of these two parameters. 
4.) After creation of the class, right click on the class, go to web services, click on the create web service. Now we will be creating a WebService i.e. our first step. 
5.) Now a window will appear, now in the first column named Web service type, select Bottom up java bean Services. In the second column named Service implementation, give the package name and your Endpoint class name i.e. pkg.Addition. 
6.) On the below of these two columns, select start service from the chooser bar and select No client from the bar below this. Give java Proxy in the client type. 
7.) Now click Finish button. Now it will first start the jboss if not started. 
8.) After the end of this process, it will create a wsdl folder inside the WebContent of the same project. Inside the wsdl folder, there is an xml file. Which is named like Endpoint.wsdl i.e. Addition.wsdl. 
9.) Now first of all you should be able to run this xml file through jboss inside eclipse as well as outside the eclipse(i.e. on the explorer). To run it through eclipse, simply right click on the xml file and select option run on server. It will run the xml file in your browser inside eclipse. Now you can use the same url to run this xml on the explorer outside eclipse. 
10.) Remember one thing, jboss cannot run its two instances i.e. in eclipse as well as outside(through Run.bat) simultaneously. So you have to stop the jboss from inside the eclipse to run it outside. Otherwise it will through exception that port is already in use. 
11.) If you want to run jboss from remote address, then start jboss through mybat.bat created by our self instead of Run.bat. 

Creating WebService Client: 
Now we can create a WebService client to call the Endpoint class generated. 
1.) Create another dynamic java project say Project1Client. It will act as a client for our Project1 project to receive methods. 
2.) Now select the project, go to file, new and select others. Now select Webservice Client inside WebServices. 
3.) Now when you select this option, a window will appear. Now in the service definition give the url of the wsdl xml i.e. address of Addition.wsdl which you have run last time. Url will look like http://localhost:8080/Project1/wsdl/Addition.wsdl
4.) Note that if you want to access this web service on remote site, then give here proper address of the server system(ip of your system here) i.e.http://192.168.0.62:8080/Project1/wsdl/Addition.wsdl for example. but before giving this proper ip address, make sure that your Jboss in running from outside the eclipse and you are able to run this url from your or from remote system. If the Jboss is not running from outside, it will show error that “service definition selected is invalid”. If you are going for your localhost, then no need to run jboss from outside. Then it will also work if your jboss is started inside the eclipse. 
5.) Now from below bar, select the “start client” option. 
6.) Now click next button. 
7.) Now if you have given the remote ip address in the service definition, then after the next button click, you have to stop the jboss from outside. Now click on next button and then select start server. It will start jboss inside the eclipse. 
8.) Stopping jboss outside in necessary after first step, because if you don’t stop it, you will not be able to run jboss inside your eclipse. 
9.) Now after everything completes without any exception, you will see a package created in your src in your this project(Project1Client). i.e package name pkg, the same package name you have given in the Project1. 
10.) There would be some classes in this package i.e. one is having same name as your endpoint i.e. Addition.java, AdditionProxy.java, AdditionService.java, AdditionServiceLocator.java, AdditionSoapBindingStub.java. These are the classes which are automatically created in yourWebService client project. 
11.) Now the AdditionProxy.java is the class which you will be using to access your methods remotely. 
12.) Now make a new class in the same package in your client project say Myclass.java. 
13.) Now in this class you can call the object of proxy class i.e. AdditionProxy.java. 
Say for example: AdditionProxy object =new AdditionProxy(); 
Int result=object.addNumbers(2,3); 
System.out.println(“Addition of two numbers:”+result); 
14.) When you will run this class with code something like this, it will show you the output as the addition of two numbers 2 and 3. 
15.) If you are running this class on the same system, then make sure that jboss is running inside your eclipse. 
16.) If you want to see the result on remote system, then simply copy whole package i.e. pkg and copy it on some remote system in any project. Also add the API’s to project if required. 
17.) Now before running it on remote system, you need to do one change in the AdditionServiceLocator.java. there is mentioned the address of your server system in the variable Addition_address variable. By default, there is given a local system address. Change it and give the ip address of your server system instead of localsystem. 
18.) Now you can run Myclass.java to see the output, but before running make sure the your jboss in running (outside eclipse through the mybat.bat file) on the server system. 
19.) Now when you will run this class, you can see this output : 
Addition of two numbers:5