JAVA ที่สำหรับไว้เขียนบนแต่ละ Plateform มี
รูปภาพจาก http://www.blueboard.com/
- J2SE หรือ SE (Standard Edition) ไว้สำหรับพัฒนา Application บนคอมพิวเตอร์ Application ที่พัฒนาด้วย SE นั้นจะติดตั้งบนเครื่องคอมพิวเตอร์(Client) ถ้าเป็น Application ที่ใช้สำหรับติดตั้งบน windows OS เราจะเรียกว่า windows application นั่นเอง
- J2EE หรือ EE (Enterprise Edition) ไว้สำหรับพัฒนาApplication ในองค์กรใหญ่ๆ หรือมีขอบเขตของโครงการกว้างมาก ส่วนมากจะเป็นเกี่ยวกับ Web application J2EE จะทำงานฝั่ง Server
- J2ME หรือ ME (Micro Edition) ไว้สำหรับพัฒนา Application บนอุปกรณ์พกพา , PDA
ตัวอย่างการพัฒนา Application ด้วย Java
Java พื้นฐาน Hello World
Java พื้นฐาน Hello World
public
class Example(){public
static
void
main(String[] args) {
System.out.println("Hello World !!");
}
}
หลักการพัฒนา Web Application พื้นฐาน
การพัฒนา Web Application แบ่งออกเป็น 2 ส่วน คือ โค้ตส่วนที่ประมวลผลฝั่ง Client และส่วนที่ประมวลผลฝั่ง Server ส่วนที่ประมวลผลฝั่ง Client เช่น HTML,Java script ส่วน Java นั้นจะทำงานอยู่ฝั่ง Server ในช่วงแรกเริ่มนั้นจะพัฒนาเป็น JSP,Servlet โดยไม่มี framework อะไรมาช่วยมากมายนัก ตัวอย่างที่จะนำมาเสนอในบทความนี้จะเป็นเพียงพื้นฐานของ JSP,Servlet เท่านั้นครับ
ตัวอย่างโค้ต JSP
<html>
<head>
<title>First JSP</title>
</head>
<head>
<title>First JSP</title>
</head>
<body>
<%= new String("Hello!") %>
</body>
</html>
จะเห็นได้ว่า JSP นั้นเป็นการเขียนโค้ต JAVA ลงใน scriptlet แทรกอยู่ใน html code เลย
ตัวอย่างโค้ต Servlet
package com.Sample;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/servletSample ")
public class ServletSample extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException{
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<h1>Hello Servlet Get</h1>");
out.println("</body>");
out.println("</html>");
}
}
จะเห็นว่า servlet จะเป็นการเขียนโค้ต JAVA แต่เราสามารถ print หน้าจอที่เป็นโค้ต html ออกมาได้
จึงมีการนำ JSP,servlet มาใช้ร่วมกัน
หลักการพัฒนา Mobile Application พื้นฐาน
การพัฒนา Mobile Application นั้นจะยกตัวอย่าง Mobile App ที่ใช้ J2ME กับ Android App(ในส่วนของ Android ไม่ได้ใช้ J2ME นะครับ แต่ที่ยกตัวอย่างมาด้วยเพราะเห็นเป็น Mobile App ตามหัวข้อนี้เท่านั้นครับ) ส่วนของการ develop จะมีตัว SDK (Software Development Kit) เป็นเครื่องมือเข้ามาใช้ในการพัฒนา Mobile application
ตัวอย่างโค้ต Mobile App ที่ใช้ J2ME
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class HelloWorld extends MIDlet {
private Display display;
private Form helloFrm;
public HelloWorld () {
helloFrm = new Form("Hello World");
}
protected void startApp() {
display = Display.getDisplay(this);
display.setCurrent(helloFrm);
}
protected void pauseApp() {
}
protected void destroyApp(boolean unconditional) {
}
}
ตัวอย่างโค้ต Mobile App ที่ใช้ J2ME
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class HelloWorld extends MIDlet {
private Display display;
private Form helloFrm;
public HelloWorld () {
helloFrm = new Form("Hello World");
}
protected void startApp() {
display = Display.getDisplay(this);
display.setCurrent(helloFrm);
}
protected void pauseApp() {
}
protected void destroyApp(boolean unconditional) {
}
}
ตัวอย่างโค้ต Android
โค้ตส่วนของ Java file
โค้ตส่วนของ Java file
package com.example.helloAndroid;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
โค้ตส่วนของ Manifest File
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloworld"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="22" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main"
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
โค้ตส่วนของ String File
<resources>
<string name="app_name">HelloAndroid</string>
<string name="hello_android">Hello Android!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
</resources>
โค้ตส่วนของ Layout activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="@dimen/padding_medium"
android:text="@string/hello_android"
tools:context=".MainActivity" />
</RelativeLayout>
สำหรับบทความนี้ผมขอจบไว้เพียงเท่านี้ก่อนนะครับ แล้วจะมีบทความอื่นๆมาให้อ่านเรื่อยๆครับ
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloworld"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="22" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main"
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
โค้ตส่วนของ String File
<resources>
<string name="app_name">HelloAndroid</string>
<string name="hello_android">Hello Android!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
</resources>
โค้ตส่วนของ Layout activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="@dimen/padding_medium"
android:text="@string/hello_android"
tools:context=".MainActivity" />
</RelativeLayout>
สำหรับบทความนี้ผมขอจบไว้เพียงเท่านี้ก่อนนะครับ แล้วจะมีบทความอื่นๆมาให้อ่านเรื่อยๆครับ
ไม่มีความคิดเห็น:
แสดงความคิดเห็น