Label

Thứ Hai, 29 tháng 11, 2010

Trail: Custom Networking

Trail: Custom Networking


http://download.oracle.com/javase/tutorial/networking/TOC.html

Thứ Sáu, 19 tháng 11, 2010

parameters in constructor

        // Create a JList that displays strings from an array
javax.swing.JList myList = new javax.swing.JList(data);
javax.swing.ListModel model = myList.getModel();
jList1.setModel(model);

//javax.swing.JList myList = new javax.swing.JList(Planets.values())

constructor của JList có thể tạo bất kì array nào, không quan tâm đến data type, vì sao vậy?

Thứ Tư, 17 tháng 11, 2010

The Swing Tutorial

Trail: Creating a GUI With JFC/Swing: Table of Contents
http://download.oracle.com/javase/tutorial/uiswing/TOC.html

Trail: Graphical User Interfaces: Table of Contents
http://download.oracle.com/javase/tutorial/ui/TOC.html

Thứ Hai, 15 tháng 11, 2010

How many way to input data from keyboard in java?

from Console

System.in <-- how to use?

Scanner sc = new Scanner(System.in).useDelimiter("\n");

How many way to input data from keyboard in java?

Thứ Ba, 9 tháng 11, 2010

JAR

run jar file need Entry Point

use [cfe] to set Entry Point, without editing or creating manifest file.

jar  cfev  filename.jar  package.ClassName *.class audio image

http://download.oracle.com/javase/tutorial/deployment/jar/index.html

Executable JAR Files


On Microsoft Windows systems, the Java 2 Runtime Environment's installation program will register a default association for JAR files so that double-clicking a JAR file on the desktop will automatically run it with javaw -jar. Dependent extensions bundled with the application will also be loaded automatically. This feature makes the end-user runtime environment easier to use on Microsoft Windows systems.

Thứ Tư, 3 tháng 11, 2010

Question 2 - Interface's Exercise

public interface TimeClient {
public void setTime(int hour, int minute, int second);
public void setDate(int day, int month, int year);
public void setDateAndTime(int day, int month, int year,
int hour, int minute, int second);
}

Suppose that you have written a time server, which periodically notifies its clients of the current date and time. Write an interface that the server could use to enforce a particular protocol on its clients.

Trong ví dụ này, đây là lớp interface được TimeServer implements,  tại sao ko là một interface từ TimeServer được lớp TimeClient triển khai, đúng theo tinh thần đã học ở đầu lesson interface, là tạo 1 protocol chung cho các nhà lập trình riêng biệt, không cầ n biết gì về công việc của nhau và hợp tác thông qua interfaces để tạo sản phẩm???

Thứ Ba, 2 tháng 11, 2010

Is the following interface valid?

Question 4: Is the following interface valid?

public interface Marker {
}


Answer 4: Yes. Methods are not required. Empty interfaces can be used as types and to mark classes without requiring any particular method implementations. For an example of a useful empty interface, see java.io.Serializable.

Thứ Hai, 1 tháng 11, 2010

Inner Class Example

public class DataStructure {
private static final int SIZE = 15;
private static int[] arrayOfInts = new int[SIZE];

public DataStructure() {
for (int i = 0; i < SIZE; i++) {
arrayOfInts[i] = i;
}
}

public int getSIZE() {
return SIZE;
}

public static int getValueOfArray(int index) {
return arrayOfInts[index];
}

public void printEven() {
InnerEvenIterator iterator = new InnerEvenIterator();
while (iterator.hasNext(getSIZE())) {
System.out.print(iterator.getNext() + " ");
}
}

//Inner Class
/* private class InnerEvenIterator {
private int next = 0;

public boolean hasNext() {
return (next <= SIZE - 1);
}

public int getNext() {
int valueNext = arrayOfInts[next];
next += 2;
return valueNext;
}
} */

public static void main(String[] args) {
DataStructure ds = new DataStructure();
ds.printEven();
}
}

