今天很幸福

虽然只是远远的在一旁看着她,和她少话,心里却觉得已经很满足了,有种难以形容的很幸福的感觉。她还是那么的样子,她全身散发着一种独特的气质。

今天我们几个老人去CS的麦当劳聚聚,只有我们几个人,还以为会有很多我不认识的老人,所以在昨天有和嘉蔚说记得等我,怕他们不知道我所以没等我所以。。。所以。。。竟然,我还不是准时到的。(早了10分钟,呵呵)看着思勇,她,荣伟,嘉蔚,瑞勤在麦当劳的交流,突然想去过了一个人的一年,又回到了小时候,中学的时候,几个电协的人在那里聊天,虽然我没有参与聊天,但至少也有当听众,今年也不例外。看起来和年前一样安静,但其实我心里一直想我应该勇敢的参他们话题。但今天我还是不能参到他们的话题。今天就做了一些研究,研究他们的话题是怎么找出来的。让我发现了一个窍门,就是随兴想到什么就讲什么。

今年,少了群智,小狗的老婆,还有我那届的快讯组组长,就感觉真的少了什么元素,化学反应式不大成立了。但我不知道,到底是什么,我只听他们说他们去马六甲了。

接下来几天就是圣诞节,没有任何决定的话,应该都会留在家,上着清心论坛和Facebook度过圣诞节吧!

fyhao.com upgraded to WordPress 2.8.6

刚把 fyhao.com 升级至最新版 WordPress 2.8.6,并选择自动更新选项。即使是安装插件都能采用自动化,真的是非常的方便。如果 Discuz! 也能这样的话,也就很好了。Discuz! 论坛的新核心插件系统是一个进展,若也能做到自动更新不用用到FTP软件的,但只需输入FTP资料系统就会自动下载,解压,安装,这样就省了很多很多时间了。

Just upgraded fyhao.com into latest version of WordPress 2.8.6, and select the auto upgrade option. Feel that it is very nice to use the auto upgrade option because it reduce the time to access the FTP software and waiting to connect the FTP server. It just requires user entering the FTP information, and it will automatically download the upgraded files and unzip it, then installed selfly…

Simple URL routing in Servlet

I have made a simple URL routing using servlet in Java. Using MainServlet as the front controller, then decide which service to call.

I have defined MainServlet.java as the Front controller, Service.java as the parent base of service, then GiftService.java for the actual service that will be carried out for action. MainServlet.java will be able to examine the URL, for example by the form of /Gift/send , it will call GiftService class and send method in the GiftService.java. Before that it calls the init method inside Service.java to transfer some instance that maybe used by the Services classes, such as HttpServletRequest, HttpServletResponse, and etc.

MainServlet.java

package com.fyhao.test;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MainServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = request.getPathInfo();
        try {
            String[] parts = path.split("/");
            if(parts.length >= 2) {
                try {
                    Class iClass = Class.forName("com.fyhao.test." + parts[1] + "Service");
                    Method init;
                    Constructor con;
                    Object myClass;
                    try {
                        init = iClass.getMethod("init", new Class[] {HttpServlet.class, HttpServletRequest.class, HttpServletResponse.class, String[].class});
                        con = iClass.getConstructor(new Class[] {});
                        myClass = con.newInstance(new Object[] {});
                        init.invoke(myClass, new Object[]{this, request, response, parts});
                    } catch (NoSuchMethodException ex) {
                        System.err.println("Please define init method on " + parts[1] + "Service");
                        return;
                    } catch (Exception ex) {
                        System.err.println("init exception");
                        return;
                    }
                    Method m;
                    if(parts.length >= 3) {
                        try {
                            m = iClass.getMethod(parts[2], null);
                            m.invoke(myClass);
                        } catch (NoSuchMethodException ex) {
                            try {
                                m = iClass.getMethod("main", null);
                                m.invoke(myClass);
                            } catch (Exception exc) {
                                System.err.println("Please define a main service method on " + parts[1] + "Service");
                                return;
                            }
                        } catch (Exception ex) {
                            System.err.println("service method exception");
                            return;
                        }
                    } else {
                        try {
                            m = iClass.getMethod("main", null);
                            m.invoke(myClass);
                        } catch (Exception exc) {
                            System.err.println("Please define a main service method on " + parts[1] + "Service");
                            return;
                        }
                    }
                } catch (ClassNotFoundException ex) {
                    defaultAction(request, response);
                }
            } else {
                defaultAction(request, response);
            }
        } catch (NullPointerException ex) {
            defaultAction(request, response);
            return;
        }
        //serviceclass("Gift", "send");
    }
    public void defaultAction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("default action here: " + request.getPathInfo());
    }
    // leave for reference
    public void serviceclass(String c, String service) {
        try {
            Class iClass = Class.forName("com.fyhao.test." + c + "Service");
            Method init = iClass.getMethod("init", null);
            Method m = iClass.getMethod(service, null);
            Constructor con = iClass.getConstructor(new Class[] {});
            Object myClass = con.newInstance(new Object[] {});
            m.invoke(myClass);
        } catch( Exception ex) {}
    }
}

 

Service.java

package com.fyhao.test;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Service {

    HttpServlet servlet;
    HttpServletRequest request;
    HttpServletResponse response;
    String[] parts;
    public void init(HttpServlet servlet, HttpServletRequest request, HttpServletResponse response, String[] parts) {
        this.servlet = servlet;
        this.request = request;
        this.response = response;
        this.parts = parts;
    }
}

GiftService.java

package com.fyhao.test;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GiftService extends Service {

    public void main() {
        System.out.println("default main methods: ");
    }
    public void send() {
        System.out.println("sending");
    }
}

 

All right, this is the coding done by me during first learning of using java reflection API, which is the great API to directly find the class by the full name, and dynamically initializes and calling its methods. However, I think it would have other better ways of doing this. Please tell me if you know. Thanks.