Tuesday, January 29, 2013

Connecting to servlet from Java client

Code as below. Hope it is easily understandable

Servlet Code:
public class MyServlet extends HttpServlet 
 public void init(ServletConfig config)
  //Servlet Init code here
     * @see HttpServlet#HttpServlet()
    public MyServlet() {
        // TODO Auto-generated constructor stub

  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub

  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws Exception
  ObjectInputStream resultStream = null;
  String results = null;
  System.out.println("Got Data......");
  resultStream = new ObjectInputStream(request.getInputStream());
  System.out.println("Got Result Stream......");
  results = resultStream.readObject();
  sendResponse(response, results);

 public void sendResponse(HttpServletResponse response, String results) throws IOException 
  ObjectOutputStream sendStream = null;
  sendStream = new ObjectOutputStream(response.getOutputStream());

Client Code:

public class MyClient 
 public String callServlet(String dataToSend)
  String responseData = "";
   URL serverURL = new URL("http://localhost:8080/MyServlet");
   URLConnection serverConnection = serverURL.openConnection();
   serverConnection.setRequestProperty("Content-Type", "text/plain");
   ObjectOutputStream out = new ObjectOutputStream(serverConnection.getOutputStream());
   ObjectInputStream in = new ObjectInputStream(serverConnection.getInputStream());
   responseData = (String) in.readObject();
   System.out.println("Records received...");
  catch (MalformedURLException e) 
  catch (IOException e) 
  catch (ClassNotFoundException e) 
  return responseData;

Friday, January 25, 2013

Getting AutoIncrement value in PHP-MySQL

It is more often than not a requirement for developers to get the value of the 'id' field back after the insert operation. PHP-MySQL has a couple of ways to do this.

The first way: use "mysql_insert_id()"

$query = "INSERT INTO tableA (id,user_id,field2, field3) VALUES (NULL, '$user_id','$field2_id','$field3_id')";
$result = mysql_query($query);
$insert_id = mysql_insert_id();

The second way: use "LAST_INSERT_ID()"
This is more useful when you have to insert the data into some other table in the same session.

Hope this helps.

N.B: Both cases are unique for the session, so there should not be any issues. But if you have multiple threads, then you might have concurrency issues.

Thursday, January 24, 2013

Wordpress - Creating a plugin

 Similar to my last post on creating wordpress templates, creating a plugin follows pretty much the same pattern of simplicity. So, lets just dive in. Here are the steps to do it.
  • Navigate to the plugins directory in your wordpress installation. [/wp-content/plugins/]
  • Create a new folder "my_plugin" in the directory
  • Create a new file "my_plugin.php" in the "my_plugin" directory
  • Type the below code in the file
        Plugin Name: my_plugin 
        Plugin URI: http://www.flexmycode.com
        Description: Test Plugin
        Author: Pramod Rao
        Version: 1.0 
        Author URI: http://www.flexmycode.com

That's all. By all means, change the content in the header.
Once this is done, if you refresh the plugins page in the admin panel in wordpress, you'll see the plugin listed below.

Wednesday, January 23, 2013

Wordpress - Creating a template

In my previous post we saw on how to create a custom child theme. In this post we'll extend our understanding to creating templates in wordpress.

When working with wordpress to create custom sites, more often than not you will encounter the need to create customized pages. Thanks to the extensibility of wordpress, this is very simple to achieve. Below are the simple steps to do this.

  • Navigate to the theme directory in your wordpress installation. [e.g. "/wp-content/themes/twentyten"]
  • Create a new file called, "my_template.php" (or whatever name you choose)
  • In the new file add the code below. 

Template Name: My Template

<?php get_header(); ?>
Welcome to my template!!
<?php get_footer(); ?>

Here, "Template Name" refers to the name that your template will be called. This will be shown to you in the admin panel while creating new pages.
Next we get the header and the footer for the site with some custom coding in between, where in you can place your HTMLs.
Simple enough and you are done. Now all you have to do is create a new page in wordpress and assign your template to it and you are ready to go!!

In the next post we will see how to create plugins in your wordpress installation.

Saturday, January 19, 2013

Wordpress - Creating a child theme

Been dabbling with wordpress these days. One of the interesting things that I came along is creating themes for wordpress. Although, I did not create a fully functional theme, I managed to create a child theme. Here's the basics of creating a child theme.
For simplicity's sake, I'll be using the TwentyTen theme that comes along with wordpress installations.

Steps to follow
  1. Create a new folder in ""wordpress/themes" directory
  2. Name it "twentytenchild". (or whatever you choose)
  3. Create a file "style.css" in that folder.
  4. Punch in the "Theme Name", "Description", "Author" and "Template" under block comments
  5. Import the twentyten stylesheet
And lo and behold. Your child theme is created.
To add custom functionality, you can start off with adding a "functions.php" in your "twentytenchild" folder. This file will extend the functions.php defined in the twentyten theme. This way you can put in your functionalities without them being erased by the updation of the original theme itself.

In the next post I'll be talking about the creation of templates in wordpress. See it here.

Friday, January 18, 2013

Shared Objects in multiple Applications

So, I had this problem of opening one Flex Application from another and passing on some data to it entirely in the client side. What hit was using SharedObject to pass around the data. The one roadblock though was that Application2 was not able to read the SharedObject created by Application1. A bit of research led to my understanding as below.

In the normal sense, when we create a SharedObject we write the code as below,
var so:SharedObject = SharedObject.getLocal( "mySO" );

What the above line does is that it reads the SharedObject named "mySO" from the file "mySO.sol" placed in the local system. If the file does not exist, then it creates a new one with the same name.

In Windows 7, you can find the file in the following file path (This may vary based on the OS)
C:\Users\Pramod\AppData\Roaming\Macromedia\Flash Player\#SharedObjects\TC6AK52F\localhost\work\myApp\flexbin\Application1.swf\mySO.sol

The problem what happens when Application2 tries to access mySO is that, it actually is looking into another folder to get the SharedObject and not the above one. So the path in which Application2 tries to access is (Again, this will vary based on the OS)
C:\Users\Pramod\AppData\Roaming\Macromedia\Flash Player\#SharedObjects\TC6AK52F\localhost\work\myApp\flexbin\Application2.swf\mySO.sol

To get around this, what we can do is create a SharedObject in a common location that both the applications can access. We do this by writing code as below,
var so:SharedObject = SharedObject.getLocal( "mySO", "/" );

The above code creates/reads a SharedObject from the following location
C:\Users\Pramod\AppData\Roaming\Macromedia\Flash Player\#SharedObjects\TC6AK52F\localhost

So now, both the applications can read the SharedObject. Hope this helps.

Tuesday, January 15, 2013

WAMP MySQL Error 1045

I have just started dabbling with wordpress. So as a first step I decided to set it up in my local system with WAMP server. Pretty surely, as soon as I set it up I ran into the mysql issue when I tried to open the phpmyadmin console in the web browser.

 #1045 - Access denied for user 'root'@'localhost' (using password: NO)

 #1045 - Access denied for user 'root'@'localhost' (using password: NO)

I figure this is a common error when setting up, but it surely wasted a lot of my time.
Here are the common steps that one can go through to fix this,

  1. Open MySQL console and login to MySQL. If you are able to login, then ensure that the password that you used to login is updated into "wamp/apps/phpmyadmin/config.inc.php".
  2. Restart WAMP server
  3. Delete all files in "wamp/tmp"
  4. Delete all cookies from your browser
After these steps try opening the phpmyadmin console again. Hopefully you won't have the same error again.