public class InnerEvenIterator {
private int next = 0;

public boolean hasNext(int S) {
return (next <= S - 1);
}

public int getNext() {
int valueNext = DataStructure.getValueOfArray(next);
next += 2;
return valueNext;
}
}

Các điểm giống và khác nhau khi dùng cách 1 và 2.

Thứ Năm, 28 tháng 10, 2010

cannot be referenced from a static context

public class ClassesandObjects{
public static void main(String[] args){
int x = 3;
passMethod(x);
System.out.println("After invoking passMethod, x = " + x);
}
public /*static*/ void passMethod(int p){
p = 10;
System.out.println("Invoke passMethod, x = " + p);
}
}


ClassesandObjects.java:4: non-static method passMethod(int) cannot be referenced
from a static context
passMethod(x);
^
1 error

What output do you think the code will produce if aNumber is 3?


if (aNumber >= 0)
if (aNumber == 0) System.out.println("first string");
else System.out.println("second string");
System.out.println("third string");


What output do you think the code will produce if aNumber is 3?

->
if ... ;
else ...;

la mot cau lenh

How do you write an infinite loop using the for statement?

Question: How do you write an infinite loop using the for statement?

Answer:
for ( ; ; ) {

}




The three expressions of the for loop are optional; an infinite loop can be created as follows:

for ( ; ; ) {    // infinite loop

// your code goes here
}


Thứ Tư, 27 tháng 10, 2010

If-then-else Statements

/* IfElseDemo */
int testscore = 76;
char Grade = 'H';
if (testscore >= 90){
Grade = 'A';
} else if (testscore >= 80){
Grade = 'B';
} else if (testscore >= 70){
Grade = 'C';
System.out.println("Grade in loop = " + Grade);
} else if (testscore >= 60){
Grade = 'D';
} //else {
// Grade = 'F';
// }
System.out.println("Grade " + Grade);
}

Nếu gán cho Grade giá trị đầu thì compile được, ra C.
Khi không gán cho Grade giá trị đầu thì báo lỗi
E:\Java\Learn>javac HelloWorldApp.java
HelloWorldApp.java:70: variable Grade might not have been initialized
System.out.println("Grade " + Grade);
^
1 error

InstanceofDemo

class InstanceofDemo {
public static void main(String[] args) {

Parent obj1 = new Parent();
Parent obj2 = new Child();

System.out.println("obj1 instanceof Parent: " + (obj1 instanceof Parent));
System.out.println("obj1 instanceof Child: " + (obj1 instanceof Child));
System.out.println("obj1 instanceof MyInterface: " + (obj1 instanceof MyInterface));
System.out.println("obj2 instanceof Parent: " + (obj2 instanceof Parent));
System.out.println("obj2 instanceof Child: " + (obj2 instanceof Child));
System.out.println("obj2 instanceof MyInterface: " + (obj2 instanceof MyInterface));
}
}

class Parent{}
class Child extends Parent implements MyInterface{}
interface MyInterface{}


Parent obj2 = new Child(); khong hieu cho ni, Sao khong dung Child obj2 = new Child();

Thứ Hai, 25 tháng 10, 2010

2. Language Basics

2. Language Basics

Variables

Exercises:

1. báo lỗi  [identifier] expected khi khai báo int ???number;

không báo illegal type ?

2. báo lỗi Non-static fields ... not references ... static context ?

Thứ Bảy, 16 tháng 10, 2010

Trang JSP don gian



JSP là gì ? Java Server Pages
JavaServer™ Pages (JSP) is the Java™ Platform, Enterprise Edition (Java EE)
technology for building applications for generating dynamic web content, such as
HTML, DHTML, XHTML, and XML. JSP technology enables the easy authoring
of web pages that create dynamic content with maximum power and flexibility. (jsp2.1 spec)

