Wicket在自编写的基于java的服务器上

时间:2009-06-11 11:45:39

标签: java webserver wicket

嘿嘿,我有一个检票口 - 也是与服务器有关的问题:

我们用java构建了一个小服务器(是的,我知道,为什么要重新发明轮子......当我在wicket-mailing-list上询问问题时我得到了很多)....所以我们只是说,急需b / c ......呃...我仍然想不出一个很好的理由,但是我没有太多能做到这一点......所以我终于得到了这个小服务器来运行servlets ...据我所知,运行wicket你需要一个可以运行servlet的服务器...(至于我缺乏英语,我只想说:它应该是像tomcat一样的servlet容器)......现在我确实有这个非常漂亮的服务器,有没有人知道我必须做什么,以便我的wicket应用程序运行它?如何告诉我的服务器它应该运行wicket-app?

希望我解释得很好...... thnx提前:))

... dg


好的,有点来源......我按照教程做了这个:

发生“神奇”的HttpServer:

 while (!shutdown) {
  // ... all the stuff before

    //check if this is a request for a servlet or a static resource
    //a request for a servlet begins with "/servlet/"
    if (request.getUri().startsWith("/servlet/")) {
        System.out.println( "SERVLET-REQUEST\n\n" );
      ServletProcessor2 processor = new ServletProcessor2();
      processor.process(request, response);
    }
    else {
        System.out.println( "STATICRESOURCE-REQUEST" );
      StaticResourceProcessor processor = new StaticResourceProcessor();
      processor.process(request, response);
    }
 // ... all the stuff after
}

现在是ServletProcessor:

public class ServletProcessor2 {

public void process(Request request, Response response) {

 String uri = request.getUri();
 String servletName = uri.substring(uri.lastIndexOf("/") + 1);
 System.out.println( "SERVLETNAME ... " + servletName );
 URLClassLoader loader = null;

try {
   // create a URLClassLoader
   URL[] urls = new URL[1];
   URLStreamHandler streamHandler = null;
   File classPath = new File(Constants.WEB_ROOT);
   // the forming of repository is taken from the createClassLoader method in
   // org.apache.catalina.startup.ClassLoaderFactory
   String repository = (new URL("file", null, classPath.getCanonicalPath() + File.separator)).toString() ;
   System.out.println( "REPOSITORY ... " + repository );
   // the code for forming the URL is taken from the addRepository method in
   // org.apache.catalina.loader.StandardClassLoader class.
   urls[0] = new URL(null, repository, streamHandler);
   loader = new URLClassLoader(urls);
 }
 catch (IOException e) {
   System.out.println(e.toString() );
 }
 Class myClass = null;
 try {
   myClass = loader.loadClass(servletName);
 }
 catch (ClassNotFoundException e) {
   System.out.println("FEHLER: " + e.toString() + " ... CAUSE: " + e.getCause() );
   e.printStackTrace();
 }

 Servlet servlet = null;
 RequestFacade requestFacade = new RequestFacade(request);
 ResponseFacade responseFacade = new ResponseFacade(response);
 try {
     servlet = (Servlet) myClass.newInstance();
     servlet.service((ServletRequest) requestFacade, (ServletResponse) responseFacade);
 }
 catch (Exception e) {
   System.out.println( "Fehler: " + e.toString() + " ... CAUSE: " + e.getCause() );
   e.printStackTrace();
 }
 catch (Throwable e) {
   System.out.println(e.toString());
 }

}
}

1 个答案:

答案 0 :(得分:3)

简单的答案是,您需要实施Servlet API。 wicket文档似乎没有说明确切的所需版本,但我确信它至少是2.4。这不是我建议你做的工作,正确地做一件事是困难的事情,你只是打开自己的痛苦世界。

如果您需要嵌入服务器,那么我建议您从jetty开始并对其进行自定义以满足您的要求。 Jetty拥有非常宽松的许可证,因此您不必担心在商业产品中使用,并且可以使用最好的产品进行扩展。