This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
invoking a php method from java
Never came across with this situation before, so would like to understand / know how to go about this ?
Goal: Call php function from Java
Let's say Java code looks like this
pulic class Testing
{
String userid;
String pass;
String url;
public static void main (String[] args )
{
String value1 = checker ( userid, pass, url );
String value2 = dataGetter ( value1 )
}
public static String checker ( String userid, String pass, String url);
{
// Code to get authenticated
}
public static String dataGetter ( value1 );
}
and PHP code looks like this
<?php
$url;
$size;
function dataGetter( value1, $size)
{
// code to get data from server
}
?>
Will this be possible ? if so can someone explain me how deployment will work ? i.e java being deployed on tomcat and php on apache ?
While they cannot communicate directly, you can have them communicate in the same way that a client browser can communicate with the server, which is to say, using ajax and javascript. Have one page generated through jsp or php (doesn't matter which) load via ajax a php page or a jsp page (or servlet). The result is whatever you want it to be in either case and in this way you can get information from either.
Alternatively, you can make a Java program that opens a connection to a php page and uses what gets returned if you don't want to use a browser.
Of course, I'm assuming you are in the strange circumstance where you cannot simply drop one technology in order to use the other or I would highly recommend that solution over this. However, as these things usually go, if you're too far into your project to back out now, then this will also work for you.
Call the PHP via the command-line or via http using java.net.URL.
Related
In android studio project I am creating an app that has a login feature. The PHP page used connects to a database and has a select statement with POST variables. if the variables "username" and "password" are both in one row in the table then that means the user has access, thus an echo of "Success" is returned.
I am wondering if from within the main activity I can create statements that essentially will read "if the php file echos "Success" then startActivity of the next page. I believe i need HTTPURLClient, however, is there a way to "retreive" the echo from the php file in a short few lines and set the echo equal to a string variable in java so I can do a comparison if statement to see if it is "success"? Most HTTPClient tutorials are pages of code to connect
I used this library https://github.com/kosalgeek/generic_asynctask.
The basic format of the code using this library is this:
HashMap post = new HashMap();
post.put("txtName", variablename); // the first argument is the column name in your html file that refers to the username
post.put("txtPass", variablepass);// the first argument is the column name in your html file that refers to the password
PostResponseAsyncTask readData = new PostResponseAsyncTask(this, post,
new AsyncResponse() {
#Override
public void processFinish(String s) {
//s is the value that you get from the php execution so if you want to check for the word success you say:
if(s.equals("success")){
//do something
}
}
});
readData.execute("http://yoursite.com/getdata.php");
Please refer to the link for more.
I'm trying to develop an extension for firebug. I want to call a java method in this extension but there is no html in it so I can't use the applet-html solution.
Here is my java Applet :
import java.applet.Applet;
import javax.swing.JOptionPane;
public class MyApplet extends Applet {
public void init() {
super.init();
System.out.println("init something");
}
public String jsCall(String hello) {
System.out.println("this method is called by a js function and say :"
+ hello);
Thread t = new Thread(new Runnable(){
public void run(){
JOptionPane jop1 = new JOptionPane();
jop1.showMessageDialog(null, "Message informatif", "Information", JOptionPane.INFORMATION_MESSAGE);
}
});
t.start();
return "lala";
}
}
I try this:
var applet = document.createElement("applet");
applet.setAttribute("code","file:///home/dacostam/z_test/firebug-extension-examples-0bdcf15/helloamd#janodvarko.cz/chrome/content/MyApplet.class");
applet.setAttribute("id","javaToJavascriptApplet");
applet.setAttribute("mayscript","true");
applet.jsCall("HelloWorld"); //jsCall not a function
And this:
var dom = document.implementation.createDocument('http://www.w3.org/1999/xhtml', 'html', null);
dom.appendChild(applet);
dom.javaToJavascriptApplet.jsCall("HelloWorld"); //jsCall not a function
The document is not like usual there is no body or html in it.
Is there an other way to call java in javascript instead of the applet-html solution?
Or is there a way to do it with this method but otherwise?
If you need more information, I'm here.
Thanks.
Edit:
It would be to long to explain you what the program do, just keep in mind that it's inevitably in java, I just need to call a method which take a parameter string and return a string or eventually void.
Edit2:
Sorry I have not thought of that, java needs to run on the client.
If your requirement is to make a call from JavaScript to your Java application, you need to clarify where does your Java app needs to run.
If Java needs to run on the client (same machine as your Firebug), it looks like you are either stuck with an applet or you need to be building a tiny webservice in your Java so you can talk to it through a web API on localhost.
If your Java can sit on a server somewhere, you could talk to your Java application through a web API (similarly to the second option above).
I want to write a Java application that talks to a MySQL database on a server and changes the value of some variable v based on what it finds in the database.
If I were to simply do this on my computer I would have something like:
//import database stuff
public class Test{
int v;
public Test()
{
v=0;
}
public void main (String[]args){
Test t = new Test();
t.setVariable();
}
public void setVariable(){
// connect to database etc etc
if(something in MySQL database is true){
v= 10;
}
else{
v=30;
}
}
}
Now lets say I wanted to deploy this on a web server and expose the variable v to multiple users on the web who want to access a home.php file that was something like
$v = //call java program to return v
echo $v;
1) what would change in my java code or how I think of the whole problem? do i need a servlet? or tomcat? or do i just need to install the jdk?
2) how about the call from the home.php file how is it made?
what is a good resource I can use to learn about these issues?
Thanks alot!
i would code this using ajax language. I would write a servlet which would be called from the web page (javascript). In the servlet i would write a DAO layer which will talk to the MySQL database. In the servlet, the business logic (i.e. checking the value) would be written & appropriate value would be returned back to the front end (web page).
public static native void doConnect() /*-{
$wnd.jQuery(document).trigger('connect',
{
jid: 'sss',
password: 'sss'
}
);
}-*/;
i tried the above ,but there is no error in firebug or gwt hosted mode
console(so i cannot know whether the code is success or not). may i know is this the correct way to call jquery trigger? but when i put alert() in bind('connect'), it was not called
inside .js file
$(document).bind('connect', function (ev, data) {
alert('not call.....at all');
var conn = new Strophe.Connection(
"http://bosh/xmpp-httpbind");
conn.connect(data.jid, data.password, function (status) {
if (status === Strophe.Status.CONNECTED) {
$(document).trigger('connected');
} else if (status === Strophe.Status.DISCONNECTED) {
$(document).trigger('disconnected');
}
});
Hello.connection = conn;
});
I had similar issues when using jQuery UI with GWT - no errors in console/dev mode, yet the code did not behave like I wanted. The reason was that jQuery (and such frameworks) extend/change many core elements of JavaScript and expect it to stay that way - however, GWT code (meaning, also JSNI stuff) is executed from a "clean" iframe (so that no external frameworks can mess with the language and cause some weird errors in GWT, that's why you have to reference to the main window via $wnd).
I'd suggest moving your doConnect function to the host page (or external js file linked to the host page) and instead just call that function from your JSNI stub:
public static native void doConnect() /*-{
$wnd._doConnect('sss','sss'); //_doConnect defined in the host page
}-*/;
Or provide helper functions that will return Arrays, etc, from the host page, so that they include all the changes that jQuery made and expects.
It's a bit late for this answer, but your original code did not work due to a simple mistake: You have properly used $win instead of window but a few characters later you have used document instead of $doc :)
public static native void doConnect() /*-{
$wnd.jQuery($doc).trigger($wnd.jQuery.Event('connect', {
jid: 'sss',
password: 'sss'
}));
}-*/;
My cell phone provider offers a limited number of free text messages on their website. I frequently use the service although I hate constantly having a tab open in my browser.
Does anyone know/point me in the right direction of how I could create a jar file/command line utility so I can fill out the appropriate forms on the site. I've always wanted to code up a project like this in Java, just in case anyone asks why I'm not using something else.
Kind Regards,
Lar
Try with Webdriver from Google or Selenium.
Sounds like you need a framework designed for doing functional testing. These act as browsers and can navigate web sites for testing and automation. You don't need the testing functionality, but it would still serve your needs.
Try HtmlUnit, or LiFT, which is a higher-level abstraction built on HtmlUnit.
Use Watij with the Eclipse IDE. When your done, compile as an .exe or run with a batch file.
Here is some sample code I wrote for filling in fields for a Google search, which can be adjusted for the web form you want to control :
package goog;
import junit.framework.TestCase;
import watij.runtime.ie.IE;
import static watij.finders.SymbolFactory.*;
public class GTestCases extends TestCase {
private static watij.runtime.ie.IE activeIE_m;
public static IE attachToIE(String url) throws Exception {
if (activeIE_m==null)
{
activeIE_m = new IE();
activeIE_m.start(url);
} else {
activeIE_m.goTo(url);
}
activeIE_m.bringToFront();
return (activeIE_m);
}
public static String getActiveUrl () throws Exception {
String currUrl = activeIE_m.url().toString();
return currUrl;
}
public void testGoogleLogin() throws Exception {
IE ie = attachToIE("http://google.com");
if ( ie.containsText("/Sign in/") ) {
ie.div(id,"guser").link(0).click();
if ( ie.containsText("Sign in with your") ||
ie.containsText("Sign in to iGoogle with your")) {
ie.textField(name,"Email").set("test#gmail.com");
ie.textField(name,"Passwd").set("test");
if ( ie.checkbox(name,"PersistentCookie").checked() ){
ie.checkbox(name,"PersistentCookie").click();
}
ie.button(name,"signIn").click();
}
}
System.out.println("Login finished.");
}
public void testGoogleSearch() throws Exception {
//IE ie = attachToIE( getActiveUrl() );
IE ie = attachToIE( "http://www.google.com/advanced_search?hl=en" );
ie.div(id,"opt-handle").click();
ie.textField(name,"as_q").set("Watij");
ie.selectList(name,"lr").select("English");
ie.button(value,"Advanced Search").click();
System.out.println("Search finished.");
}
public void testGoogleResult() throws Exception {
IE ie = attachToIE( getActiveUrl() );
ie.link(href,"http://groups.google.com/group/watij").click();
System.out.println("Followed link.");
}
}
It depends on how they are sending the form information.
If they are using a simple GET request, all you need to do is fill in the appropriate url parameters.
Otherwise you will need to post the form information to the target page.
You could use Watij, which provides a Java/COM interface onto Internet Explorer. Then write a small amount of Java code to navigate the form, insert values and submit.
Alternatively, if it's simple, then check out HttpClient, which is a simple Java HTTP client API.
Whatever you do, watch out that you don't contravene your terms of service (easy during testing - perhaps you should work against a mock interface initially?)
WebTest is yet another webapp testing framework that may be easier to use than the alternatives cited by others.
Check out the Apache Commons Net Package. There you can send a POSt request to a page. This is quite low level but may do what you want (if not you might check out the functional testing suites but it is probably not as easy to dig into).
As jjnguy says, you'll need to dissect the form to find out all the parameters.
With them you can form your own request using Apache's HTTP Client and fire it off.