JSP.1.1 What Is a JSP Page
A JSP page is a textual document that describes how to create a response object
from a request object for a given protocol. The processing of the JSP page may
involve creating and/or using other objects.
A JSP page defines a JSP page implementation class that implements the
semantics of the JSP page. This class implements the javax.servlet.Servlet
interface (see Chapter JSP.11, “JSP Container” for details). At request time a
request intended for the JSP page is delivered to the JSP page implementation
object for processing.
HTTP is the default protocol for requests and responses. Additional request/
response protocols may be supported by JSP containers. The default request and
response objects are of type HttpServletRequest and HttpServletResponse
respectively.

JSP.1.1
Một trang JSP là một “textual document” mô tả làm thế nào để tạo một đối tượng phản hồi từ một đối tượng yêu cầu cho một giao thức nhất định. Sự xử lý của trang JSP có thể bao gồm tạo và/hoặc sử dụng các đối tượng khác.
Một trang JSP định nghĩa một lớp thực thi trang JSP mà thực thi các ngữ nghĩa của trang JSP.
Lớp này bổ sung các giao diện javax.servlet.Servlet. Tại thời điểm yêu cầu một yêu cầu dành cho các trang JSP được chuyển về cho các đối tượng thực hiện trang JSP để xử lý.
HTTP là giao thức mặc định cho các yêu cầu và phản hồi. Các giao thức yêu cầu/phản hồi thêm vào có thể được hỗ trợ bởi JSP containers. Đối tượng yêu cầu/phản hồi mặc định là kiểu lần lượt tương ứng với HttpServletRequest và HttpServletResponse

//You can think of servlets as Java code with HTML inside; you can think of JSP as
HTML with Java code inside. (JSP-Overview p.3)

Mã nguồn trang JPS:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Order Confirmation</TITLE>
<LINK REL=STYLESHEET
HREF="JSP-Styles.css"
TYPE="text/css">
</HEAD>
<BODY>
<H2>Order Confirmation</H2>
Thanks for ordering <I><%= request.getParameter("title") %></I>!
</BODY></HTML>

http://localhost:8080/JPSoverview/test.jsp?title=Hello+JAVA+World

Khi chạy được kết quả

Order Confirmation


Thanks for ordering Hello JAVA World!

Request như thế nào, quá trình ra sao? Reponse nữa ??
http://my.opera.com/nguyenhung2709/blog/show.dml/493169

Ant



Demo
<project name="copydir" default="Copydir" basedir=".">
<property name="src" value="WEB-INF" />
<property name="dist" value="dist" />
<target name="Copydir">
<copydir src="${src}"
dest="${dist}"
includes="**/*.*"
excludes="**/web.xml"
/>
</target>
</project>

result:
WEB-INF: tld/ and 5 files  (total 10 files)
dist: tld/ and 4 files  (loại file web.xml)
YES!!!CHEER!!

Nhưng đoạn code này lại không exclude file web.xml

<!– Create deployable war file –>
<target name=”war” depends=”compile” description=”Packages app as WAR”>
<mkdir dir=”${dist.dir}” />
<war destfile=”${dist.dir}/HelloWorldPortlet.war”
webxml=”${web.dir}/WEB-INF/web.xml”>
<classes dir=”${build.dir}/classes”/><!– Include compiled classes –>
<fileset dir=”${web.dir}” ><!– Include jsp pages –>
<include name=”**/*.*” />
<exclude name=”**/web.xml” />
</fileset>
<lib dir=”${lib.dir}”><!– Include lib jars –>
<exclude name=”**/j2ee.jar” />
</lib>
</war>
</target>

Không hiểu ?
Cuối cùng cũng xong, ngốc thật :D

Anatomy for a portlet



Chiều nay: cấu tạo portlet, tạo Hello World Portlet
mất tập trung quá…

Packaging p.176

A portlet project is made up at a minimum of three components:

  • Java Source

  • Configuration files

  • Client-side files (*.jsp, *.css, *.js, graphics, etc.)


These files are stored in a standard directory structure which looks like the following:
          /PORTLET-NAME/
build.xml
/docroot/
icon.png
view.jsp
/css/
/js/
/WEB-INF/src/ (Java Source)         //not created by default
liferay-display.xml
liferay-plugin-package.properties
liferay-portlet.xml
portlet.xml ()
web.xml

