I would like to send a request to a Java Servlet from PHP and receive the response from the same and show it on the PHP page. How should this be done?
Thanks and Regards
Abishek R Srikaanth
If all you want is to print the response of a GET request to an external resource plain vanilla into the PHP response, then you can use file_get_contents() for that.
<?php echo file_get_contents("http://example.com/someservlet"); ?>
The servlet's doGet() method will be invoked and whatever response it returns (which can even be a forwarded JSP) will be printed as string to the PHP response.
If you want a little more fine grained control, e.g. using POST or something, then head to curl() instead. The linked PHP manual contains several examples.
Regardless of the way, please note that whenever it returns HTML, that you should ensure that you end up with valid HTML. For example, nesting <html> tags is illegal. Pass the PHP page through the w3 validator if you're unsure. Otherwise you'd better have to parse the HTML to extract the <body> pieces of interest or to use an <iframe> instead.
<iframe src="http://example.com/someservlet"></iframe>
If I'm understanding you correctly you want to read the response of a servlet in php and then output it from php?
You can use file_get_contents to the url (Probaly not the best way, but for simplicities sake it the easiest) and then just echo the output.
For example:
$content = file_get_content('http://www.google.com');
echo $content;
But if you want to be able to login or use the session at the servlet side you will need think of something else. As each request to the server from php will be a new one, it does not store cookies etc.. like browsers do.
Hope that helps
This is a situation which might do well to be rethought, but, if there are no other options...
If there is a way to actually update the portlet, then I would recommend creating some form of service call -- SOAP, custom RPC, etc -- on the Java side. Technically this is the most correct way to do things.
Failing that, if this is a simple GET request, then use file_get_contents.
If it has to be a POST/PUT/DELETE, then you can use cURL. cURL also has the benefit of being able to handle simulated sessions, which means that you are then able to simulate a log in and actions following that (though not without some difficulty).
If you don't have cURL and you need to POST/PUT/DELETE, then the streams library might be able to give you what you need.
If you don't have the streams library or cURL and you need to POST/PUT/DELETE, then there are other means of accomplishing that, but maybe you should really re-rethink that situation.
If all of the above don't work, then you will need to tame the Spectral Wolf. The Spectral Wolf fears only fire. I can no longer help you, but if you master the Spectral Wolf, he will guide you. Godspeed.
If you really want to do that, you can create a Java app that takes parameters to populate request and response objects, instantiates a servlet, runs the correct method, gets the result and displays it, then call this Java app from PHP.
Or you can use the experimental and unrecommended PHP / Java Integration module.
Related
I am looking at extracting data from a survey tool using their API. Being fairly new to APIs, I went through their documentation, few videos and figured out a little of what's happening.
But along with the Method and Attributes, they also have a sample request in Java/PHP and a sample response. Now the sample response is the JSON file fomat I'm expecting back but what do I have to do with a sample response?
As in, from all the videos I have seen you call an API using a particular URL. What is this sample request used for? Is it for using it in your own code?
I know its quite a silly question but any input would be much appreciated.
Assume you have a system that manages "virtual machines" for you; and that offers an API like:
/system/create-new-virtual-machine
/system/{virtual-machine-id}/start
So: your client would first call the first API method to somehow define a new virtual machine (providing all the required data to create a configuration).
Obviously, when you create a new thing, that should result in a new URI; which you would use later on to make other calls. In our example, the create call would return 200 ... and give you the URI for the newly created vm within the result body.
Long story short: just think of your REST api as the interface of a component/module your code is dealing with. Some of the API operations just tell you "fine, I passed" ... but others will want to give you information back. And that is what you need the response body for.
I am developing a REST web service in Java which on clients' request processes the request body and gives the output to the client. The request body sent by the client consists of a GO(programing language) program which the server executes(runs) on the server machine and returns the standard output of the program back to the client. Now since the request body contains some text(program), I cannot use HTTP GET method to do that. I can use PUT or POST, but I have learnt that they(PUT and POST) are generally used for updating/creating a resource. Since, I am not creating any resource here, is it conceptually correct to use PUT or POST. If not, which is the HTTP method that I need to use?
Looking at the problem you are solving and comparing to an existing production solution , I suggest that you can use POST in your scenario.
Reasoning - Example Production code solving similar problem:-
Assuming that the problem you are trying to solve is this:-
Clients submit code in Go programming language, Your server compiles it, runs it and then gives the output. Assuming also that, it is somewhat similar to many online coding websites like hackerEarth, their API documentation page and the sample python code provided show that we can use HTTP:POST to submit code to the server for its processing.
Reasoning - RFC for HTTP POST, PUT :-
POST is designed to allow a uniform method to cover the following functions:
Providing a block of data, such as the result of submitting a
form, to a data-handling process;
The PUT method requests that the enclosed entity be stored under the supplied Request-URI. If the Request-URI refers to an already existing resource, the enclosed entity SHOULD be considered as a modified version of the one residing on the origin server.
Referring to the above statements, we can conclude that in the context of this problem which you are solving, you are requesting the server to do some data-handling for the enclosed entity, so you can use POST.
Using jQuery AJAX, can we call a specific JAVA method (e.g. From an Action class)
The returned data from that Java method would be used to fill in some HTML code.
Please let me know if this can be done easily using jQuery (like it does in DWR)..Also for multiple data points in HTML, do we need to make multple AJAX requests?
The simple answer is you map your ajax calls to urls, which are in turned map to methods in your java code. The Ajax -> URI mapping happens on the client side (which ever js framework you are using, and the URI -> specific handler mapping happens within the java application)
What java framework are you using? There should be very clear and simple documentation on how to do this. For standard Java EE mappings (meaning you are not using any frameworks like Spring or Roo) I found this on google: http://javapapers.com/servlet/what-is-servlet-mapping/
"For multiple data points in HTML" I assume you are talking about having multiple parts of the html update. You can do this with multiple requests, or you can do it with one request. If you do the latter, the server needs to return all the data you need to update the dom appropriately.
It's not as transparent as with DWR--DWR handles making JavaScript look like Java. With jQuery you'll get JSON (or just HTML if/when it's easier that way). It's still pretty straight-forward, though. You'd send the Ajax request to a URL, rather than having it look like a local method call.
I'm not sure what you mean by "multiple data points in HTML" -- you get back whatever data you get back, and you can do with it whatever you want. If the response has all the data you need, then you wouldn't need to make multiple requests.
The Same Origin Policy is preventing me from fetching the JSON data I need from another web site (with permission). I saw one person who was working around this with JsonpRequestBuilder, but I'm not sure if that's going to be the best solution for me. The only other option that comes to my mind would be to have an intermediary servlet on my server.
What's my best bet here? I have concerns with both methods. With an intermediary servlet, I worry about the delay that would introduce. With the JsonpRequestBuilder, it looks like I have to create a complete JavascriptObject for each method I'll call from the other site, even though I only need to pull out a single value from some of those methods.
I don't use Java, but JSONP is what I usually implement when I need cross-domain chatter, and I'm sure someone will have made a Java library that unwraps it. It requires a change on the third-part's site, but it is a very simple change.
EDIT: Sounds like that is what that library does, sorry... but still... it's the way to go :)
Check out the CORS Specification. We are using this to successfully circumvent the SOP using our own server with GWT's devmode Jetty.
You don't have to "create a complete JavaScriptObject", a JavaScriptObject is actually just a mean to call to JavaScript from the Java world, so you only need the one getter for the value you need, and it can even return a "nested" value:
public native String getFoo() /*-{
return this.nested[0].obj.foo;
}-*/;
Whether you'll use JSONP (and JsonpRequestBuilder) or a "proxy servlet" actually only depends by the capabilities of the "service" you need to call: JSONP is JavaScript, not JSON, so the server has to return a "JSONP response script" or you won't be able to use JsonpRequestBuilder (and similarly, you won't be able to (safely) use CORS or a proxy-servlet if the server returns a "JSONP script" rather than application/json).
Hi All
I have a requirement like, from some web page on some event user will redirect to MyApp's login page with some parameter. I want to test MyApp is able to get that parameter or not. To test that i made another app in which i am using (code is below ). But this is showing parameter in url which i don't want. I also want to know what other ways are there to redirect/forward to some another url with parameter ( parameter should be hidden hiddn ).
<%
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location","http://localhost:8080/MyApp/login/login.jsf?user_id=inc&reference_id=123456789");
%> THANKS
It's not possible to do POST redirects, so you can't just redirect with a POST from your scriptlet.
What you can do is:
Build an HTML form in POST with the parameters that you want to pass, but remember, the POST perceived security is brittle, since with a tool like firebug anyone can see those variables (or they can just look at the html source).
A way around the problem is use encryption or one time passwords (that last for a session). I used this way, to connect seamlessly a Java application with PHP. If you're interested I could look it up and share the details of that solution.
First of all, do not use scriptlets. If you dont want to see the parameter in the URL, don't send it with the URL. You can do a POST submit or pass it as a hidden variable from your page.