Using a Web-Service with Java Servlets - java

I'm trying to develop a very simple Java web application using JSP and Servlets.
1) There is a textbox and a submit button on the page,
2) The user enters his name, say John, to the textbox and clicks the button,
3) The string is forwarded to my servlet,
4) At the doPost method of my servlet, I access the posted string variable,
5) The web service I'll use has a sayHello method that takes an argument and returns "Hello " concatenated with the argument,
6) So, I call the sayHello method of the web-service, get the returned variable and forward this to a JSP, which basically writes Hello John.
I'm familiar with the JSP and Servlet thing, but I don't know how to use an already existing web-service, or how to make use of a functionality that is already implemented in that web-service.
All I have is the name of the method, sayHello, the URL of the web service, http://example.com/hello_service and a link to a wsdl file which contains xml-like code that I do not know how to make use of.
My question is, how do I make use of that web service, or how do I call a method inside a servlet?
Thanks in advance.

I'm using Eclipse for JavaEE Developers. How do I generate a client automatically?
Drop the WSDL file in your dynamic web project (or create a new project for it), rightclick it, choose Web Services > Generate Client, complete the wizard with default settings. A new package will be created where the generated WSDL client code is been placed. One of those classes have a ServiceLocator in the classname.
In the servlet, you need to instantiate the ServiceLocator class, get the SOAP service from it and then invoke the desired methods on it. Further detail can't be given since the WSDL is unknown.
See also:
Eclipse - Creating Web Service Client (Eclipse's own tutorial does it bit differently)

You can use "wsimport" from jax-ws to generate a client jar for the web-service. Then, including the client jar in your classpath, you can call the web service just like you would call any regular method.

you have to create client stubs which will be part of your code project (which has the servlet). The WSDL defines how to generate these stubs. The you can call the methods in the stub from your servlet. You can use a variety of tools to generate these stubs, Axis2 is one of the most widely used.
Here is the apache Axis2 documentation which tell you how to do it.
This stub will have the methods that the wsdl has defined. You will basically call these methods and internally the stub implementation (autogenerated from wsdl by axis2) will create the SOAP request based on the arguments you pass to the method. Then it will send this request over HTTP or HTTPS to the webservice URL. You will feel like you're calling code that resides on your machine, but internally it makes the call to remote webservice.

Related

Jersey REST web service find source code from URL

I have a prebuild framework where web services are already implemented using the Jersey framework.
I have a list of web services URLs and access to jar library where there is a complete set of jars(200+) including the web-service jars.
I need to find the extract class and method name which is invoked when a web-service url is invoked.
E.g webserice url: https://myserver/resources/v1/foo/bar
How to find which class(along with package name) and method is invoked when above url is hit.
I have tried finding keywords "foo" "foo/bar" "bar" using find in files options of notepad++. But it gives a big list of jars and not able to drill down the exact class and method name.

Using a GAE Endpoints object in a servlet

I'm adding a simple web interface with the already working backend I wrote in Java for and Android app. I know there is a way for exposing GAE ednpoints to JS. https://cloud.google.com/appengine/docs/java/endpoints/consume_js
Instead I want the calls to the endpoint to be generated from a java servlet and pass them to a JSP so I initialise the UserEndpoint and call it's methods inside the doGet().
Is there anything wrong with this method ?

java servlet google in app payment

I have studied the java in app billing code snippet at
https://developers.google.com/in-app-payments/docs/tutorial
and I am unable to use it to make my application capable of doing in app payments.
My first question is how do I set up a servlet to handle payment requests. Do I put the getJWT() method in the servlet and call it from the doPost() method?
My second question is what do I do with the String that getJWT() returns? It should be the json object that holds the purchasing information, but I don't know how the jsp file I have should process it.
I have searched for example code using java servlets and jsps to study but found none. I have found python code, but I can't translate python into java yet. If anyone knows of an example (complete) of google in app billing using java servlets and jsps I would appreciate it if you could post a link also.
Thank you.
On the server you need an HttpServlet derived class that takes the order request (in doPost()), calls the JWT libraries together with the Seller Secret to generate the signed JWT string, then returns the result in the response.
On the client side in the HTML page you can use a templating system (e.g. AppEngine + JSPs) or Ajax calls to your servlet to get the generated JWT.
The generated JWT is one of the paramenters to the goog.payments.inapp.buy() JavaScript API.
Below is a simple AppEngine Python implementation of In-App Payments. You can re-use the client-side code and replace the server-side with the Java implementation:
https://code.google.com/p/iap-python/

How can I upload a file to Struts from a standalone Java class?

I am trying to call a Struts action from simple, standalone Java class.
The Struts action uses FormFiles to receive file uploads.
I am reading about URLConnection and HttpURLConnection, but am not sure how to proceed.
In general form submission is just an HTTP POST request with parameteres.
It gets generated by your java standalone class and sent to the server, the server proceeds this request and then redirects it to Struts.
However I wouldn't use here directly the low-level java API since it doesn't implement the HTTP protocol.
If putting an additional jar to your project is not an issue, I suggest you to use
Apache HTTPClient project
Here is an example (much more simple than it would be with HttpConnection):
Example
As per my understanding from your question ... you are creating object of action class (by standalone java class) and want to use it similar as action class works in struts.
But when you create object of action class it works as a normal java class instead of struts's action class.
Action class object must be created by struts framework only.

invoke webservice dynamically using java

i want to invoke a web service based on the url and method name,when given the input parament in xml format ,i need to invoke the web service ,but i can't generate the client stub using tools like wsdl2java because the url and method name are given dynamically so these class aren't compiled.
for example,http://localhost:9090:/hello?wsdl there have a method
string sayhello(String []names); the input param likes <arg0>john</arg0> <arg0>lucy</arg0>
it seems that i need to generate soap request in code so is there any library can help me do this?
thank you for giving any recommendation!
That's possible and yes, you will need to generate the SOAP request yourself and also parse the reply yourself.
Some links to help with this including source code etc.:
SoapUI (complete source code of a generic SOAP client including a nice UI)
http://anshu-manymoods.blogspot.com/2009/10/how-to-simple-generic-soap-test-client.html
http://www.java-tips.org/other-api-tips/httpclient/how-to-send-an-xml-document-to-a-remote-web-server-using-http-5.html
http://biomoby.open-bio.org/CVS_CONTENT/moby-live/Java/docs/soapServlet.html
http://www.soapuser.com/ngx_22jul01.html
IF JavaScript is an option you can check this out...
There's SAAJ but it's pretty verbose.
If you can use Spring, Spring-WS has a number of client options.
You can always just build up the XML by hand, too (and parse the returned XML).

Categories

Resources