-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rafał Rusin
committed
Jul 18, 2010
1 parent
fa7d296
commit 3c114a3
Showing
5 changed files
with
64 additions
and
13 deletions.
There are no files selected for viewing
37 changes: 29 additions & 8 deletions
37
example-helloworld/src/main/java/org/apache/jetty/service/example/HelloWorld.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,64 @@ | ||
package org.apache.jetty.service.example; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.apache.jetty.service.api.JettyService; | ||
import org.mortbay.jetty.Handler; | ||
import org.mortbay.jetty.HttpConnection; | ||
import org.mortbay.jetty.Request; | ||
import org.mortbay.jetty.handler.AbstractHandler; | ||
import org.mortbay.jetty.handler.ContextHandler; | ||
import org.mortbay.jetty.handler.ContextHandlerCollection; | ||
import org.mortbay.jetty.handler.ResourceHandler; | ||
import org.mortbay.resource.FileResource; | ||
import org.mortbay.resource.Resource; | ||
|
||
public class HelloWorld { | ||
|
||
private JettyService jettyService; | ||
private ContextHandlerCollection handler; | ||
private Handler reg; | ||
|
||
public void setJettyService(JettyService jettyService) { | ||
this.jettyService = jettyService; | ||
} | ||
|
||
public void init() { | ||
public void init() throws Exception { | ||
handler = new ContextHandlerCollection(); | ||
handler.addContext("app", "app").setHandler( | ||
new AbstractHandler() { | ||
|
||
handler.addContext("/app", "app").setHandler(new AbstractHandler() { | ||
public void handle(String target, HttpServletRequest request, | ||
HttpServletResponse response, int arg3) throws IOException, | ||
ServletException { | ||
response.setContentType("text/html"); | ||
response.setStatus(HttpServletResponse.SC_OK); | ||
response.getWriter().println("Hello World Handler"); | ||
response.getWriter().println("<h1>Hello World Handler</h1>"); | ||
|
||
Request base_request = (request instanceof Request) ? (Request)request:HttpConnection.getCurrentConnection().getRequest(); | ||
base_request.setHandled(true); | ||
} | ||
}); | ||
handler.addContext("","static").setHandler(new ResourceHandler()); | ||
ResourceHandler r = new ResourceHandler(); | ||
|
||
Resource r2 = new FileResource(getClass().getResource("/static")); | ||
r.setBaseResource(r2); | ||
ContextHandler ch = handler.addContext("",""); | ||
ch.setHandler(r); | ||
ch.setClassLoader(getClass().getClassLoader()); | ||
|
||
jettyService.registerApp("helloWorld", handler); | ||
reg = jettyService.registerApp("helloWorld", handler); | ||
} | ||
|
||
public void destroy() { | ||
jettyService.unregisterApp(handler); | ||
public void destroy() throws Exception { | ||
jettyService.unregisterApp(reg); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import org.apache.jetty.service.JettyServiceImpl; | ||
import org.apache.jetty.service.example.HelloWorld; | ||
|
||
public class Test { | ||
public static void main(String[] args) throws Exception { | ||
JettyServiceImpl a = new JettyServiceImpl(); | ||
a.init(); | ||
|
||
HelloWorld h = new HelloWorld(); | ||
h.setJettyService(a); | ||
h.init(); | ||
Thread.sleep(100000); | ||
h.destroy(); | ||
a.destroy(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters