Archive for January, 2007

PHP - Loops

Wednesday, January 31st, 2007

We use loops to repeat execution of block of code.
There are several loop types. Depending on situation, sometimes it is better to use one kind of loop than another, but every loop can be transformed into another one. This is not so important for beginners, you just need to know that there is good and better use of particular loop.

We have following loops:

for (*1; *2; *3)
{
// block
}

for loop is usually used when we know exactly how many times we need to execute repeating block of code.

for loop has three parameters. First, *1 is statement, and it is executed at the loop very start, and it often used for counter initialization. Second, *2 is a condition which is true or false. Condition is checked at every iteration start. If condition is true, execution will continue, if it is false execution will break. Third, *3 is statement and it is executed at the end of every loop iteration. It is usually used for counter increasing.

while (*)
{
// repeating block of code
}

(more…)

PHP - Data types and conversions between them

Tuesday, January 30th, 2007

PHP variables holds data. Data has a type. It is a number, text, array… There are four basic, two compound and two special types.
Basic types are integer, float, string and boolean.
Compound types are array and object,
Special types are resource and NULL.

When you are declaring variable, you can declare its type, like this:

integer $variable;
string $variable;
boolean $variable;

You can also declare variable without datatype, in which case PHP will try to determine type of data which a variable holds. Declaring variable without a type is simple, just:

$variable;

(more…)

Implementing Simple Java GZIP Utility

Friday, January 26th, 2007

This examaple shows how to implement a GZIP program in Java as a command-line utility. It is demonstrating the use of Java I/O streams and shows the simplicity of plugging one stream type into another.

(more…)

Java JDBC Example - Basic Database Operation

Friday, January 26th, 2007

This is a simple code that shows how to use JDBC. It shows how to select data from the northwind database running on localhost to which we connect using USERNAME/PASSWORD login. This is the basic database operation. INSERT/UPDATE/DELETE are even simpler since they don't require looping through the result set.

(more…)

RSS and PHP SimpleXML Example

Tuesday, January 23rd, 2007

RSS is one of the most popular applications of XML on the web. Almost every major media company exports its content as an RSS feed.

This example shows how to use SimpleXML, a feature of PHP 5, to parse an RSS feed.

(more…)

Spark it Up

Monday, January 22nd, 2007

After years of developing our app on PHP and MySQL, we hit the ceiling. The lack of frameworks and structure made further development just too costly. Adding all the new features that we have in the pipeline wasn't fun any more.

So we went to the drawing board and came up with the new platform. After researching all the technologies and frameworks out there, many of which are are providing training on, we chose Java with Spring and Hibernate frameworks.

Welcome Spark!

Marakana Spark

The new Spark platform lets us crank new features in days with a rock-solid reliability. Find out more about it - come to the next San Francisco Java Meetup and you'll get to peak under the hood.

The Way Of Web 2.0 Development

Monday, January 22nd, 2007

Want to read a really great article about how to built super useful Web 2.0 apps? Check out the free Getting Real publication by 37Signals.

This one really resonates with us and explains the basic philosophy behind Ruby on Rails platform.

PHP - Hello World Example

Wednesday, January 17th, 2007

Today, we are starting the PHP examples series. Almost every programming class, book or a tutorial starts with the Hello World example.
We are going to honor this tradition by making Hello World our first PHP example in this series.

This is your first PHP application.
As you can see, you must escape PHP code from HTML with

<? PHP Code here ?>

Another one, commonly used is

<?php PHP Code here ?>

echo is a command for printing messages.

(more…)

Java JSP - Login System Example

Wednesday, January 17th, 2007

Here is an example of a simple login system.

<html>
<body>
<form action="login.jsp" method="post">
    <h1>Please Login</h1>
    Login:    <input type="text" name="login"><br>
    Password: <input type="password" name="password"><br>
    <input type=submit value="Login">
</form>
</body>
</html>
Login JSP Page
<%
String login = request.getParameter("login");
String password = request.getParameter("password");
if ( login.equals("admin") && password.equals("guess") ) {
        // Valid login
        session.setAttribute("authorized", "yes");
} else {
        // Invalid login
        session.setAttribute("authorized", "no");
}
%>
<html>
<body>
<h1>Login Confirmation</h1>
You logged in with <%= login+"/"+password %>
<br><br>
<a href="secret.jsp">See the secret</a>
</body>
</html>
The Secret Page
<jsp:include page="secret_include.jsp"/>
<html>
<body>
<!– Assume user is logged in and good to go –>
<h1>Welcome</h1>
Here is the secret
</body>
</html>
The Code Protecting The Secret
<%
String authorized = (String)session.getAttribute("authorized");
if ( authorized.equals("no") ) {
%>
<jsp:forward page="login.html"/>
<%
}
%>

Java JSP - Simple JSP Example Example

Wednesday, January 17th, 2007

This example illustrates how to write a simple JavaServer Page. Here we implement a game of Craps:

1. Roll two dice
2. If the sum of the two is 7 or 11, you win

(more…)

Java JDBC - Fetching a Record Example

Wednesday, January 17th, 2007

This program gets a customer from a NorthWind-like database.

The example demonstrates the use of prepared statements, SQL SELECT * calls, and use of result meta info.

To run this example against a MySQL database you would do:

/path/to/java
-Djdbc.drivers=com.mysql.jdbc.Driver
-classpath .:mysql-connector-java-3.1.8-bin.jar
ShowCustomer
"jdbc:mysql://my.server.com:3306/Northwind?user=myuser&password=mypassword"

(more…)

Java JDBC - Updating a Record Example

Monday, January 15th, 2007

This program modifies a customer in a NorthWind-like database.

The example demonstrates the use of prepared statements and SQL UPDATE calls.

To run this example against a MySQL database you would do:

/path/to/java
-Djdbc.drivers=com.mysql.jdbc.Driver
-classpath .:mysql-connector-java-3.1.8-bin.jar
ModifyCustomer
"jdbc:mysql://my.server.com:3306/Northwind?user=myuser&password=mypassword"

(more…)

Java JDBC - Listing Records Example

Sunday, January 14th, 2007

This program lists customers from a NorthWind-like database.

The example demonstrates the use of JDBC statements and SQL SELECT calls.

To run this example against a MySQL database you would do:

/path/to/java
-Djdbc.drivers=com.mysql.jdbc.Driver
-classpath .:mysql-connector-java-3.1.8-bin.jar
ListCustomers
"jdbc:mysql://my.server.com:3306/Northwind?user=myuser&password=mypassword"

(more…)

Java JDBC - Inserting a Record Example

Sunday, January 14th, 2007

This program adds a customer into a NorthWind-like database.

The example demonstrates the use of prepared statements and SQL INSERT calls.

To run this example against a MySQL database you would do:

/path/to/java
-Djdbc.drivers=com.mysql.jdbc.Driver
-classpath .:mysql-connector-java-3.1.8-bin.jar
AddCustomer
"jdbc:mysql://my.server.com:3306/Northwind?user=myuser&password=mypassword"

(more…)

Java Networking - Web Server Example

Saturday, January 13th, 2007

This program demonstrates a simple multi-threaded web server.

This example demonstrates:

- Server Socket
- Reading/writing from/to sockets
- Threads
- Reading files from disk
- Regular Expression matching
- Date formatting
- Nested (inner) classes

(more…)