portlet.xml: là file quan trọng (vì sao quan trọng ?)
docroot/WEB-INF/portlet.xml

<portlet>

<portlet-name>my-greeting</portlet-name>

<display-name>My Greeting</display-name>

<portlet-class>com.liferay.util.bridges.mvc.MVCPortlet</portlet-class>

<init-param>

<name>view-jsp</name>

<value>/view.jsp</value>

</init-param>

<expiration-cache>0</expiration-cache>

<supports>

<mime-type>text/html</mime-type>

</supports>

<portlet-info>

<title>My Greeting</title>

<short-title>My Greeting</short-title>

<keywords>My Greeting</keywords>

</portlet-info>

<security-role-ref>

<role-name>administrator</role-name>

</security-role-ref>

<security-role-ref>

<role-name>guest</role-name>

</security-role-ref>

<security-role-ref>

<role-name>power-user</role-name>

</security-role-ref>

<security-role-ref>

<role-name>user</role-name>

</security-role-ref>

</portlet>

portlet-name

The portlet-name element contains the canonical name of the portlet. Each portlet name is unique within the portlet application. (This is also referred within Liferay Portal as the portlet id)

display-name

The display-name type contains a short name that is intended to be displayed by tools. It is used by display-name elements. The display name need not be unique.

portlet-class

The portlet-class element contains the fully qualified class name of the portlet.

init-param

The init-param element contains a name/value pair as an initialization param of the portlet.

expiration-cache

Expiration-cache defines expiration-based caching for this portlet. The parameter indicates the time in seconds after which the portlet output expires. -1 indicates that the output never expires.

supports

The supports element contains the supported mime-type. Supports also indicates the portlet modes a portlet supports for a specific content type. All portlets must support the view mode.

portlet-info

Portlet-info defines portlet information.

security-role-ref

The security-role-ref element contains the declaration of a security role reference in the code of the web application. Specifically in Liferay, the role-name references which role’s can access the portlet.

PLT.23.4 Directory Structure

A portlet application follows the same directory hierarchy structure as web applications.

In addition it must contain a /WEB-INF/portlet.xml deployment descriptor file.

Portlet classes, utility classes and other resources accessed through the portlet application classloader must reside within the /WEB-INF/classes directory or within a JAR file in the /WEB-INF/lib/ directory.

-In developing your own portlets you are free to use any framework you prefer,

such as Struts, Spring MVC, or JSF. For this portlet we will use the Liferay MVCPortlet

framework as it is simple, lightweight, and easy to understand.

framework? Struts, Spring MVC, JSF ?

Làm xong rồi, nhưng làm sao nó chạy được file class khi không có khai báo ???

JSP



Tài liệu tham khảo:

http://www.courses.coreservlets.com/Course-Materials/msajsp.html

http://jcp.org/aboutJava/communityprocess/final/jsr245/index.html

http://pdf.coreservlets.com/JSP-Overview.pdf

http://java.sun.com/products/jsp/overview.html

Anatomy a Portlet

http://www.liferay.com/documentation/liferay-portal/6.0/development/-/ai/anatomy-of-a-portlet

HTTP Response



HTTP Response khá giống với HTTP Request. Dấu hiệu khác biệt duy nhất là response bắt đầu với một dòng trạng thái status so với Request-Line. Status-Line, cũng giống như Request-Line, chứa ba mục ngăn cách bởi các khoảng trống.

Một HTTP response bắt đầu với một Status-Line và có thể chứa các header và một message body. Header có thể mô tả quá trình truyền dữ liệu, xác định response, hoặc phần body kèm theo.
Dòng bắt đầu với phiên bản cao nhất của HTTP mà server hỗ trợ.
Code:

HTTP/1.1 200 OK Date: Sun, 08 Oct 2000 18:46:12 GMT Server: Apache/1.3.6 (Unix) Keep-Alive: timeout=5, max=120 Connection: Keep-Alive Content-Type: text/html <html>…

