Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

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()

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