As mentioned earlier, Jetty's most widely used application is easy to embed in the application, rather than as an application server, the following is the simplest demo to demonstrate a simple application
1. Download and import dependencies
First, you should build a normal Java project and then put the dependency pack in.
The first way to obtain a dependency pack is to import a jar package from the Lib directory in the previously downloaded jetty server
Also on the Jetty download page, which is the download link to the jetty server, the download entry for the package can be seen below:
Choose the first Jetty-9 go in, you will see many versions of 9.x, click in to download the latest:
After downloading, add the Java package in plugins to the project Classpath
2, write a demo
First write a processor to handle the request from the Web, Testcontroller.java, code as follows:
Import java.io.IOException;
Import Java.io.PrintWriter;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Org.eclipse.jetty.server.Request;
Import Org.eclipse.jetty.server.handler.AbstractHandler; public class TestController extends Abstracthandler {@Override public void handle (String target, Request basereq
Uest, HttpServletRequest request, httpservletresponse response) throws IOException, Servletexception {
SYSTEM.OUT.PRINTLN (target); Response.setcontenttype ("text/html;
Charset=utf-8 ");
Request.setcharacterencoding ("Utf-8");
Response.setstatus (HTTPSERVLETRESPONSE.SC_OK);
Baserequest.sethandled (TRUE);
PrintWriter out = Response.getwriter ();
if (Target.equals ("/favicon.ico")) {System.out.println ("1");
Out.println ("404");
else {System.out.println ("2"); Out.print ("
To be exact, the controller should be handler, must inherit from Abstracthandler, and rewrite the handle method to process the request, and you can see that you can use the Servlet's API directly here, By default, the jetty request will have a/favicon.ico request in the background, although it does not affect the front end display, but in order to prevent back-end duplication, so here is a filter
Handler after writing, then write a portal service to load handler and start the service, here is Jettyservice.java
Import Org.eclipse.jetty.server.Server;
public class Jettyservice {public
static void Main (string[] args) throws Exception {
Server server = new Server (8 989);
Server.sethandler (New TestController ());
Server.start ();
Server.join ();
}
So a simple demo is finished, run the main method directly to start the service
Then open the browser access: Http://127.0.0.1:8989/?name=jetty can see the following results
In fact, input/xxx?name=xxx can be intercepted here because target is in the background, so different processing can be performed for different requests
The following output can be seen in the background:
You can see that embedded jetty programming is very simple and flexible, here is just one of the simplest cases, the actual production will also use a lot of advanced programming methods and configuration