Changes between Initial Version and Version 1 of waue/2009/0813


Ignore:
Timestamp:
Aug 13, 2009, 1:19:42 PM (15 years ago)
Author:
waue
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • waue/2009/0813

    v1 v1  
     1鑲嵌 jetty code
     2
     3
     4
     5
     6Using the ServletHandler
     7
     8If you do not wish to use web applications but you want to deploy servlets, then you need to register at least one context and at least the ServletHandler with the server. You are able to statically configure individual servlets at a specific URL pattern, or use dynamic mapping to extract servlet names from the request URL.
     9
     10The ServletHandler can be used with a HttpServer:
     11{{{
     12HttpServer server = new HttpServer();
     13server.addListener(":8080");
     14HttpContext context = server.getContext("/");
     15ServletHandler handler= new ServletHandler();
     16handler.addServlet("Dump","/dump/*",
     17                   "org.mortbay.servlet.Dump");
     18context.addHandler(handler);
     19}}}
     20Code Example:  Using ServletHandler in HttpServer
     21
     22Alternately, the org.mortbay.jetty.Server can be used instead of a HttpServer, so that it's conveniance methods may be used:
     23{{{
     24Server server = new Server();
     25server.addListener(":8080");
     26ServletHttpContext context = (ServletHttpContext)
     27    server.getContext("/");
     28context.addServlet("Dump","/dump/*",
     29                   "org.mortbay.servlet.Dump");
     30}}}
     31Code Example:  Using ServletHandler in Server
     32
     33Using Static Servlet Mappings
     34The examples above used defined servlet mappings to map a request URL to a servlet. Prefix (eg "/dump/*"), suffix (eg. "*.jsp"), exact (eg "/path") or default ("/") mappings may be used and they are all within the scope of the context path:
     35{{{
     36#!java
     37Server server = new Server();
     38server.addListener(":8080");
     39ServletHttpContext context = (ServletHttpContext)
     40    server.getContext("/context");
     41context.addServlet("Dump","/dump/*",
     42                   "org.mortbay.servlet.Dump");
     43context.addServlet("Dump","/dump/session",
     44                   "org.mortbay.servlet.SessionDump");
     45context.addServlet("JSP","*.jsp",
     46                   "org.apache.jasper.servlet.JspServlet");
     47context.addServlet("Default","/",
     48                   "org.mortbay.jetty.servlet.Default");
     49}}}
     50Code Example:  Static servlet mappings
     51
     52Examples of URLs that will be mapped to these servlets are:
     53 || /context/dump        || Dump Servlet by prefix ||
     54 || /context/dump/info  ||      Dump Servlet by prefix ||
     55 || /context/dump/session  ||   SessionDump Servlet by exact ||
     56 || /context/welcome.jsp         || JSP Servlet by suffix ||
     57 || /context/dump/other.jsp  ||         Dump Servlet by prefix ||
     58 || /context/anythingelse        || Default Servlet ||
     59 || /anythingelse        || Not this context ||
     60
     61Using Dynamic Servlets
     62Servlets can be discovered dynamically by using the org.mortbay.jetty.servlet.Invoker servlet. This servlet uses the request URI to determine a servlet class or the name of a previously registered servlet:
     63{{{
     64#!java
     65Server server = new Server();
     66server.addListener(":8080");
     67ServletHttpContext context = (ServletHttpContext)
     68    server.getContext("/context");
     69context.addServlet("Dump","/dump/*",
     70                   "org.mortbay.servlet.Dump");
     71context.addServlet("Invoker","/servlet/*",
     72                   "org.mortbay.jetty.servlet.Invoker");
     73}}}
     74Code Example:  Dynamic servlet mappings
     75
     76Examples of URLs that will be mapped to these servlets are:
     77 || /servlet/Dump        || Dump Servlet by name ||
     78 || /servlet/com.acme.MyServlet/info  ||        Servlet by dynamic class ||
     79 || /servlet/com.mortbay.servlet.Dump  ||       Dump Servlet by class or ERROR ||
     80
     81By default, the Invoker will only load servlets from the context classloader, so the last URL above will result in an error. The Invoker can be configured to allow any servlet to be run, but this can be a secuirty issue.
     82
     83Deploying Web Applications
     84
     85The Servlet Specification details a standard layout for web applications. If your content is packaged according to these specifications, then simply call the addWebApplication(...)methods on the  org.mortbay.jetty.Server instance, specifying at minimum a context path, the directory or war file of your application. Jetty is then able to discover and configure all the required handlers including security, static content and servlets.
     86
     87The addWebApplication(...)methods transparently create and return an instance of WebApplicationContext which contains a WebApplicationHandler.
     88
     89The WebApplicationHandler extends ServletHandler and as well as servlets, it provides standard security and filters. Normally it is configured by the webdefault.xml file to contain Invoker, JSP and Default servlets. Filters, Servlets and other mechanisms are configured from the WEB-INF/web.xml file within the web application.
     90
     91This example configures a web application located in the directory ./webapps/myapp/ at the context path / for a virtual host myhost:
     92
     93server.addWebApplication("myhost","/","./webapps/myapp/");
     94
     95Code Example: Configuring a web application
     96
     97The arguments to the addWebApplication method are:
     98
     99    * An (optional) virtual host name for the context
     100    * A context path
     101    * The location of the web application, which may be a directory structure or a war file, given as a URL, war filename or a directory name.
     102
     103The addWebApplication method is overloaded to accommodate the parameters marked as (optional).
     104
     105
     106Note: If you run Jetty within JBoss, then you should NOT use the addWebApplication API (or XML), as this bypasses the JBoss classloaders. Use the JBoss deployment mechanisms instead and only use the Jetty configuration for listeners and logs.
     107
     108
     109Multiple Web Applications
     110
     111To make things even easier, if you have multiple web apps to deploy, you can accomplish this with a single method call:
     112
     113server.addWebApplications ("myhost","./webapps/");
     114
     115Code Example: Configuring multiple web apps
     116
     117
     118Given the code above, Jetty would look in the directory "./webapps/" for all war files and subdirectories, and configure itself with each web application specified therein. For example, assuming the directory webapps contained the war files webapps/root.war, webapps/customer.war and webapps/admin.war, then Jetty would create the contexts "/", "/customer/*" and "/admin/*" mapped to the respective war files. NOTE the special mapping of war files (or directories) named root to the context "/".
     119
     120In order to actually deploy the web application, it is also necessary to configure a port listener. The full code example to deploy the web application in the code snippet is:
     121{{{
     122#!java
     123Server server = new Server();
     124SocketListener listener = new SocketListener();
     125listener.setPort(8080);
     126server.addListener(listener );
     127server.addWebApplication("myhost","./webapps/myapp/");
     128server.start();
     129}}}
     130Code Example:  Deploying a web application
     131
     132
     133Using XML
     134
     135The same web application can be deployed instead via an XML configuration file instead of calls to the API. The name of the file is passed to Jetty as an argument on the command line (see the section on  Jetty demonstrations for instructions). The following excerpt deploys the same web application as given in the code example above:
     136{{{
     137#!xml
     138                             
     139<Configure class="org.mortbay.jetty.Server">
     140  <Call name="addListener">
     141    <Arg>
     142      <New class="org.mortbay.http.SocketListener">
     143          <Set name="Port">
     144            <SystemProperty name="jetty.port"
     145             default="8080"/>
     146          </Set>
     147      </New>
     148    </Arg>
     149  </Call>
     150
     151  <Call name="addWebApplication">
     152    <Arg>/</Arg>
     153    <Arg><SystemProperty name="jetty.home"
     154          default="."/>/webapps/myapp
     155    </Arg>
     156  </Call>
     157 </Configure>
     158
     159}}}