This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Web Services and the fly likes Can I create a Web Service for an application that is constructed using Servlets and JSPs? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "Can I create a Web Service for an application that is constructed using Servlets and JSPs?" Watch "Can I create a Web Service for an application that is constructed using Servlets and JSPs?" New topic
Author

Can I create a Web Service for an application that is constructed using Servlets and JSPs?

H S Kumar
Greenhorn

Joined: Jun 08, 2011
Posts: 10
I am new to Web Services. I am trying to develop a web service that will interact with the servlets of an existing application.

Please let me know whether I need to use Restful Web Services for this purpose or not?

Also please provide some pointers of sample for such web services.

Thanks in advance.
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
What does "interact" mean? Do both WS and web app run on the same server?

Whether a REST or SOAP WS is the better choice depends on lots of circumstances about which we know nothing. For example, what kinds of data are to be sent back and forth? What kinds of clients do you envision? What are your security requirements? etc.
H S Kumar
Greenhorn

Joined: Jun 08, 2011
Posts: 10
Tim Moores wrote:What does "interact" mean? Do both WS and web app run on the same server?

Whether a REST or SOAP WS is the better choice depends on lots of circumstances about which we know nothing. For example, what kinds of data are to be sent back and forth? What kinds of clients do you envision? What are your security requirements? etc.


Interact means that WS will send request to Servlet and Servlet will return HTTP response. And this response will be converted as SOAP response by WS.
Both WS and Webapp will run in same machine and in one other configuration will be there in which WS and webapp will run on different machine.

The kind of data i am sending is just username and passwords to Servlet. Further I will need to send an attachment also.
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
This should be possible. The WS -whether it's implemented using SOAP or REST is immaterial- can access a servlet via an HTTP library like Apache HttpClient; that can handle file uploads (which is what I assume you mean by "attachement") as well.

Start by writing a standalone app that accesses the servlet using HttpClient. Once you have that, you can incorporate it into the WS.
H S Kumar
Greenhorn

Joined: Jun 08, 2011
Posts: 10
Tim Moores wrote:This should be possible. The WS -whether it's implemented using SOAP or REST is immaterial- can access a servlet via an HTTP library like Apache HttpClient; that can handle file uploads (which is what I assume you mean by "attachement") as well.

Start by writing a standalone app that accesses the servlet using HttpClient. Once you have that, you can incorporate it into the WS.


Please help me by giving some links(sample code or any online sample code) to develop a Http client that can send requests to Servlets
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Not "a HTTP client" - Apache HttpClient, which a search will find quickly. Its web site has lots of documentation and example code.
H S Kumar
Greenhorn

Joined: Jun 08, 2011
Posts: 10
Tim Moores wrote:Not "a HTTP client" - Apache HttpClient, which a search will find quickly. Its web site has lots of documentation and example code.


Hi Tim,

I have made a Apache Http client. That is working fine. Here is the code :
package pack;

import org.apache.commons.httpclient.HttpException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import java.io.IOException;


public class HttpClentSample {
public static void main(String args[]) throws HttpException, IOException {

DefaultHttpClient client = new DefaultHttpClient();
System.out.println("Trace 1");
HttpGet get = new HttpGet("http://localhost:8080/18-JUNE-2012-HttpWS-2/ServletInsert");
System.out.println("Trace 2");
HttpResponse response = client.execute(get);
System.out.println("Trace 3");
HttpEntity entity = response.getEntity();

if (entity != null)
{
long len = entity.getContentLength();
if (len != -1 )
{
System.out.println(EntityUtils.toString(entity));
}
}
}
}




But When I try to create a web Service for this Apache HttpCleint file and create .aar file and try to upload the file on Axis-2, I got error ar "Faulty Service"

I am copying the error trace that comes on Axis-2 admin console :

Error: java.lang.NoClassDefFoundError: org/apache/http/client/methods/HttpUriRequest at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source) at org.apache.axis2.description.java2wsdl.DefaultSchemaGenerator.(DefaultSchemaGenerator.java:136) at org.apache.axis2.deployment.util.Utils.fillAxisService(Utils.java:499) at org.apache.axis2.deployment.ServiceBuilder.populateService(ServiceBuilder.java:386) at org.apache.axis2.deployment.repository.util.ArchiveReader.buildServiceGroup(ArchiveReader.java:101) at org.apache.axis2.deployment.repository.util.ArchiveReader.processServiceGroup(ArchiveReader.java:143) at org.apache.axis2.deployment.ServiceDeployer.deploy(ServiceDeployer.java:81) at org.apache.axis2.deployment.repository.util.DeploymentFileData.deploy(DeploymentFileData.java:136) at org.apache.axis2.deployment.DeploymentEngine.doDeploy(DeploymentEngine.java:659) at org.apache.axis2.deployment.repository.util.WSInfoList.update(WSInfoList.java:144) at org.apache.axis2.deployment.RepositoryListener.update(RepositoryListener.java:337) at org.apache.axis2.deployment.RepositoryListener.checkServices(RepositoryListener.java:241) at org.apache.axis2.deployment.RepositoryListener.startListener(RepositoryListener.java:331) at org.apache.axis2.deployment.scheduler.SchedulerTask.checkRepository(SchedulerTask.java:64) at org.apache.axis2.deployment.scheduler.SchedulerTask.run(SchedulerTask.java:71) at org.apache.axis2.deployment.scheduler.Scheduler$SchedulerTimerTask.run(Scheduler.java:90) at java.util.TimerThread.mainLoop(Unknown Source) at java.util.TimerThread.run(Unknown Source) Caused by: java.lang.ClassNotFoundException: Class Not found : org.apache.http.client.methods.HttpUriRequest at org.apache.axis2.deployment.DeploymentClassLoader.findClass(DeploymentClassLoader.java:86) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 19 more


Please help me in Creating a Web Service for Apache HTTP Client java program.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can I create a Web Service for an application that is constructed using Servlets and JSPs?
 
Similar Threads
Change Web Service Permission
Job Prospects for Web Services
Help
how to call a service running on AS 400 box from java ?
struts.action.excludePattern not working in Struts2.1.8.1