How to write a WebService and call it in Java?

Hi, I am new to WebServiece. Can some one guide me how to write a simple webservice and where to deploy it and how to deploy it?

I prefer JAVA.. I have hands on in ApacheTomcat and JBOSS. (JSPs)

I simply need to execute a function in server like "Progress.aspx/GetProgress" as in below example.

I've gained interest from the below post: ( I dont need the same as below :) )

http://www.experts-exchange.com/Programming/Languages/Scripting/AJAX/Q_25990114.html?cid=238
LVL 15
StealthyDevAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rajkumar_pbCommented:
You can have a look at this tutorial: Its really a straightforward tutorial.

http://www.experts-exchange.com/articles/Web_Development/Web_Frameworks/Spring-Jersey-Integration.html

The only thing you need to change is use JSP/Servlet instead of Spring framework.
0
mukundha_expertCommented:
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
StealthyDevAuthor Commented:
Hi rajkumar and mukundha..

First of all, thanks for your valuable comments.

As I already said, I have knowledge in AJAX and JSPs... I just want to know how to use WebService and how to create one.

Both of your comments says different method of implementing WebService. But all I wanted to know is using JBoss or ApacheTomcat how to run a WebService (The simplest method).
(Step-By-Step in creating and deploying it.)

Say, for example, I may have a DB connection.
#1 Function [WebService] will return a value from a table
#2 Function [WebService] will update another row in a table.

Thanks in advance.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

mukundha_expertCommented:
well, i would prefer JBoss - since it comes with JAX-ws support.

to create a webservice for eg, that returns some value from a table,

write a POJO which does your task.

public class Myservice{
 public String mymethod(){
  return "" ;//some data from table.
}
}

if you annotate this class with @Webservice - this is the simplest way to create a webservice.

when you deploy this app - probably a web application - to your jboss - this class will be deployed as a Webservice.

to consume the webservice,
get the WSDL, generate Java Client Proxies - all IDE's support wsdl - proxy generation.
or use WSDl2Java

u can use the client proxies to invoke a remote webservice.

the samples are available in the link i gave you

0
rajkumar_pbCommented:
That might not be an issue as far as you understand the WebService concept. Anyway i'll let you know what are all the steps you need to do.

Have a look at the code snippet

End-Point Interface

package org.ws

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public interface Greeting {
	 @WebMethod String sayHello(String name);
}

End Point Implementation

package org.ws;

import javax.jws.WebService;

@WebService(endpointInterface = "org.ws.Greeting")
public class GreetingImpl implements Greeting {

	@Override
	public String sayHello(String name) {
		return "Hello, Welcom to jax-ws " + name;
	}

     //Just create a method that do some DB processing as u need
}

A Endpoint Publisher

package org;

import javax.xml.ws.Endpoint;

import org.ws.GreetingImpl;

public class WSPublisher {
	public static void main(String[] args) {
		Endpoint.publish("http://localhost:8080/WS/Greeting",new GreetingImpl());
	}
}

Client
 
package org;

import org.Greeting;
import org.GreetingImplService;

public class Client {
	public static void main(String[] args){
	
GreetingImplService service = new GreetingImplService();
Greeting greeting = service.getGreetingImplPort(); 
System.out.println("------->>  Call Started");
System.out.println(greeting.sayHello("Ali"));
System.out.println("------->>  Call Ended");
	}
}

Run the Client class to see the WebService in action.

Open in new window

0
rajkumar_pbCommented:
For JAX-WS

1. Add the WS Servlet Container in web.xml

2. Create the Resource class and annotate it as @WebService

3. Create a End-Point Publisher class to expose your service

4. Create a client code to test it.

For REST

1. Add the WS Servlet Container in web.xml

2. Create the WSResource class

3. Use @Path to make service accessible by the URI.

4. Point your browser towards the WS URL to see it in action.
0
StealthyDevAuthor Commented:
Mukundha, I understand, what is to be done. but how it is to be done in JBoss, as you prescribed.?
I have attached screenshots of how i run a JSP page.
Consider, in the index.jsp page, i call AJAX for getting a value, say "some data from table". How to do that?

Rajkumar, I think, we are very close. I choose option#2 - REST. How to in the screenshot i have attached.?

I am googling each and every notations you use, like - POJO, REST, JAX-WS.. I am pretty exploring today.

Looking forward for more posts and suggestions.

P.S., I use jboss-4.0.4.GA....
MyTestServer.JPG
0
rajkumar_pbCommented:
I think this is what you need:

http://netbeans.org/kb/61/websvc/rest.html

Yes,creating a CRUD enable REST Based Web-Service is easy with NetBeans. Hope it  helps you
0
StealthyDevAuthor Commented:
As per the above link, Tomcat and GlassFish can be used for RESTful webservices in Netbeans 6.5.
Whereas I am using: Netbeans 5.5 with jboss-4.0.4.GA

Please advise.

Regards.

P.S.: I am struck only in selecting the package in the dropdown (4th selection in my screenshot - previous post) How to get RESTful package and where to put for NetBeans 5.5 :)

0
rajkumar_pbCommented:
Pretty simple. Type the package name as you dont have a package already and it'll create the Service class inside that package. it'll create the basic skeleton for your service and now you can start writing a simple Web Method inside that.

Now deploy and run the project to see your Hello World or whatever method in action
0
StealthyDevAuthor Commented:
Yes, it worked.

Now, I have created a webservice. (See attached image).

How to execute and see the service.?

I usually execute JSP like this:
http://localhost:8080/TestServer/index.jsp
And it is in TestServer/Web Pages/Index.jsp

My webservice is in TestServer/Web Services/Service - GetScore (function)

Regards.

MyTestServer.JPG
0
StealthyDevAuthor Commented:
Hi, i have found an XML.
Service-mapping.xml
0
rajkumar_pbCommented:
The XML you found is the WSDL file. Simply run the project to see the Web Service in action.
0
StealthyDevAuthor Commented:
No I am not able to run it. It is running my index.jsp page by default.
0
rajkumar_pbCommented:
Try to create a client code like the one in the link below and try to run the client

http://java.sun.com/webservices/docs/2.0/tutorial/doc/JAXWS3.html
0
rajkumar_pbCommented:
Also refer this to see what config you need to change in web.xml

http://www.developers-blog.org/blog/default/2009/10/23/JAX-WS-Example
0
rajkumar_pbCommented:
And this is what you exactly want

http://www.netbeans2.org/kb/docs/websvc/jax-ws.html

JAX-WS in NetBeans. Kickstart tutorial
0
StealthyDevAuthor Commented:
Hi guys, I have gained some knowledge about the webservices now. But still not able to run the webservice. I will try it in the near future. If you guys find any solution for the above comment. Please revert. As of now closing the question.

Best Regards.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
WCF

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.