HTTP Status-Line bắt đầu với chỉ báo HTTP, mã trạng thái, và một đoạn text mô tả response.
Hai mục còn lại trong Status-Line là Status-Code và Reason-Phrase. Status-Code là một bộ ba kí tự chỉ báo kết quả của request. Status-Code phổ biến nhất là 200. Giá trị này thông báo yêu cầu của client thành công.
Phân loại HTTP Status Code
Header Field
HTTP request và response có thể có một hay nhiều message header. Message header bắt đầu với tên trường và dấu (“:”). Trong một số trường hợp, chỉ có tên trường trong phần header. Trong hầu hết các trường hợp khác header chứa các thêm thông tin khác nữa, các thông tin này đi sau dấu “:”. Một message header kết thúc ở cuối dòng, nhưng nếu một client cần biểu diễn nhiều hơn một dòng thì dòng tiếp theo sẽ bắt đầu với một hay nhiều kí tự trống hay kí tự gạch ngang (ascii character 8). Ví dụ sau là của User-Agent header:
Code:
GET / HTTP/1.1
Accept: */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)
Host: www.ft.com
Connection: Keep-Alive

Nếu một message header chứa một chuỗi giá trị phân tách bởi dấu “,”; ta có thể tách ra thành các dòng riêng, như ví dụ sau tách các giá trị của Accept-Encoding:

Code:
GET / HTTP/1.1
Accept: */*
Accept-Language: en-us
Accept-Encoding: gzip
Accept-Encoding: deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)
Host: www.ft.com
Connection: Keep-Alive

Bảng sau thể hiện các HeaderField, phạm vi áp dụng của chúng trong các request hay response, hay trong message body (entity) đi kèm với request hay response.

Bảng HTTP Header Field

Nguon: http://my.opera.com/nguyenhung2709/blog/show.dml/493169

Gửi các request và xử lý response – Giao thức HTTP



Trước hết chúng ta cùng tìm hiểu hoạt đông của giao thức HTTP truyền thống – Hypertext Transfer Protocol, xem xét các bản tin – message của giao thức này. Thay vì quan tâm đến các bit hay byte, chúng ta để ý đến các từ ngữ (thông báo) được xác định trong đặc tả HTTP và các quy tắc kết hợp chúng lại với nhau.
Một HTTP message bắt đầu với một dòng request hay status, tiếp theo có thể là nhiều loại headers và phần message body.

Cấu trúc của HTTP Message


HTTP là một giao thức kiểu client/server; client đưa ra các request, và server sẽ trả lời các request này. Cấu trúc các HTTP message vì thế cũng thay đổi theo yếu tố này. Có một định dạng cho HTTP request và cho các response.
HTTP Request
Mỗi request bắt đầu với một Request-Line. Dòng này chỉ ra phương thức mà client yêu cầu, tài nguyên, và phiên bản của HTTP mà client có thể hỗ trợ. Request-Line có thể có tiếp sau một hay nhiều header và một message body.

Một HTTP request bắt đầu với một Request-Line và có thể bao gồm các header và message body. Phần header có thể mô tả quá việc truyền dữ liệu, xác định các yêu cầu hay phần message body kèm theo.
Code:
GET / HTTP/1.1
Accept: */*
Accept-Language: en-us A
ccept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)
Host: www.ft.com
Connection: Keep-Alive

Request-Line chứa ba mục phân biệt, đó là method, uri, và phiên bản HTTP, mỗi mục được phân tách bởi một hay nhiều khoảng trống.

Một HTTP Request-Line có một phương thức, một địa chỉ định danh tài nguyên (URI), và thông báo phiên bản HTTP.

Phương thức được xác định trên dòng đầu tiên của Request-Line. HTTP định nghĩa tất cả là 8 phương thức. Một HTTP server chỉ được yêu cầu hỗ trợ các phương thức GET và HEAD; nếu chúng hỗ trợ các phương thức HTTP khác, sự hỗ trợ đó phải được gắn với các quy tắc của HTTP. Đặc tả HTTP cũng có các mở rộng để các phương thức khác có thể được bổ sung trong tương lai.

