13 December 2011

Background Process with Servlet

ที่ใส่ with servlet ไว้ด้วยในหัวเพื่อบอกว่าผมยังจะ extends HttpServlet เหมือนเดิมในคลาสผม แต่ว่าจะทำให้มันทำงานอยู่แม้จะไม่ได้มีรีเควสเข้าไปที่ Servlet ตัวนั้นก็ตาม โดยสิ่งที่ผมจะทำคือการ implement Runnable เข้ามา ตามข้างล่าง
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(
 name="TestRunnable",
 urlPatterns={"/TestRunnable"},
 loadOnStartup=1
 )
public class TestRunnable extends HttpServlet implements Runnable {

}

09 December 2011

Abstract Class VS Interface

Abstract Class
abstract class เป็นคลาสที่จะมี abstract method อยู่ด้วยหรือไม่ก็ได้

abstract class Test{
 public String a(){
  return "hello";
 }
 public abstract String b();
}

06 December 2011

GenericServlet VS HttpServlet

javax.servlet.GenericServlet
Signature : public abstract class GenericServlet extends java.lang.Object implements Servlet, ServletConfig, java.io.Serializable

- GenericServlet จะประกอบไปด้วย protocal พื้นฐานใน Servlet
- GenericServlet จะมีต้นแบบ(พิมพ์เขียว) สำหรับการใช้งาน Servlet
- Lifecycle ของ GenericServlet จะอยู่กับ init และ destroy และ method ใน ServletContext interface
- GenericServlet สามารถทำงานผ่านการ Override service method

javax.servlet.http.HttpServlet
Signature : public abstract class HttpServlet extends GenericServlet implements java.io.Serializable

- HttpServlet จะประกอบด้วย protocal Http เท่านั้น
- HttpServlet จะมีต้นแบบ(พิมพ์เขียว) สำหรับการใช้งาน Http
- HttpServlet extend มาจาก GenericServlet
- HttpServlet สามารถใช้ session และ cookie ได้
- HttpServlet ทำงานผ่าน doGet() doPost() doXXX()

01 December 2011

เรื่องของ Path

path คือการอ้างถึงตำแหน่งเส้นทางโดยในการเขียนโปรแกรมนั้น path คือการเรียกตำแหน่งต่างๆของโปรแกรม หรือ sub folder โดยจะมี

Relative path เป็นการเรียก path แบบมีความสัมพันธ์กัน เช่น
เรามีโปรแกรมอยู่ c:\myapp\app.exe แล้ว app.exe ทำการเรียก path ./data/data.txt นั้นก็หมายความว่าโปรแกรมเรียกไปที่ c:\myapp\data\data.txt

Absolute path คือการกำหนดชื่อ path แบบเต็มโดยจะบอกตั้งแต่ root เช่น
c:\myapp\data\data.txt

09 November 2011

Event Driven Programming

  

Event Driven Programming เป็นแนวคิดในการเขียนโปรแกรมที่แตกต่างจากการเขียนโปรแกรมเบื้องต้นที่คุ้นเคย โดยการเขียนโปรแกรมที่เราต้องมองการทำงานให้ทะลุปุโปร่งหรือให้เข้าใจทุกลำดับการทำงานของโปรแกรมก่อนแล้วค่อยเขียนโปรแกรมควบคุมการทำงาน เราเรียกวิธีการเขียนโปรแกรมแบบนี้ว่า Structure Programming ซึ่งใช้ได้ผลดีหากโปรแกรมนั้นมีขนาดเล็ก และง่ายต่อการทำความเข้าใจลำดับขั้นตอน

แต่ถ้าหากเป็นโปรแกรมขนาดใหญ่ที่มีความซับซ้อนแล้วการเขียนโปรแกรมแบบ Structure Programming นั้นคงเป็นเรื่องยาก ซึ่ง Event Driven Programming ก็คือแนวคิดที่มาจัดการด้านนี้โดยแนวคิดนี้จะสนใจแค่ว่าถ้าเกิดเหตุการณ์ขึ้นมาแต่ละอย่างจะจัดการกับมันอย่างไร โดยไม่สนใจว่ามีอะไรเกิดขึ้นกับโปรแกรมบ้าง ทำให้สามารถโฟกัสไปยังสิ่งที่ต้องการจัดการและจัดการมันได้อย่างง่าย....

