Java creating server that is ran as one file with database? - java

I have a program that I've created that is meant to poll an html internal page with different IPs that update and then will run a telnet session to those IPs to see if the device still has a connection... I'm attempting to challenge myself in creating something further with a dynamic webpage instead of my program spitting out console output...
My Issue:
I dont know what technologies / libraries Java has to execute such things
I want:
A Local Server, to upload a page LOCALLY only (no security is needed as this will be strictly intranet)
My program to implement: A database of sorts to save "logs" essentially that a certain IP / device has had successful connections
in the past....maybe stored to an external file is fine i presume (my
program currently has to re-poll everytime i run it.. i want some kind
of "remembering"..
Is it possible this can all be done in one file? so if i want my computer to run this as soon as it starts up... it will run... grab
its current state of the database of IPs... poll them (periodically)
and then persist and save and update the HTML page dynamically....
I hope i'm being as descriptive as possible... Its a bit of an abstract.. I really just want some introduction to different libraries ... a friend recommended stuff like MongoDB or something but I want to stay strictly to Java programming

Related

Synchronous use of database insertion

I have created a java application that is inserting data to a mysql database. Under some conditions, i need to post some of these data via email, using a java application that I will write as well.
My problem is that i am not sure how i should implement this.
From what I understand, I could use a UDF inside MySql to execute a java application, for which there are many against opinions into using it. Let alone that both the database and the mail client application will reside in a VM that i dont have admin access, and dont want to install anything that neither me nor the admin knows.
My other alternative, that I can think of, is to set up the mail client (or some other application), to run every minute just to check for newly inserted data. Is this a better aproach? Isn't it going to use resources for doing almost nothing. At the moment the VM might not be heavily loaded, but i have no idea how many applications there might end up running on the same machine.
Is there any other alternative i should consider using?
You also need to consider the speed of internet, database server load, system resources. If you have enough memory and less load to insert data in databases or database load is not so much then you can approach this by cron setup. For linux call a script for every 5 minutes. The script perform the following-
1. Fetch unread Emails as files
3. Perfrom shell script to read needed data.
3. write data to mysql
4. Delete the email
If you have heavy loaded system then wise you need to do this once or twice in an hour or may vary.

Using a server for a turn game (Android) - how to store data?

I am writing a simple turn game in Android for two players. I already developed an offline version (when two persons exchange the phone) and now want to create an on-line one.
They way I wanted to do it was to use Tomcat or my own PHP server for my website. My question would be whether it is possible and how to store data?
I think that the only thing I need to send to the server is the current score and an array representing the board. Then I'll be able to retrieve these pieces of information on client's side. Am I thinking properly?
I gotta say, I am completely new to all this stuff. I talked to my friend who programs in Java and he told me that I should use Tomcat. But how? It is a local server so the only way it could work would be when two mobile phones are connected to the same network or what?
The simplest way would be to implement a set of RESTful services on an online server.
You can use Tomcat if you want, but the cheapest approach would be to have a simple web hosting with i.e. PHP.
Create REST API's like /get/board, /get/move, /update/move, /authenticated and so on.
The simplest way to start out is probably not even with any real service, but just a couple of PHP scripts and an MySQL database.
You'll need some server, per example just a simple FTP server where you can put some on-the-internet reachable scripts on, and host the (MySQL) database.
Then per example for keeping the score you'll just need 2 scripts : 1 that saves and 1 that retrieves the score of a player.
Saving the score will just demand you to call on a link as per example:
www.server.com/saveScore.php?playerId=111&score=60
In the PHP you could just take this out with $_GET['playerId'] and $_GET['score'] and save those values in to your database with a query.
Returning the score to your app could work out with calling a link as:
www.server.com/retrieveScore.php?playerId=111
Which will simply query your database for the info on that player, and then print it as XML or JSON.
Then your app can just parse the response (xml/json) again.
A good idea would be to move this from $_GET to $_POST (POST is not visible in the link, but you'll need a bit more complicated code to transmit the data through that) once it works.
Once you've got that going you can work on actually building a service, REST would be a good choice, in order to increase maintainability, extendability and security.

How can I restrict access to the php file on server to Java app only?

I have a Java application that sends user score to the mysql table. When the user is done, Java app accesses the .php file on server and the .php server performs a query on the database (inserting score).
I am concerned about the (in)security of this method. I mean, if someone finds out the direct url to the .php on a server, they can produce a lot of mess in the dabase. Can you advise how I could prevent the .php from executing the query other than accessed by the Java app?
edit: The problem is that Java application is NOT run on the server, it's run on the user computer using Java Web Launcher platform. So it's not an applet...
The problem is conceptual. You should never be sure that users can't find out the real address (security by obscurity). You could use SSL, still this is no means against a good guess.
Since the Java program is run on the client side, a .htaccess restricting access to a certain IP is also not an option.
My suggestion is to create a separate user in mysql, grant this user access only to necessary tables and perform the database queries on behalf of this user directly in Java. This way all data is encrypted (see http://dev.mysql.com/doc/refman/5.5/en/ssl-connections.html) and no URL/access point is exposed. Of course it means your MySQL server must be reachable from outside which poses a risk, too. You should have a good root password!

making several request to a server with java without getting banned?

I don't if the question title fits, but here is my problem:
I have a regular webhosting service in hostmonster, with a website built in php.
So I have php script running in a cron job that monitors a xml file for changes, and everytime a new entry comes into that xml file the script stores it in a database.
In the other hand there is java built desktop client, which needs to be noticed ASAP that a new entry is created, for this the client connects to a second php file every second, and this second files tells if there has been changes or not.
The thing is, every 260 connections my I.P gets banned from the server :( and the client crashes, the client will be used by several users.
I contacted support on how to handle this, but they tell me to use a single connection, I tried reusing the UrlConnection but after the first request it just gives null. then I tried with Sockets but no luck. I know there are libraries that manage this but I dont know how are they called. Can someone give me advice?
thank you guys.
Use a long polling method. Hold the connection opened until response arrives. This way you only need to ask for the update once.
PHP may not be the best tool for this job though.

Print service on java web application

I have an Enterprise Java web application that all of the business is migrated from client-server application. So I need to adapt all requirement including printing on every client (Using Dot Matrix / Plain text).
Printing requirement for client on separated client is they must get the report right away after the process is finished, let say print some system information. On Client-server is easy just for printing, but when I migrate to web apps, we used Server/applet service that embedded to the apps. So the applet will be initialized when the client open the browser.
It works, but too many issues. Sometime the applet is closed suddenly without action from client, so automatically the applet will be failed for checking the queued report from database. Is there any suggestion for this case?
I use Spring Framework as a business layer.
I had the same problem years ago.
This is what I did.
Since my processing was taking place on the server, I knew when the transaction was finished. So what I did was to send the print request directly from the server. The client printer was configured in the server and since it was used in the LAN it could be easily reached. That way I finished with all the problems I had with the applet, the tradeoff was they can only print in the office, but in my situation that was fine, not necessarily applies for you, give it a try.
UPDATE
In my case, the print was needed by different departments along the country.
Each department had an specific, unique printer for that task. So what I did was have that printer mounted in the server filesystem ( in Unix something like /Volumes/printers/EPSON-12345 , in Windows as x:\printers\EPSON-12345 ) When the user finished the transaction in the webapp I start in the server a print request. Java does not have any problem at all to print to a local printer and since a mapped printer is taken as local ( even though it is a remote one ) you don't need further authorizations or anything special.
Since I knew what the specific printers were mapped, and I knew what kind of transaction that was, I knew where to print.
Finally I use a property file to allow the printer to change from time to time and/or add more printers.
I don't quite remember the details but it was something like this in the server:
ResourceBundle bundle = ResourceBundle.getBundle("printers");
String serviceName = bundle.getString("MEXICO.CITY.PRINTER");
PrintService service = PrintService.find( serviceName );
service.print( // document and etc );
But that was almost 4 yrs. ago so I don't remember exactly the details, but my program is still running :)
You can read more about the printing service in java here. It is way much simpler than what it look like.
Other ways to achieve a good result:
generate a PDF (using iText, or better JasperReports) on the server, and send it for download to the client, where he can print it.
have a print.css, and generate a web-page that is suitable for printing.
If you do not want the client browser to orchestrate the printing (i.e. by sending a binary blob with a custom MIME-type which the user must set up to be sent to the printer), you must let your server do it.
This usually means that the user must have a default printer attached to their profile, and that the server knows how to send prints to that printer. Lots of fun :)

Categories