Bảng HTTP Method

Mục tiếp theo trong Request-Line là Request-uri. Mục này cung cấp địa chỉ định danh tài nguyên cho một tài nguyên. Ví dụ, Request-uri là /, chỉ ra một request cho tài nguyên gốc. Cho các request không yêu cầu một tài nguyên cụ thể (như là TRACE request hay trong một số trường hợp cả OPTIONS request), client có thể dùng một dấu * cho Request-uri.

Mục cuối cùng trong Request-Line là phiên bản HTTP. Như trong ví dụ, phiên bản HTTP là 1.1 chứa trong đoạn text HTTP/1.1.

Tiếp sau Request-Line, một HTTP request có thể bao gồm một hay nhiều dòng message header. Một message header có thể chứa các loại general header, request header, hoặc entity header. General header áp dụng trong truyền dữ liệu; request header áp dụng cho các request cụ thể, và entity header áp dụng cho message body trong request.

Một HTTP request luôn chứa một dòng trống sau Request-Line và bất kỳ header nào. Nếu request bao gồm một message body, phần body đi sau một dòng trống. Dòng trống – blank line rất quan trọng vì server xác định được phần kết của request, hoặc phần kết của header. Không có dòng trống, server nhận các message sẽ không biết được các header khác nữa có tiếp tục được truyền không.

Nguon: http://my.opera.com/nguyenhung2709/blog/show.dml/493169

Task List



02-11: thac mac ve interface, chi tiet trong giay

Java co quy tac rat cu the, tuong minh, mac du nhieu nhung can thiet cho viec code chuong trinh tuong minh. rat cu the

khong nhu minh nghi truoc day, rat mo* magn`

Len ke hoach cho Learning the Java Language (hoc nhu the nao, co tieng Anh thi the nao, ton’ thoi gian)

ton thoi gian nhu the nao, trong bao lau, vi con phai hoan thanh portlet

tieng Anh thi sao, lau^ la vi tieng anh, vay thi hoc tieng Anh truoc

Có vấn đề với portlet,
Co van de syntax highlight, khong dung cach trinh bay
Liferay khùng quá! Như thế này: tạo 1 portlet JSP-Portlet, deploy chạy được rồi
Sau đó vào Control Panel Liferay deactive đi, rồi xóa folder JSP-Portlet trong webapp
Lại tạo portlet JSP-Portlet, deploy vào, báo “OK”, vào lại không thấy đâu, phải vào CP liferay đẻ active lại
Chắc là vẫn lưu giá trị trong CSDL, nhưng mà đã xóa folder rồi, khùng quá

#Task: Suy nghĩ là một bảng màu vàng giống kiểu note
Co van de voi link, localhost:8080/view_1.txt khong duoc
chắc là do liferay quản lý hết folder webapp

Building Java Programs

http://download.oracle.com/javaee/6/tutorial/doc/

http://education.oracle.com/pls/web_prod-plq-dad/db_pages.getlppage?page_id=212&path=SJPF

http://wikis.sun.com/display/code

http://www.oracle.com/technetwork/java/index-jsp-135888.html

http://www.oracle.com/technetwork/java/index-138747.html

Learning the Java Language



Unknow:

1/ Use interface as a data type

2/ Inner Class


3/ Generic

1. Object-Oriented Programming Concepts

2. Language Basics

Variables

Operators

Expressions, Statements, and Blocks

Control Flow Statements

3. Classes and Objects

Classes

Objects

More on Classes

Nested Classes

Enum Types

Annotations

4. Interfaces and Inheritance

Interfaces

Inheritance



5. Numbers and Strings

Numbers

Characters

Strings

6. Generics

7. Packages


Swing

http://download.oracle.com/javase/tutorial/uiswing/components/index.html

Hello world!

Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!