โดยถ้าหากเราต้องการแก้ไขระบบเราก็ไม่ต้องไปสนใจส่วนที่ไม่เกี่ยวข้อง แต่ถ้าหากเป็น Structure Programming อาจจะต้องคิดใหม่หมด หรือถึงขั้นรื้อระบบใหม่กันเลยทีเดียว....

14 June 2011

Change Computer Name On Ubuntu Desktop


Step 1. run gksudo gedit /etc/hostname /etc/hosts
Step 2. Change your computer name in two files.
Step 3. Restart.

02 May 2011

HTML::Template 2


 <tmpl_if> กำหนดเงื่อนไขใน template
if.pl
#!c:\perl\bin\perl.exe
use HTML::Template;
my $tmpl = HTML::Template->new(filename=>'tmpl/if.tmpl');
$tmpl->param(bool=>1);
print "Content-type:text/html\n\n",$tmpl->output();

if.tmpl
<html>
<head>
    <title>if</title>
</head>
<body>
<tmpl_if name="bool">
  is true
<tmpl_else>
  is false
</tmpl_if>
</body>
</html>

<tmpl_unless> ใช้งานเหมือนกับ <tmpl_if> แต่เป็นเงื่อนไขที่ตรงกันข้าม(opposit)

HTML::Template

ไฟล์ที่เป็น template นั้นจะใส่นามสกุลไว้ว่า .tmpl โดยต้องใช้ HTML::Template โหลดขึ้นมา

<tmpl_var> เป็นการส่งค่ามาแสดงใน template

test.tmpl
<html>
<head>
    <title>Test Template</title>
</head>
<body>

My Home Directory is <tmpl_var name=home>
<br />
My Path is set to <tmpl_var name=path>

</body>
</html>

test.pl
#!c:\perl\bin\perl.exe
use HTML::Template;
use CGI::Carp qw(fatalsToBrowser);

#open html template
my $tmpl = HTML::Template->new(filename => 'tmpl/index.tmpl');

#fill parametors
$tmpl->param(home => $ENV{HOME});
$tmpl->param(path => $ENV{PATH});

#send
print "Content-Type:text/html\n\n",$tmpl->output();

<tmpl_loop> ทำลูปใน template

test.tmpl
<html>
<head>
    <title>Loop Template</title>
</head>
<body>
<tmpl_loop name=emp>
    <tmpl_var name=name> -- <tmpl_var name=job><br>
</tmpl_loop>

<tmpl_loop name=loop2>
    <tmpl_var name=var1> -- <tmpl_var name=var2><br>
</tmpl_loop>
</body>
</html>
test.pl
#!/perl/bin/perl.exe
use HTML::Template;
use CGI::Carp qw(fatalsToBrowser);

my $tmpl = HTML::Template->new(filename=>'tmpl/loop.tmpl');
$tmpl->param(emp => [{name=>'test1',job=>'unknow'},{name=>'test2',job=>'programer'}]);

my @var1 = qw(who am i);
my @var2 = qw(set to me);

my @loop_data = ();

while(@var1 and @var2){
    my %row_data;
    $row_data{var1} = shift @var1;
    $row_data{var2} = shift @var2;
   
    push(@loop_data,\%row_data);
}
$tmpl->param(loop2=>\@loop_data);

print "Content-Type:text/html\n\n";
print $tmpl->output();

<tmpl_include> ดึงเอา template อื่นมา
include.pl
#!/perl/bin/perl
use HTML::Template;
use CGI::Carp qw(fatalsToBrowser);

my $tmpl = HTML::Template->new(filename=>'tmpl/include1.tmpl');

print "Content-Type:text/html\n\n",$tmpl->output();


include1.tmpl
<html>
<head>
    <title>include</title>
</head>
<body>
from include1.tmpl<br>
<tmpl_include name="include2.tmpl">

include2.tmpl
from include2.tmpl
</body>
</html>

01 May 2011

Compiler Directive Module


ใจการสร้าง module ขึ้นมานั้นเราสามารถเลือกเฉพาะบาง subroutine หรือตัวแปร ให้ทำงานได้โดยใช้การทำ Compiler Directive ซึ่งโครงสร้างคร่าวๆจะเป็น

##############################
package modulename;
use Exporter;

#@ISA จะทำหน้าที่เก็บโมดูลที่ inheritance ถ้าไม่เจอใน package จะทำการค้นหาจาก ตัวแปรนี้
@ISA = qw(Exporter);

#เป็น default load เมื่อเรียกใช้โมดูล subroutine ใน @EXPORT จะสามารถทำงานได้
@EXPORT = qw(function $variable @arrayVar %hashVAr);

#ถ้าต้องการใช้ต้องส่งพารามิเตอร์ ตอนโหลดโมดูล
@EXPORT_OK = qw(functionTwo $var);

#เหมือน @EXPORT_OK แต่ทำเป็นกลุ่มเอาไว้
%EXPORT_TAGS = qw('name'=>[qw(function functionTwo)]);
##############################

ตัวอย่าง
package DModule;

use Exporter;

@ISA = qw(Exporter);
@EXPORT = qw(fnc1);
@EXPORT_OK = qw(fnc2);
%EXPORT_TAGS = ('all'=>[qw(fnc1 fnc2)]);

sub fnc1
{
print "fnc1 print
";
}

sub fnc2
{
print "fnc2 print
";
}

1;

วิธีการเรียกใช้
#!c:\perl\bin\perl.exe

print "content-type:text/html\n\n";
use DModule qw(:all !fnc2); #โหลดฟังก์ชั่นจาก %EXPORT_TAGS ยกเว้น fnc2

fnc1();
fnc2(); #ฟังก์ชั่นนี้จะไม่ทำงาน

การโหลดแบบอื่น
:DEFAUL เป็นค่าที่บอกว่าให้โหลดจาก @EXPORT
use DModule qw(:DEFAULT fnc2);
fnc1 และ fnc2 สามารถถูกเรียกใช้ได้

อ้างอิง
http://docstore.mik.ua/orelly/perl/prog3/ch12_05.htm
http://world.std.com/~swmcd/steven/perl/module_anatomy.html
http://perldoc.perl.org/Exporter.html

30 April 2011

หนังสือน่าอ่าน The Underground PHP and Oracle Manual


โหลดได้จาก oracle เลย : http://www.oracle.com/technetwork/topics/php/026973.pdf

GET/POST Method in Perl


module สำหรับ ใช้ get กับ post แยก subroutine กันนะครับ

DataParam.pm
package DataParam;

sub new
{
my $class = shift;
my $self = {};
bless($self,$class);
return $self;
}

sub GetParam
{
my $self = shift;
my $params = $ENV{'QUERY_STRING'};
my $data = {};
foreach (split(/&/,$params))
{
my ($key,$value) = split(/=/,$_);
#### บรรทัดล่างเอาไว้แปลงค่าอักษรที่เป็น % นำหน้า
$value =~ s/%(..)/chr(hex($1))/ge;
$data->{$key} = $value;
}
return $data;
}

sub PostParam
{
my $self = shift;
my $params;
read(STDIN,$params,$ENV{'CONTENT_LENGTH'});
my $data = {};
foreach (split(/&/,$params))
{
my ($key,$value) = split(/=/,$_);
#### บรรทัดล่างเอาไว้แปลงค่าอักษรที่เป็น % นำหน้า
$value =~ s/%(..)/chr(hex($1))/ge;
$data->{$key} = $value;
}
return $data;
}

1;

วิธีการใช้งาน
index.pl
#!c:\perl\bin\perl.exe

use strict;
use CGI;
use DataParam;

my $param = DataParam->new();
my $GET = $param->GetParam();
my $POST = $param->PostParam();

my $cgi = CGI->new();
print $cgi->header("text/html");

################## xxx กับ yyy คือชื่อตัวแปรที่ส่งมาครับ
print $POST->{'xxx'};
print $GET->{'yyy'};

29 April 2011

create Perl Module


perl module จะมีนามสกลุเป็น .pm โดยด้านบนต้องระบบ package เอาไว้

Test.pm
package Test;

