Call an external web service from a servlet - java

I'm developing a servlet that gets a name of a web service and could be forward the request to an external web service, for example: http://www.webservice.com/...
I have build a response wrapper that intercept response output but I can't forward request to an external web service, it works only if I redirect the request to a servlet that is on same server.
Example:
request.getRequestDispatcher("aMyServlet").forward(request, response) // WORKS
request.getRequestDispatcher("http://www.webservice.com/...").forward(request, response)
Doesn't because Tomcat searches http://www.webservice.com/... on the server as a local resource.
How can I do external request?
Thanks

forward method that you are using is used for communicating between server resources, (eg: servlet to servlet as you have found out) If you want to redirect to another location, you can use the HttpServletResponse's sendRedirect method.
Better option is to
Perform your own HTTP request and stream the results back to the
browser. This sounds harder than it is. Basically you create a
java.net.HttpURLConnection with the URL of the web site you want to
"redirect" to. This can actually contain the query parameters (as long as
they're not too large) since it will never be sent to the user's browser
either and will not appear in the browser URL bar. Open the connection, get
the content and write it to the Servlet's OutputStream.

To make any request to an outside service, you'll have to explicitly make a new HTTP request and handle its response. Take a look at the HttpUrlConnection class.

You don't mention what kind of service you want to invoke, but either way, your servlet is functioning as a service client, so you should be looking at service client technologies.
For invoking REST style services, java.net.URL or Apache Commons HttpClient can be used to send a request for a URL and fetch the reponse.
For invoking SOAP services, you can use Apache Axis, or Java WSIT.

Related

Receive an http post from an external server with servlet

on my public web application running on JBoss 5 with Java 6 I need to implement something that receive an http post from another external server with some parameters (server to server call) .
The server that call me doesn't need an answer, just standard http code reponse( es 200 - OK).
Can I use a servlet for this?
If not what should I use? (I cant'use restfull or soap ws).
To my knowledge, your best bet for given situation is REST or SOAP. But you already mentioned that you cannot use that.
I don't see any reason why a servlet can't be used here. You have request & response objects in servlet doPost method. You can manipulate the response object to set appropriate response code back.

Sharing of request object between two web application

I have two web applications, WebA andWebB, deployed on different tomcat. I want to send request object fromWebA to WebB. I had used wurfl in WebB application for getting information about browser and operation system etc..or other work.
On WebA application I want to show that data graphically(just like analytics).so for this purpose i had to pass request object to WEbB application.please suggest any way to pass request object from WebA to WebB with same session.
What you need is a proxy, passing everything from webA to webB. The request object contains: request parameters, http headers, cookies. You could try to write a simple proxy yourself (with the help of HttpClient) opening a connection to webB and setting all the necessary data.

applet->servlet->a record file(on the server) : how do i do this?

To make a poll form using an applet, i wanted to know how can my applet communicate with a servlet . That servlet is meant to write the result to the text file on the server. I have no idea how can i do this.
You could use java.net.URLConnection for this.
Assuming that your servlet is mapped on an URL pattern of /myservlet and your applet is been served from the context root, then this should do:
InputStream servletResponse = new URL(getCodeBase(), "myservlet").openStream();
// ...
That's all. The getCodeBase() is inherited from Applet class and dynamically returns the applet's code base URL (from where the applet was been downloaded). The servletResponse will contain whatever you wrote to response.getOutputStream() or response.getWriter() in the servlet. For example just an "ok" string or an easily parseable format like XML or JSON. You could pass request parameters as a query string in the GET request URL, or in the POST request body.
See also:
Using java.net.URLConnection to fire and handle HTTP requests
https://stackoverflow.com/questions/9361399/how-to-send-list-of-objects-from-servlet-to-applet
The applet and the servlet is separate. There is no easy way you can use magic to make this easier.
A servlet is a snippet inside a web server which gets executed when a HTTP request is being made to the correct URL on the web server. Hence you need to make a HTTP request to the correct URL on the web server where your servlet is running.
This is done in the same way you do any other HTTP request from an applet, which is done in the same way that you do a HTTP request from a self-standing application.
Well you have several options for the applet/servlet communication....
http requests. (This may be the easiest). For this you can use Apache HTTP Components
Remote Method Invocation RMI. This may be more complicated than http requests but it depends on what you want to achieve.
Sockets. (I think http requests is flexible enough for your use case but just in case)
javascript. You can call a javascript function from your applet and let the javascript function submit the information to the servlet through ajax, websockets, etc.
Of course there are many other options but these are some ideas and remember that you may need to sign your applet.
If your question is about how to write to a file there are many tutorials. Here is a good one

Getting post data from request

I'm writing a server side app in Java using the HttpCore library.
I have an HttpRequest and I'm trying to get the postdata sent from a form. The problem is- when I use request.getEntity() it returns a null object, even though when I look through HTTPFox on what kind of request I'm sending the post data is clearly there.
What am I doing wrong?
There seems to be some confusion. You are sending requests from a browser to the server. The server is likely using the servlet API. There you handle requests using the doPost(..) method of an HttpServlet. You have an HttpServletRequest from which you can get the parameters - request.getParameter("paramName")
HttpCore on the other hand is used to make requests, not to handle requests. It is used as an http client (in the role of the browser).

Google protocol buffers and servlets

I am wondering how I can use google protocol buffers to accept a request and send a response back to a client? I am thinking about writing a servlet which will take a request.
Is the following trail of thought the correct way to implement this:
1. Have a .proto file which is the message definition for the incoming request.
2. Write a servlet which accepts this request, does various tasks like querying database
and then sends a response. Will this response require a separate .proto message definition with all the fields that make up the response?
3. Will the client just invoke the doGet() method of my servlet and pass the request, it should then return a response as a protobuff object?
Any suggestion or idea will be very much appreciated.
Typically you'd want a request message and a response message, yes. You'd also probably want a method name to describe the action - that's certainly how the built-in PB services work.
The client wouldn't invoke doGet() - it would make a request (probably a POST rather than a GET) and your servlet would receive it.
Now, ideally you could have a general "ProtocolBufferServlet" which could service the requests by handing them off to services implementing the appropriate interfaces.
I suggest you look at the documentation for Protocol Buffer services and the Java services generated code for more information. You could implement an RpcChannel which worked over servlets, or get the client to make the HTTP post directly. You'd probably use dependency injection of some kind at the server side to tell the servlet what was implementing the service.
HI,
I have this up and running. I ended up posting a http request as a post to my servlet. I was able to take the request protocol buffer, read the request, do some processing and then send back a response. It was actually really simple once I got it working. We used the 1 .proto file to define the request and response message structure.

Categories

Resources