As a kind of personal project, I'm trying to develop a Java Applet that will stream from a collection of music that I will host on the same machine as the applet. I know when you request files in an applet, it looks for them in the files of the computer which is accessing the applet. I, however, want to access files that are hosted on the same server as the applet.
My intention is to, when the client wants to stream a specific song, the server will begin loading the data and streaming the data to the client. I think this would be possible using the URL class in Java, but would that not be going out to the internet only to come back to the very same server to get its files? Or will it recognize that the files are local and access them without going out to the web?
Summary: What is the best way for an applet to access files that are hosted on the same server as itself?
An applet runs on the client machine. In order to access files the server you're going to have implement functionality on the server-side to expose those files to your client. There are various ways to do this in Java - the most common would be:
1) a custom servlet or
2) a web service
Here are some links to articles which could be helpful:
Can a web service return a stream?
FileServlet serving from absolute path
As an aside I think your question is quite broad. You're probably going to have to investigate one or both of the methods above, try to implement one of them and then come back to SO with more specific questions.
Related
I have a simple cloud IDE,I want to make it able to build and run applications remotely, the target application's source files will be in a remote server in isolated virtual machine (e.g Windows 8.1,or Ubuntu 14.04). It's not difficult to build that application but how to run it and view its output to users ?
What if it's a desktop application (suppose it's written in C# or Java or Python)?
Note: users access there applications only using browsers (e.g Firefox,Chrome,...)
Edit: desktop application may contains GUI stuff not only console ;)
You need a web application.Now this web application when loads send request to backend code that backend code will do SSH to remote machine and read the file from specific location.Now that read stream will be send back in response and displayed on web based UI. In these type of application few thinks matters.
1) Like if you whole file at once then it will take time to display that content to user.Better idea will be read around 100 lines at once and when user scroll down then again send request to web server to read next 100 lines in this way you can decrease response time and better user experience.
Each of the languages you mentioned offers a Web Services framework of some kind. Pick one, and implement something that a) starts your app, b) shows the output. Depending on the processing time (how long it takes to complete) you might even get away with just one.
You can go for a self-contained, standalone service:
C#: Is it possible to create a standalone, C# web service deployed as an EXE or Windows service?
Python:
Best way to create a simple python web service
Java:
https://technology.amis.nl/2009/06/05/publish-a-webservice-from-a-poja-plain-old-java-application-that-is-out-of-the-container-using-endpoint-class/
Alternatively, you might use a container (server) for your app, like Apache with mod_mono or IIS for C#, Tomcat, Jetty, Jboss for Java, Apache with mod_wsgi for Python (just examples).
The web service would probably sit on the remote machine, so it could use system calls ('command line') to run your core app, and then it would send the results over http. Since you mention GUI, there could be more layers to the solution:
The GUI - static HTML, desktop app, sending requests to the 2nd layer, say displaying dropdowns for parameter1 and -2
The Web Service - takes the params from the request, say http://remote.machine.land/start/app?parameter1=X¶meter2=Y, runs a local command like /home/users/myapp.sh -parameter1=X -parameter2=Y
The application itself - not necessarily aware of any internet out there.
This way you stay free to change/enhance any part at a time, call the 2. layer programmatically, introduce load-balancing, etc.
3.
From my understanding the Java Client is what is rendered onto the browser such as the javascript, "the client side" for example what the user see's (tables, html, the sorting etc) things in the jsp. The server side is the servlet that makes the connection and call the specific class's based upon the request. I feel as though I am completely wrong as when I do research I am getting different answers. CAn I get some clarification on this please.
Java running on the client side means the end user needs Java installed and is running a Java program on their computer. This could be a standalone Java executable program (a traditional application), or it could be through a browser running a Java applet. Either way the end user's computer is actually running the Java code locally.
Java on the server side means that all the Java processing happens on a server somewhere, and the person accessing the site or service doesn't need Java installed. Servlets and JSPs fall into this category. The server does all the processing and then returns HTML/Javascript/CSS, etc to the end user. The user's web browser receives and processes those files to display the web site.
It sounds like you are getting confused because web browsers use Javascript to display dynamic sites, but Javascript is a totally separate language that has nothing to do with Java. In other words, a web site could be powered by Java servlets on the back end and could also use Javascript on the front-end, but they aren't directly related or dependent on each other. All the HTML, Javascript, CSS, etc that make up the web site are processed by the user's browser and the user doesn't have to know or care how all that stuff is generated on the back end.
I have a web application running with support for some specific pieces of hardware. This is achieved in the following steps:
User runs a small installer that places java files (and a couple
others) on the client machine. The main piece is a jar called "hardwareManager"
User visits web app. The web app runs a java applet which, due to
a .java.policy file placed during the install, has permission to
interact with the client machine outside the browser sandbox.
The applet checks to make sure the hardwareManager is running,
and if not runs a command to start it.
User interacts with the web app which sends commands to the applet via
javascript. The applet then writes commands to a text file
on the client machine. The text file is constantly monitored by the
hardwareManager which runs any commands it reads in.
This works, but seems clunky. I have a couple ideas on how to improve it, but I don't know which, if any, are even worth trying.
Would it be better to set up the hardwareManager as a socketServer and have the applet connect directly to it, rather than going through text files? Is that even possible?
Is there a way to eliminate the applet altogether and have the javascript talk directly to the hardwareManager? Maybe by writing the hardwareManager to be a local http server? What port should it run on? Do javascript xss limitations fit in here somewhere?
It would be less clunky to start the Java application using Java Web Start. This would remove the need to daemonize or install the Java hardware manager.
Another alternative is to use a built-in browser inside Java. I supose this is not an option, since you depend heavily on Javascript (I suppose to provide a rich client experience).
If you already have to install something on the client machine, why did you make the choice to go with a web application?
Talking from experience: We had a Java EE application which needed to print to PoS printers at the client site. We installed a small "synchronizer" application that connects through SSH and synchronizes all clients files. Afterwards, it loads the JAR and executes the program. This program connects through RMI with the server and subscribes to a JMS queue to receive the print assignments.
Applied to your case: Why not let your Java application connect to the server directly? You can use HTTP, SOAP or even JMS over RMI. You can then launch the hardware command from the server (instead of from the limited JavaScript webbrowser environment). This way, you get tons of features: authentication, buffering of commands, and you can even share hardware between multiple clients.
Schematic:
<----AJAX------> Web browser
ApplicationServer
<---HTTP/SOAP--> Java hardware manager application
You can launch the Java application using Java Web Start, which allows you to update the application automatically (instead of needing to pass every client a new installer).
I need to load somehow the html code of a webpage A into a javascript string of another webpage B, on a different host. I know this is impossible to do with javascript alone because of the same origin policy, and I know I could do it loading the page via php on my server and then send results back to the user's client but I wouldn't be able to handle so many requests, so I need it to be done directly by the user's browser. I can use nearly whatever browser scripting language/applet framework common enough to be installed on the majority of my users' computers, like flash and java.
On example, what if I use flash or java to load the external html code and then call a javascript callback function providing the source? Could this work?
Do you have ANY idea? I gratefully accept any suggestion, and I REALLY appreciate examples!
Thank you very much!
Matteo
It would require a digitally signed and trusted applet in order to reach cross-domain, short of the user running a plug-in 2 architecture JRE and the site implementing a Cross-Domain XML.
Ordinarily, unsigned Java applets or applications may only connect
back to the web server from which they originated. This restriction
prevents an applet from causing denial-of-service attacks by making
rapid connections to arbitrary machines on the Internet.
In Java SE 6 update 10, both the Java Web Start and Java Plug-In
technologies contain preliminary support for cross-domain policy
files, which specify how unsigned code may access web services on the
Internet. The crossdomain.xml policy file is hosted on a given server
and allows either selected clients, or clients from anywhere, to
connect to that server. Cross-domain policy files make accessing web
services much easier, particularly from unsigned applets.
"via php on my server and then send results back to the user's client but I wouldn't be able to handle so many requests"
So many requests? That is not so many reqs; just making php script to read couple page and creating new page depending the data. If that is too much for your server ..hard to believe.. you sure can do that kind of thing with flash (clients computer) to load those two pages, and parse the datas to one html page and display it (via js) to clients browser. Kind of weird question after all.. perhaps i did not understand it :)
I have a java application that will take the image as an input and output another image. I have a website with a popular host (PHP+MYSQL Hosting). I want to create a page on the website with PHP with a form where a user can upload an image which will then pass the image onto the Java application.
What I am planning on doing is when then user uploads the image, it gets stored in a folder on the web server. I will then call the java app on the server passing the url of the image as an argument and then the java app will output another image, let’s say, to a result folder. The PHP page after the execution will then display the result image on the browser.
Now my questions are:
Is it possible to execute java apps on popular webhosts (for example mine is WebHostingBuzz.com)?
The java app is fairly heavy as it does a lot of image processing. Should I offload the java app to another web server? If yes, are there any services that will run my java app?
(Optional) It’s a demo of my java app and I don’t want to store the images people upload. Is there a way where I can directly pass the uploaded image to the java app and output the image generated directly instead of storing it on the web server? I would prefer this because, if the image is big, I can make PHP stop the execution after a timeout.
How do I communicate with the java app from PHP for info on its execution, for example When PHP calls the java app, the page has to wait till the app finishes processing? I want the java app to send a response to the PHP page saying that the processing is completed and the page is redirected or refreshed accordingly.
I hope you get the idea, please suggest the technologies that I can use to implement this and also if you have a better idea, post it!
Thanks!
Now my questions are: Is it possible to execute java apps on popular webhosts (for example mine is WebHostingBuzz.com)?
It's technically possible. But the hosting has to install JRE at the host and give the PHP user sufficient OS-level and filesystem-level permissions. So you're really dependent on the hosting whether they provide this opportunity. Best is to just contact their support team and ask it.
If it is supported, you could just use shell_exec().
$result = shell_exec("java -jar /path/to/imageprocessor.jar " + $imagepath);
if ($result) {
// Shell execution succeed.
} else {
// Shell execution failed.
}
For asynchronous communication / background processing, the client has to fire an ajaxical request.
If it is not supported, consider porting Java to PHP. The GD image library has pretty a lot of functions which may be of use.
Google App Engine allows to host Java (and Python) web applications. The SDK and the basic account is free of charges. With the SDK, you could develop and test the application locally and then simply deploy to App Engine (NetBeans and Eclipse plugins are available).
Then the PHP app could send the data in a HTTP POST to the Google App Engine application and get the result in the response data.
Or the data is stored first in a database blob and a processing job is put in a task queue (a 'message queue'). This has the advantage that the PHP client request will return immediately after the data has been POSTed. Then, the PHP application could poll for the result data while Google App Engine processes the image. The PHP side would be more responsive this way.
Wouldn't it be easier to make your java app a web app, that PHP could call via an url in wich he would put the url of the image so java can download it?
like http://yourjavaserver/imageprocessing?imgurl=IMAGE_URL
and the java servlet would reply with the image file itlsef.
You can look for "java hosting" on google, to find a host for this, but it's more expensive than PHP hosting. Maybe the best choice would be to get a dedicated server which could host both PHP and java applications...
I think your best bet here is with your java app running as cron(or a deamon) that can load the file details from the database. This will require a (one or more) page-refresh on the users part after the generation is complete, at which point your script can recall the image from the database/filesystem.
I do not think you will be able to do this in real-time due to timeout restrictions on the PHP webpage. However, you could write a java applet that can take the file and process it before sending it to the server (or depending on how you intend to use it, perhaps you do not need to upload it after the transformation?).