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


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

--

Legend:

Unmodified
Added
Removed
Modified
  • waue/2009/0813

    v1 v2  
    1 鑲嵌 jetty code
     1鑲嵌 jetty 5.1.x code
    22
    3 
    4 
     3 = ServletHandler =
    54
    65Using the ServletHandler
    76
    8 If 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.
     7If 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.
    98
    10 The ServletHandler can be used with a HttpServer:
     9 ==  HttpServer ==
     10You are able to statically configure individual servlets at a specific URL pattern, or use dynamic mapping to extract servlet names from the request URL.
     11
     12 * Code Example:  Using ServletHandler in HttpServer
     13
    1114{{{
     15#!java
    1216HttpServer server = new HttpServer();
    1317server.addListener(":8080");
     
    1822context.addHandler(handler);
    1923}}}
    20 Code Example:  Using ServletHandler in HttpServer
     24
     25 == org.mortbay.jetty.Server ==
    2126
    2227Alternately, the org.mortbay.jetty.Server can be used instead of a HttpServer, so that it's conveniance methods may be used:
     28
     29 * Code Example:  Using ServletHandler in Server
    2330{{{
     31#!java
    2432Server server = new Server();
    2533server.addListener(":8080");
     
    2937                   "org.mortbay.servlet.Dump");
    3038}}}
    31 Code Example:  Using ServletHandler in Server
    3239
    33 Using Static Servlet Mappings
    34 The 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:
     40
     41 ===  Static Servlet  ===
     42
     43 * Using Static Servlet Mappings
     44
     45The examples above used defined servlet mappings to map a request URL to a servlet.
     46
     47Prefix (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:
     48
     49 * Code Example:  Static servlet mappings
    3550{{{
    3651#!java
     
    4762context.addServlet("Default","/",
    4863                   "org.mortbay.jetty.servlet.Default");
     64
    4965}}}
    50 Code Example:  Static servlet mappings
     66
    5167
    5268Examples of URLs that will be mapped to these servlets are:
     
    5975 || /anythingelse        || Not this context ||
    6076
    61 Using Dynamic Servlets
     77=== Using Dynamic Servlets ===
     78
    6279Servlets 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:
     80
     81 * Code Example:  Dynamic servlet mappings
     82
    6383{{{
    6484#!java
     
    7292                   "org.mortbay.jetty.servlet.Invoker");
    7393}}}
    74 Code Example:  Dynamic servlet mappings
     94
    7595
    7696Examples of URLs that will be mapped to these servlets are:
     
    81101By 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.
    82102
    83 Deploying Web Applications
     103=== Deploying Web Applications ===
    84104
    85105The 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.
     
    91111This example configures a web application located in the directory ./webapps/myapp/ at the context path / for a virtual host myhost:
    92112
     113 * Code Example: Configuring a web application
     114
     115{{{
     116#!sh
    93117server.addWebApplication("myhost","/","./webapps/myapp/");
     118}}}
    94119
    95 Code Example: Configuring a web application
    96120
    97121The arguments to the addWebApplication method are:
     
    107131
    108132
    109 Multiple Web Applications
     133=== Multiple Web Applications ===
    110134
    111135To make things even easier, if you have multiple web apps to deploy, you can accomplish this with a single method call:
    112136
     137 * Code Example: Configuring multiple web apps
     138{{{
    113139server.addWebApplications ("myhost","./webapps/");
    114 
    115 Code Example: Configuring multiple web apps
    116 
     140}}}
    117141
    118142Given 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 "/".
    119143
    120144In 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:
     145
     146 * Code Example:  Deploying a web application
     147
    121148{{{
    122149#!java
     
    128155server.start();
    129156}}}
    130 Code Example:  Deploying a web application
    131157
    132158
    133 Using XML
     159
     160 = Using XML =
    134161
    135162The 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:
     163
    136164{{{
    137165#!xml
     
    158186
    159187}}}
     188