How does this Java application change when deployed on a server? - java

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).

Related

.net webapplication running on azure isn't in sync with data in sql

I have a Webapplication and database running on Azure. I have also made a java desktop application to add and delete rows in the db.
Now whenever i add or delete something in my database it will normally take half an hour to update the website. I want it to immediately update the website
this is my site
I use Ms SQL server2013,java8,MVC5 and an azure student license
For example i add a new continent to the database:
public class ContinentController : Controller
{
private IGradeRepository repository;
public ContinentController(IGradeRepository gradeRepository)
{
repository = gradeRepository;
}
public ActionResult ListContinents(int SelectedYear)
{
ViewBag.SchoolYear = SelectedYear;
IEnumerable<Continent> continents = repository.FindBySchoolyear(SelectedYear).Continents;
return View(continents.Select(co=>new ContinentsListViewModel(co)).ToList());
}
this is my code in java
repositoryController.insertContinent(new Continent(txtContinentName.getText().trim()));
txtContinentName.clear();
I expect some kind of issue with azure, because when i tested it on my localdb everything worked.
This is certainly not an issue with Azure. If the changes from your Java code are actually committed to the database and not just in memory, then they will be visible immediately.
Without code we can only speculate, but I think you're not committing to the database from your Java code.

Running test for database adapter using main() Method

I have a problem in a system that I am working as back-end support for. I need to write a test that calls one of the classes handeling the communications with our database so I can log out what it actually returns.
System setup
Our system is developed in Java and deployed on a weblogic server. It consists of many parts that I will not go into detail on here. But the interesting part is that we have a class acting as an adapter for our database. We call it "CMAdapter" and it is an implementations of IBM Content Manager specific code to handle interaction with our database. In this class we have a methid called fetchAct() that take one object with search parameters as an argument and it returns the result of the search. In this case it returns one act. The code we have is running on a weblogic server, that has an IBM Information Integrator for Content installed so that it can communicate with IBM Content Manager that is installed and running on a different server. The system is deployed on the server using a .ejb and a few .jar files.
The problem
I have recieved a case stating that for some acts the users are not recieving the full act as expected but only parts of it. The system itself displays no errors and the documents are present in the database. So what I am trying to do is write a simple test program that calls this "CMAdapter" with a predetermined set of searchcriteria so that I may log out the return of the search.
My question
How can I make a freestading class with a main() method and run it on the server? I need to make a call to the CMAdapter.fetchAct() method in a way so it runs on the server like any normal query?
My test class
public class TestHamtaAkt
{
public static void main(String[] args) throws BasException
{
Log LOG = Log.getLog(TestHamtaAkt.class);
// Get the CMAdapter
CMAdapter cmadapter = new CMAdapter();
// Create empty instance of our query object
SokVO sokvo = new SokVO();
// Put a value to search for in our query object
AttributVO aktAttribut = new AttributVO();
aktAttribut.setNamn(DLAKonstanter.AKT_KORT_R_KOD);
aktAttribut.setVarde("090084831574");
sokvo.aktAttributLista().add(aktAttribut);
// do the search an recieve the answer
AktVO aktvo = cmadapter.hamtaAkt(sokvo);
// log out the result
LOG.debug("main", "Akten som hämtades: " + aktvo.toString(), null);
}
}
Thanks to all for reading my question. It appears I have found the answer to my own question. It was hiding with a collegue of mine. The answer to my problem was, to be able to access the server deployed code, I need to get the JNDI context from my webserver and from that do a jndi lookup for the class I need.
I still have some problems making the connection but that is problably just my configurations that are off. I now know how I get a simple java class to make a call to a deployed class on a server.
here is the code I am currently using to get the context from the WLS server:
private static InitialContext getWLSContext(String url) throws NamingException
{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, WLS_CONTEXT_FACTORY);
//env.put(Context.PROVIDER_URL, "t3://" + host + ":" + port);
env.put(Context.PROVIDER_URL, url);
return new InitialContext(env);
}
This is my code for geting the class I need.
public static EJBObject getRemote(String url, String jndiname, Class homeClass, AppserverTyp typ) throws Exception
{
Object obj = getWLSContext(url).lookup(jndiname);
EJBHome home = (EJBHome) javax.rmi.PortableRemoteObject.narrow(obj, homeClass);
Class homeBase = home.getClass();
Method m = homeBase.getMethod("create", (Class[])null);
EJBObject remote = (EJBObject) m.invoke(home, (Object[])null);
return remote;
}
I hope this helps someone with a similar problem to move forward. Like I said I have still to actually get this code working for me but my this is the answer to my initial question on how to make a call to a deployed method from an external class.

Calling PHP function from Java [duplicate]

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.

Call java method in javascript without html firebug extension

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).

How can I fill out an online form with Java?

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.

Categories