Archive for the ‘Examples’ Category

Marakana Launches Free Tutorials

Monday, January 21st, 2008

We have opened some of our tutorials that was previously available only to paying participants for use by the general public. The new FREE Tutorials section features some quick overviews on topics such as Java, J2EE, XML, XSL, and other. Here's the list of the initial few:

Hope you enjoy it!

Testing with TestNG

Friday, January 4th, 2008

When asked about automated unit testing in Java, most developers first think of JUnit. Without a doubt, JUnit is still the de facto standard when it comes to test-driven development in Java, and is arguably the most popular/successful in the xUnit family of frameworks created by Kent Beck, and (in case of jUnit) Erich Gamma, the co-author of the very influential Design Patterns book (one of GoF).

Yet, despite all the popularity and the success behind jUnit, the open source community chose to embrace yet another automated testing framework for Java called TestNG. Created by Cedric Beust and Alexandru Popescu in 2004, TestNG tries to address many of the shortcomings of jUnit outlined here and here.

Of course, the jUnit camp has not stood still since 2004, and it caught up to TestNG with many of the features introduced in version 4.0, most notably the annotation-based API. Yet, TestNG is still considered superior to jUnit is the areas of flexibility, configuration-driven-testing, dependency-driven-testing, reusability of test cases through parameters, and efficient/quick rerunning of failed and skipped tests. Also, TestNG is not just limited to unit-testing, since it can also serve well as an all-around testing framework for integration and system testing.

For those with a significant investment in jUnit, TestNG folks even provide a simple Java utility to help with the migration from jUnit.

Filippo Diotalevi from IBM wrote a great TestNG example that outlines some of the main principles behind the framework as well as the @Test, @Before, @After annotation syntax that is at the heart of TestNG. It's a great read.

Update: if you live in the Bay Area, you can hear me talk about TestNG at the next event of The San Francisco Java Meetup on February 11th, 2008 at our classroom in the city. I hope to see you there!

Securing Java web applications using FORM-based container-managed security

Tuesday, November 20th, 2007

This is a simple example on how to secure a Java web application (regardless of whether it is based on Servlets, JSPs, Struts, Spring, JSF, or any other combination) using just simple FORM-based container-managed security. True, other, much more sophisticated security frameworks exist, but this is by far the simplest way to wrap a security layer around an application regardless of its underlying implementation.

We also demonstrate how to authenticate users against a relational database (in this case we used MySQL).

The example application shown below was tested on Tomcat, but the principles should work on any modern Java EE servlet container.

(more…)

PHP - How to Read RSS

Tuesday, June 12th, 2007

Here's an example of how easy it is to parse RSS feeds using SimpleXML. This particular code reads the RSS of the upcoming Marakana public courses. You can use it on your website.

<?php
                // The List of Public Courses across all Locations
                $feed = simplexml_load_file( "http://marakana.com/training/schedule.rss" );
               
                foreach( $feed->channel->item as $item ) {
                        echo '<a href="', $item->link, '">', $item->title, "</a><br/>\n";
                }
       
        ?>

Go ahead, try it!

Marko

JavaScript OOP

Wednesday, March 7th, 2007

As JavaScript is gaining in popularity due to Ajax, Object-Oriented Programming is becoming more and more important. If you are coming from a language such as Java, you'd have some difficulty grasping the JavaScript object model. Perhaps because there isn't one, really. But there's a way to accomplish most of the design patterns that you are accustomed from a real OOP language.

Let's look at this one item at a time.

(more…)

PHP - Online Quiz Game

Tuesday, February 27th, 2007

This is a ready to go example. Basically, it enables you to create online quizzes and validate them, displaying number of guessed answers. Since it is written in object oriented manner, it is easy to extend its capabilities.

Example uses three DB tables and seven classes.

DB tables are:

quiz_quizzes
quiz_questions
quiz_answers

Relationship between quiz, question and answer is: quiz have one or more questions, and each question have one or more answers. Only one answer is correct. So, each quiz have unique QuizID. Each question have unique QuestionID and it is connected with quiz through its QuizID. Each answer have unique AnswerID and it is connected with question through its QuestionID.

(more…)

PHP - DB authentication system

Tuesday, February 27th, 2007

Here you have an example of a pretty complete and "ready to go" class for user authentication system. Although the class is user based, there are several methods not quite about users, but these are helpers for quick start.

DBConnect - connects to database;
DBDisconnect - disconnects from database
CreateSecurityUsersTable - creates table for storing usernames and passwords
GetUsers - creates an array with users info
Insert - creates new user.
Update - modifies user info
Delete - deletes user
ChangePassword - updating password is separated from Update
CheckLoginCredentials - check username and password
GetUserInfoByName - gets user info by provided name
GetUserInfoById - gets user info by provided user id

(more…)

PHP - Turing test

Tuesday, February 27th, 2007

Turing test is a small image with random text which user should enter in order to process his further requests. It is based on presumption that text on image could be recognized only by humans. This is the reason why you will find these images very distorted. Basically, web server will create image background, simple text and apply image distortion with mathematical function. This way, web server makes impossible to use OCR software to recognize text on the image.

(more…)

PHP - Image filters

Tuesday, February 27th, 2007

One of many advantages of PHP5 over PHP4 is ability to process images with various image filters.

According to PHP manual, you can
1. Convert image to negative
2. Convert image to grayscale
3. Set the brightness of an image
4. Set the contrast of an image
5. Colorize image. This filter will change color saturations in your image.
6. Edge detection. This filter will highlight the edges of objects on image
7. Emboss. Filter will create a relief of image.
8. Blur. Blurs the image. This filter have two variations, Gausian blur and Selective blur.
9. Mean removal. This filter will make your image looks like a sketch.
10. Smooth. Filter will make object edges transitions smoother.

(more…)

PHP - Protected download

Thursday, February 22nd, 2007

In this example, we are protecting a file from an unauthenticated download.
The script will ask for username and password, and if it is OK, it will send a file. Notice, that we do not give file URL. We are sending file through PHP script, so the real location of the file stays secret for the outside world. The downside is that a user cannot continue an interrupted download.
Depending on the server configuration, you may not have sufficient permissions to use mime_content_type (it is disabled by default). If this is a situation, just hardcode your MIME type.

(more…)

PHP - Sessions

Thursday, February 22nd, 2007

Sessions allow us to save user "state" between subsequent visits to the page. Sessions and cookies are similar mechanisms. Both allow us to save data, which will be available to us at a later visit. Main difference is that session data is stored on a web server, while cookies resides on client machines. Because of this, sessions are capable of handling much more data than cookies.

Sessions work in the following way: when a user visits your web page, he stars a session and gets assigned a unique ID. This ID is called Session ID (SID). SID is sent to a user using a cookie.

(more…)

PHP - Compressing a file

Thursday, February 22nd, 2007

This example shows how to create gz archive.

<?
  $fp = fopen("/file.dat", "r" );
  $content = fread ($fp, filesize("/file.dat"));
  fclose($fp);
  $fp = gzopen("/file.gz", "w9" );
  gzwrite($fp, $content);
  gzclose($fp);
?>

PHP - Downloading a file

Thursday, February 22nd, 2007

This example will show you how to download a file from a remote location.
First we will open a connection to remote host. We will use fopen function to do that. You could also use functions from sockets module if you want more power to control socket behavior. When you open a connection with fopen, you can read and write to that connection just like from/to a file. fopen is good because it is easy to use.

(more…)

PHP - Mailing list

Friday, February 16th, 2007

The following is an example of a class which can be used to handle a mailing list.
Everything is bundled into one class, for your quick start.

Commands are sent through GET requests, like this:

emaillist.php?command

Available commands are:

create - creates db table for storing addresses
list - lists all addresses in database
unsubscribe - displays unsubscribe form
write - displays form for writing messages

There are command which can be sent through POST request, and they are used through available forms.

(more…)

PHP - Sending messages with attachements

Friday, February 16th, 2007

This one is little tricky. We need to form the message in a special way.
Our message will be divided in at least two parts, one for the message and one for the attachment.
This is a so called MULTIPART message.
These two parts need to be separated, so we need a BOUNDARY between them. We will form the message by reading the content of the attachment and then converting it so that it can be transfered by email.
In the following example, we will send an HTML message with an attachment.
(more…)