sub new{
$class = shift;
$self = {};
bless($self,$class);
return $self;
}

sub TestFunction{
my $self = shift;
return "hello my module";
}

new subroutine นั้นเป็นคอนสตัคเตอ ของโมดูลต้องประกาศไว้ด้วย
ต่อจากคอนสตัคเตอร์ก็เป็นฟังก์ชันของเรา เวลาเรียกใช้งานก็ใช้ use ตามปกติ

myfile.pl
#!c:\perl\bin\perl.exe
use Test;

print "content-type:text/html\n\n";

$data = Test->new();
print $data->TestFunction();

Regular expression


$result = "is somthing";

if($result =~ /something/){
print "result มีข้อความ something\n";
}

ดู regular expression เพิ่มเติม http://www.troubleshooters.com/codecorn/littperl/perlreg.htm

Logical Operator





&& ถ้าหากด้านซ้ายเป็นจริงจะทำงานด้านขวา

|| ถ้าด้านซ้ายเป็นเท็จจะทำงานด้านขวา
// ถ้าด้านซ้ายไม่ได้กำหนดค่าทำด้านขวา

ตัวอย่าง

$x = 0;
$y = $x && 5;
$y จะมีค่าเท่ากับ 0

$x = 1;
$y = $x && 5;
$y จะมีค่าเท่ากับ 5

$x = 0;
$y = $x || 5;
$y จะมีค่าเท่ากับ 5

$x = 20;
$y = $x || 5;
$y จะมีค่าเท่ากับ 20

$y = $x // 5;
$y จะมีค่าเท่ากับ 5

String Operator

การเชิื่อม string กับ ตัวแปร
print "string".$x."=".$y."\n";

print repeat
print "-" x 20;
ผลลัพธ์จะได้ - ต่อกัน 20 อัน

Syntax Condition Test

Test Condition,Excute Code
if($x >= 0){
print "x is positive number\n";
}

Defind code,Test Condition,Excute
print "x is positive number\n" if($x >= 0);

Excute code,Test Condition,Excute Code
($x >= 0) && print "x is positive number\n";

28 April 2011

hash variable

ตัวแปร hash ใน perl จะขึ้นต้นด้วย % ซึ่งสามารถกำหนดค่าโดย

%hash = (key1,value1,key2,value2,.....);

%hash = (key1 => value1,key2 => value2,...);

การแสดงค่าของ hash

$hash{key};

แสดงเฉพาะ key ที่อยู่ใน hash

@allKey = keys %hash;

แสดงเฉพาะ value ที่อยู่ใน hash

@allValue = values %hash;

loop hash

while(keys %hash){
print $_."=".$hash{$_};
}

for(values %hash){
print $_;
}

array in perl

array ใน perl จะใช้เครื่องหมาย @ นำหน้าตัวแปร

@array = (value1,value2,value3,...);

โดยสามารถแสดงค่าของแต่ละ index โดยใช้

$array[number]; # number หมายถึงตัวเลขของ index เริ่มจาก 0

function ที่ใช้กับอาเรย์

push(@array,value); #เพิ่มข้อมูลเข้าไปยังท้ายสุดของอาเรย์
pop(@array); #ลบข้อมูลท้ายสุดของอาเรย์
unshift(@array,value); #เพิ่มข้อมูลไปที่ index 0
shift(@array); #ลบข้อมูลที่ index 0
delete($array[index]);

การนับจำนวนของ array
scalar(@array);
$#array+1; #เป็นการหาค่า index ตัวสุดท้ายของ อาเรย์ ถ้านำมาบวก 1 คือจำนวนอาเรย์ทั้งหมด

foreach ของ array

foreach(@array){
print $_;
}

foreach(0..$#array){
print $array[$_];
}

แสดงค่าตำแหน่งของข้อมูลที่เก็บบน memory

#!c:\perl\bin\perl.exe

use strict;
use CGI;

my $cgi = CGI->new;
print $cgi->header("text/html");

my $data = "myData";
my $ref = \$data; #$ref จะเก็บค่าตำแหน่งของ $data
print $ref."<br>"; # แสดงค่าตำแหน่ง
print $$ref."<br>"; #ใช้ค่าของตำแหน่งแสดงข้อมูลที่ตำแหน่งนั้น
print ${$ref}; #แสดงข้อมูลที่ตำแหน่งนั้น

