Sending Simple Data From Java to PHP via a POST Request


So the software I am developing in my leisure time is about to complete. Past weekend I worked on it's security aspects and implemented many things like encryption and encapsulation of byte code, Encryption of POST data, Generation of unique ID for each system using my software and the licensing part. I will try to explain each part in my coming posts.

Earlier I demonstrated How to display Splash Screen in Java. Today I will show how I am sending simple data from my Java code to my PHP page on Web server.

 package www.isumitjha.com;  
 import java.io.BufferedReader;  
 import java.io.IOException;  
 import java.io.InputStreamReader;  
 import java.io.OutputStreamWriter;  
 import java.io.PrintStream;  
 import java.net.HttpURLConnection;  
 import java.net.MalformedURLException;  
 import java.net.URL;  
 import java.net.URLConnection;  
 import java.net.URLEncoder;  
 class Test {  
  public static void main(String args[]) {  
  try {  
   // opening a connection to the site  
   URL url = new URL("http://api.isumitjha.com/example.php");  
   HttpURLConnection con = (HttpURLConnection) url.openConnection();  
   // Setting the DoOutput flag true for using output   
   con.setDoOutput(true);  
   con.setRequestMethod("POST");  
   // Encoding the POST data  
   String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("Hello", "UTF-8") + "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("World", "UTF-8");  
   OutputStreamWriter osw = new OutputStreamWriter(  
   con.getOutputStream());  
   // Sending parameter to site  
   osw.write(data);  
   osw.flush();  
   con.getInputStream();  
   System.out.println("RESPONSE CODE = " + con.getResponseCode());  
   BufferedReader in = new BufferedReader(new InputStreamReader(  
   con.getInputStream()));  
   String line = null;  
   while ((line = in .readLine()) != null) {  
   System.out.println(line);  
   }  
  } catch (MalformedURLException e) {  
   e.printStackTrace();  
  } catch (IOException e) {  
   e.printStackTrace();  
  }  
  }  
 }  

Now let's see the simple PHP code that prints all value received via POST request.

 <?php  
 foreach ($_POST as $key => $data) {  
   echo $data." ";  
   }  
 ?>  

So this is how I am sending data to my server. In my next post I will show you how to encrypt POST data using Advanced Encryption Standard and receive response from server in JSON format.

I hope you enjoyed reading :)

How to display Splash Screen in Java



We all know the importance of user interface in any commercial as well as non commercial software. Today while working on one of my project I felt that the loading time of my application is quite longer than usual.

The reasons behind  longer loading time are the loading of configuration file and downloading of resources from web. Here splash screen is the requirement for this situation.

 Although there are many ways to implement splash screen in java but I prefer the built in API for splash screen. Let's see how to use this native Splash screen API for your application.

Java SE 6 provides a solution that allows the application to display the splash screen much earlier, even before the virtual machine starts. A Java application launcher is able to decode an image and display it in a simple non-decorated window.

Below is the sample Java Swing application that do some time consuming task before initializing UI.

Copy and save below code in file  MySplashScreenDemo.java

 package com.decryptoniter.splash;  
 import java.awt.FlowLayout;  
 import javax.swing.*;  
 public class MySplashScreenDemo {  
  MySplashScreenDemo() {  
  // some time consuming task before UI initialization  
  someTask();  
  // UI will initialize now   
  initUI();  
  }  
  void someTask() {  
  System.out.println("Some time consuming task is executed");  
  try {  
   Thread.sleep(10000);  
  } catch (Exception e) {  
   e.printStackTrace();  
  }  
  }  
  void initUI() {  
  JFrame frame = new JFrame("Splash Screen Demo");  
  JPanel panel = new JPanel();  
  panel.setLayout(new FlowLayout());  
  JLabel label = new JLabel("www.isumitjha.com");  
  panel.add(label);  
  frame.add(panel);  
  frame.setSize(300, 300);  
  frame.setLocationRelativeTo(null);  
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  frame.setVisible(true);  
  System.out.println("UI initialized");  
  }  
  public static void main(String s[]) {  
  MySplashScreenDemo ss = new MySplashScreenDemo();  
  }  
 }  

Now compile MySplashScreenDemo.java:
javac -d . MySplashScreenDemo.java

Run this application: java com.decryptoniter.splash.MySplashScreenDemo

You will notice that it takes 15 seconds to load the UI after running the application. Now to display splash screen while UI is not loaded we can use java command line argument.

Display a Splash Screen using the Command-Line Argument 

To display a splash screen from the command line use the -splash: command-line argument. This argument is a Java application launcher option that displays a splash screen:

java -splash:<file name> <class name>

For our application use. java -splash:loading.gif  com.decryptoniter.splash.MySplashScreenDemo

Now you can see the splash screen as soon as application is run while UI is not loaded.

Note: The splash screen can display any gif, png, or jpeg image, with transparency, translucency, and animation.