As mentioned earlier, the most widely used jetty can be easily embedded In the application, rather than as an application server, the simplest demo is used below to demonstrate a simplest application
1, download and import dependencies
should first create an ordinary java project, and then import the dependent package into it
Regarding the acquisition of dependent packages, the first way is to import the jar package in the lib directory of the jetty server downloaded earlier.
is also on the jetty download page, above is the download link of jetty server, below you can see the download of the package Entrance:
select the first Jetty-9 to enter, you will See many versions of 9.x, click to download the latest:
After downloading, add the java package in the plugins to the project classpath.
2, write a demo
First write a processor to process the request from the web, TestController.java, the code is as follows:
To be precise, the Controller here should be a Handler, which must inherit from AbstractHandler, and rewrite the handle method to process the request. You can see that the Servlet API can be used directly here. By default, jetty requests After the completion, there will be a request for /favicon.ico in the background. Although it does not affect the front-end display, in order to prevent the back-end from repeating processing, a filter is performed here
After writing Handler, write an entry service to load the Handler and start the service, here is JettyService.java< /span>
import org.eclipse.jetty.server.Server;public class JettyService {public static void main(String[] args) throws Exception {Server server = new Server(8989); server.setHandler(new TestController()); server.start(); server.join(); }}
such a simple demo is finished, run the main method directly to start the service
Then open the browser to visit: http ://127.0.0.1:8989/?name=jetty can see the result below
In fact, the input here /xxx?name=xxx can be intercepted, because the target is obtained in the background, so it can be executed for different requests Different processing
You can see the following in the background Output:
can see the embedded jetty Programming is very simple and flexible. Here is just the simplest case. Many advanced programming methods and configurations are used in actual production.
Description:< /p>
package youling. studio.jetty;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 TestHandler extends AbstractHandler {@Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {//User request Uri //http://localhost:8080/index.html?name=rolin -> target=/index.html System.out.println("target is :" + target); //Set the http header information response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); response.setStatus(HttpServletResponse.SC_OK); baseRequest.setHandled(true); //Get the stream output to the browser PrintWriter out = response.getWriter(); if("/favicon.ico".equals(target)){//jetty will request this uri again at the end of each request by default System .out.println("request /favicon.ico"); out.write("404"); }else{ System.out.println("request other!"); out.print("hello jetty!
"); //Other request parameters if(request.getParameter("name") != null) {out.print("your name is "+request.getParameter("name")+" !
");}} }}
package youling.studio.jetty;import org.eclipse .jetty.server.Server;public class JettyService {public static void main(String[] args ) throws Exception {//Create jettyserver Server server = new Server(8080); //Set handler server.setHandler(new TestHandler()); //Start server.start(); server.join(); }) pre>pom file
< project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http:// maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">4.0.0 youling.studio groupId> test-jetty 0.0.1-SNAPSHOT jar test-jetty http://maven .apache.org UTF-8 project> junit junit 3.8.1 test org.eclipse.jetty jetty-server 9.2.22.v20170606 org.eclipse.jetty jetty-webapp 9.2.22.v20170606 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 hand le(String target, Request baseRequest, 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 span>= response.getWriter(); if(target.equals("/favicon.ico")) {System.out.println("1"); out.println("404");} else {System.out.println("2"); out.print("< h3>hello jetty!"); if(request.getParameter("name") != null) {out.print(request.getParameter( "name"));}} }}
< /p>
import org.eclipse.jetty. server.Server;public class JettyService {< span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important">public static void main(String[] args) throws Exception {Server server = new Server(8989); server.setHa ndler(new TestController()); server.start(); server.join(); }}
< /p>
WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 4554 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC