- 分享
- 0
- 人气
- 0
- 主题
- 20
- 帖子
- 706
- UID
- 19942
- 积分
- 2395
- 阅读权限
- 20
- 注册时间
- 2005-10-28
- 最后登录
- 2021-8-30
- 在线时间
- 643 小时
|
本人初学 Java Reflection API 而设计了一个简单的 URL Routing。
它能分析URL,如 /Gift/send,而调用 GiftService class 然后调用 send() method。
本人设计了三个基本的 class,如 MainServlet.java (最主要的入口文件,也就是 Front Controller),Service.java (各 service 服务的父类),及 GiftService.class (实例服务)。
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");
- }
- }
复制代码 这篇文章的原文出自本人的部落格文章:http://fyhao.com/2009/12/computer-and-it/java/simple-url-routing-in-servlet/
由于本人初学这方面的架构知识,请各位高手给本人一些意见,或指导方向。
这样,就万分感激了。 |
|