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 :) )
LVL 15
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

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

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

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.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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

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


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

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

End Point Implementation


import javax.jws.WebService;

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

	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;



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

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("------->>  Call Ended");

Run the Client class to see the WebService in action.

Open in new window


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.


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.
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....
I think this is what you need:

Yes,creating a CRUD enable REST Based Web-Service is easy with NetBeans. Hope it  helps you
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.


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 :)

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
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:
And it is in TestServer/Web Pages/Index.jsp

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


StealthyDevAuthor Commented:
Hi, i have found an XML.
The XML you found is the WSDL file. Simply run the project to see the Web Service in action.
StealthyDevAuthor Commented:
No I am not able to run it. It is running my index.jsp page by default.
Try to create a client code like the one in the link below and try to run the client
Also refer this to see what config you need to change in web.xml
And this is what you exactly want

JAX-WS in NetBeans. Kickstart tutorial
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.
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

From novice to tech pro — start learning today.