declare scalar variable

ประกาศแบบไม่กำหนดค่าเริ่มต้นให้
my $scalar;

ประกาศแบบกำหนดค่าเริ่มต้นให้
my $scalar = value;

ประกาศแบบหลายตัวแปร
my($scalar1,$scalar2,.....) = (value1,value2,....);

27 April 2011

use strict in perl

strict module เป็นตัวที่คอยช่วยตรวจสอบการเขียนโค๊ดของเราว่าถูกต้องตามหลักหรือไม่

#!c:\perl\bin\perl.exe

use strict;
use CGI;

my $cgi = CGI->new;
print $cgi->header("text/html");

$ref = \$foo;
print $$ref; #ถ้าใส่ use strict จะแสดง error แต่ถ้าไม่ใช่จะเป็นหน้าเปล่า

$ref = "foo";
print $$ref; #ถ้าใส่ use strict จะแสดง error แต่ถ้าไม่ใช่จะเป็นหน้าเปล่า

$file = "STDOUT";
print $file "Hi!"; #ถ้าใส่ use strict จะแสดง error แต่ถ้าไม่ใช่จะแสดงคำว่า Hi! อย่างเดียว (ผิดเนื่องจากไม่ใส่ . เชื่อม)

perl auto-flush

การทำ auto-flush ใน perl ทำได้โดยการเซ็ตค่า $| เป็นหนึ่งซึ่งโดยปกติแล้วค่านี้จะมีค่าเป็น 0

#!c:\perl\bin\perl.exe

use CGI;

my $cgi = CGI->new;
print $cgi->header('text/html');

$|=1;

for ( 1..5 ) {
print "Value is $_";
sleep 1;
}

เมื่อทำการรัน จะเห็นข้อความขึ้นมาหนึ่งบรรทัดแล้วหยุดไปหนึ่งวิแล้วก็ค่อยมีบรรทัดใหม่ขึ้นมา

printf VS sprintf

ความแตกต่างของ printf กับ sprintf คือ sprintf มี null character (\0) ต่อท้าย

print ใน perl

#!c:\perl\bin\perl.exe

print "content-type:text/html\n\n";

#print ค่าของตัวแปร
print "Server $ENV{'SERVER_NAME'}\n\n";

#print ตัวอักษรด้วย (')
print 'We are running Perl version:';
print $^V."\n\n";

#print multiline
print < The perl binary : $^X
The perl script : $0
EOF

passing parametor to subroutines

การส่งค่า parametor ใน perl

รับเข้ามาเป็น array ใช้ @_
รับค่าแต่ละตัวตามลำดับ $_[1],$_[2],.....

การใช้ quote ใน perl

double qoute (") ค่าที่อยู่ใน doble qoute ถ้าเป็นตัวแปรจะแสดงค่าของตัวแปรนั้นออกมา ถ้าเป็นตัวอักษรพิเศษก็จะแสดงตัวอักษรพิเศษออกมา

single qoute (') ค่าที่อยู่ใน single qoute จะแสดงตามที่อยู่ข้างในทั้งหมด

black qoute (`) ค่าที่อยู่ในนี้คือคำสั่ง shell หรือ dos โดยจะ return ค่าผลลัพธ์ที่ได้ สามารถใช้ตัวแปรมารับค่าได้

#!c:\perl\bin\perl
print "content-type:text/html \n\n";
print "Hello World\n"; #จะได้ Hello World
print 'Hello World\n'; #จะได้ Hello World\n
print `date`; #จะได้ The current date is: Wed 04/27/2011 Enter the new date: (mm-dd-yy)

ผลลัพธ์ใน browser
Hello World Hello World\nThe current date is: Wed 04/27/2011 Enter the new date: (mm-dd-yy)

ผลลัพธ์ใน view->source
Hello World
Hello World\nThe current date is: Wed 04/27/2011
Enter the new date: (mm-dd-yy)

my function ในภาษา perl

การใช้ my function กับตัวแปรใน perl คือการกำหนด variable scope ถ้าเราสร้างตัวแปรที่มีชื่อเหมือนกับ global variable ในฟังก์ชั่นโดยใช้ my ด้วย global variable จะไม่ถูกแก้ไข

#!c:\perl\bin\perl
print "content-type:text/html \n\n";
$myString = "haha";
sub test
{
my $myString = "test";
}
print $myString."->01";
&test();
print $myString."->02";

ผลลัพธ์
haha->01
haha->02

###############################################

#!c:\perl\bin\perl
print "content-type:text/html \n\n";
$myString = "haha";
sub test
{
$myString = "test";
}
print $myString."->01";
&test();
print $myString."->02";

ผลลัพธ์
haha->01
test->02

###############################################

#!c:\perl\bin\perl
print "content-type:text/html \n\n";
my $myString = "haha";
sub test {
$myString = "test";
}
print $myString. "->01";
&test();
print $myString. "->02";

ผลลัพธ์
haha->01
test->02

###############################################

#!c:\perl\bin\perl
print "content-type:text/html \n\n";
my $myString = "haha";
sub test {
my $myString = "test";
}
print $myString. "->01";
&test();
print $myString. "->02";

ผลลัพธ์
haha->01
haha->02

20 April 2011

Method access modifier ใน objective-c

‘-’ in the front of the definition, signifies that the method is an instance method.
‘+’ to define a class method (more on class methods in a future post).

credit : http://macdevelopertips.com/objective-c/objective-c-defining-a-class.html

เปรียบเทียบชื่อ template ของ xcode ios

Cocoa Touch OpenGL Application -> OpenGL ES Application

Cocoa Touch Tab Bar -> Tab Bar Application

Cocoa Touch Utility -> Utility Application

Cocoa Touch Application -> Window-based Application / View based application

Cocoa Touch List -> Navigation based Application

12 April 2011

ความหมายของ prefix "NS"

อ้างอิงจาก wiki ละกัน

NEXTSTEP, an object-oriented, multitasking operating system by NeXT, as well as a prefix for certain system-provided classes in the operating system, such as "NSString"

มันคือ nextstep นั้นเอง

http://en.wikipedia.org/wiki/NS

31 March 2011

The Windows Installer Service could not be accessed.

วิธีแก้ปัญหา
The Windows Installer Service could not be accessed. This can occur if you are running Windows in a safe mode, or if the Windows Installer is not correctly installed. Contact your support personnel for assistance.”

ทำตามขั้นตอนดังนี้

Click Start
click Run
พิมพ์ MSIEXEC /UNREGISTER
click OK

Click Start
click Run
พิมพ์ MSIEXEC /REGSERVER
click OK
รอสักครู่

หลังจากนั้นลองทดสอบลงโปรแกรมดูอีกครั้งครับ

24 February 2011

when The ARP entry addition failed: Access is denied.

delete : netsh interface ip delete neighbors "Network card name here" "Gateway.IP.goes.here"

Add : netsh interface ip add neighbors "Network card name here" "Gateway.IP.goes.here" "MAC-address-of-gateway-with-dash-here"

20 January 2011

Java Access Specifiers [Access Modifiers]

The access to classes, constructors, methods and fields are regulated using access modifiers i.e. a class can control what information or data can be accessible by other classes. To take advantage of encapsulation, you should minimize access whenever possible.
Java provides a number of access modifiers to help you set the level of access you want for classes as well as the fields, methods and constructors in your classes. A member has package or default accessibility when no accessibility modifier is specified.
Access Modifiers
1. private
2. protected
3. default
4. public


19 January 2011

Windows 7 – Enable Telnet

It’s very rare that I use Telnet these days, so it took a long time for me to notice that by default it was not packaged with Windows 7. I did some research and found out that this was also true for Windows Vista. More than likely this was an attempt to make Windows more secure by default, as Telnet is very insecure and whenever you have the choice you should always use SSH. However, with that being said, you can quickly re-enable Telnet by following these steps:

1. Start
2. Control Panel
3. Programs And Features
4. Turn Windows features on or off
5. Check Telnet Client
6. Hit OK

After that you can start Telnet via Command Prompt.

copy ทั้งดุ้นมาจาก http://www.fettesps.com/windows-7-enable-telnet/