close
Warning:
Can't synchronize with repository "(default)" (Unsupported version control system "svn": /usr/lib/python2.7/dist-packages/libsvn/_fs.so: failed to map segment from shared object: Cannot allocate memory). Look in the Trac log for more information.
- Timestamp:
-
Aug 13, 2009, 1:30:09 PM (16 years ago)
- Author:
-
waue
- Comment:
-
--
Legend:
- Unmodified
- Added
- Removed
- Modified
-
v1
|
v2
|
|
1 | | 鑲嵌 jetty code |
| 1 | 鑲嵌 jetty 5.1.x code |
2 | 2 | |
3 | | |
4 | | |
| 3 | = ServletHandler = |
5 | 4 | |
6 | 5 | Using the ServletHandler |
7 | 6 | |
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. |
| 7 | 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. |
9 | 8 | |
10 | | The ServletHandler can be used with a HttpServer: |
| 9 | == HttpServer == |
| 10 | 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. |
| 11 | |
| 12 | * Code Example: Using ServletHandler in HttpServer |
| 13 | |
11 | 14 | {{{ |
| 15 | #!java |
12 | 16 | HttpServer server = new HttpServer(); |
13 | 17 | server.addListener(":8080"); |
… |
… |
|
18 | 22 | context.addHandler(handler); |
19 | 23 | }}} |
20 | | Code Example: Using ServletHandler in HttpServer |
| 24 | |
| 25 | == org.mortbay.jetty.Server == |
21 | 26 | |
22 | 27 | Alternately, 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 |
23 | 30 | {{{ |
| 31 | #!java |
24 | 32 | Server server = new Server(); |
25 | 33 | server.addListener(":8080"); |
… |
… |
|
29 | 37 | "org.mortbay.servlet.Dump"); |
30 | 38 | }}} |
31 | | Code Example: Using ServletHandler in Server |
32 | 39 | |
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 | |
| 45 | The examples above used defined servlet mappings to map a request URL to a servlet. |
| 46 | |
| 47 | 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: |
| 48 | |
| 49 | * Code Example: Static servlet mappings |
35 | 50 | {{{ |
36 | 51 | #!java |
… |
… |
|
47 | 62 | context.addServlet("Default","/", |
48 | 63 | "org.mortbay.jetty.servlet.Default"); |
| 64 | |
49 | 65 | }}} |
50 | | Code Example: Static servlet mappings |
| 66 | |
51 | 67 | |
52 | 68 | Examples of URLs that will be mapped to these servlets are: |
… |
… |
|
59 | 75 | || /anythingelse || Not this context || |
60 | 76 | |
61 | | Using Dynamic Servlets |
| 77 | === Using Dynamic Servlets === |
| 78 | |
62 | 79 | Servlets 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 | |
63 | 83 | {{{ |
64 | 84 | #!java |
… |
… |
|
72 | 92 | "org.mortbay.jetty.servlet.Invoker"); |
73 | 93 | }}} |
74 | | Code Example: Dynamic servlet mappings |
| 94 | |
75 | 95 | |
76 | 96 | Examples of URLs that will be mapped to these servlets are: |
… |
… |
|
81 | 101 | By 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 | 102 | |
83 | | Deploying Web Applications |
| 103 | === Deploying Web Applications === |
84 | 104 | |
85 | 105 | The 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. |
… |
… |
|
91 | 111 | This example configures a web application located in the directory ./webapps/myapp/ at the context path / for a virtual host myhost: |
92 | 112 | |
| 113 | * Code Example: Configuring a web application |
| 114 | |
| 115 | {{{ |
| 116 | #!sh |
93 | 117 | server.addWebApplication("myhost","/","./webapps/myapp/"); |
| 118 | }}} |
94 | 119 | |
95 | | Code Example: Configuring a web application |
96 | 120 | |
97 | 121 | The arguments to the addWebApplication method are: |
… |
… |
|
107 | 131 | |
108 | 132 | |
109 | | Multiple Web Applications |
| 133 | === Multiple Web Applications === |
110 | 134 | |
111 | 135 | To make things even easier, if you have multiple web apps to deploy, you can accomplish this with a single method call: |
112 | 136 | |
| 137 | * Code Example: Configuring multiple web apps |
| 138 | {{{ |
113 | 139 | server.addWebApplications ("myhost","./webapps/"); |
114 | | |
115 | | Code Example: Configuring multiple web apps |
116 | | |
| 140 | }}} |
117 | 141 | |
118 | 142 | Given 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 | 143 | |
120 | 144 | In 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 | |
121 | 148 | {{{ |
122 | 149 | #!java |
… |
… |
|
128 | 155 | server.start(); |
129 | 156 | }}} |
130 | | Code Example: Deploying a web application |
131 | 157 | |
132 | 158 | |
133 | | Using XML |
| 159 | |
| 160 | = Using XML = |
134 | 161 | |
135 | 162 | The 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 | |
136 | 164 | {{{ |
137 | 165 | #!xml |
… |
… |
|
158 | 186 | |
159 | 187 | }}} |
| 188 | |