Java 用 jetty 开发 HTTP服务器 来 获取每次 GET请求中 的请求体
监听 本机IP地址 中的 /httpCli
端口 5011
Main.java
package com.JettyServer; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.handler.ContextHandler; import org.eclipse.jetty.server.handler.ContextHandlerCollection; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; /***/ public class App { public static void main( String[] args ) { System.out.println( "Hello Jetty Server !" );
com.JettyServer.App appMain = new com.JettyServer.App(); ContextHandlerCollection contexts = new ContextHandlerCollection();//region jetty-------- httpCli调试用-------------------- ServletContextHandler context1 = new ServletContextHandler(ServletContextHandler.SESSIONS); context1.setContextPath("/"); com.JettyServer.HttpServletCli httpServletCli = new com.JettyServer.HttpServletCli(appMain);//httpapi context1.addServlet(new ServletHolder(httpServletCli), "/httpCli");//httpapi contexts.addHandler(context1); //region jetty服务器启动------------------------------- Server server = new Server(5011);//设置端口 server.setHandler(contexts); try { server.start();//jetty服务器启动 System.out.println( "Jetty Server 启动成功。port : 5011 " ); server.join(); } catch (Exception e) { e.printStackTrace(); //logger.error(e.getMessage()); } //endregion ------------------------------------------------- }
HttpServletCli.java
package com.JettyServer; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; public class HttpServletCli extends HttpServlet { private com.JettyServer.App _appMain; public HttpServletCli(com.JettyServer.App app ) { this._appMain=app; } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { System.out.println("get response: " +response.toString()); System.out.println("get request: " +request.toString()); StringBuilder buffer = new StringBuilder(); String line; // 获取请求体的输入流 BufferedReader reader = request.getReader(); // 逐行读取数据 while ((line = reader.readLine()) != null) { buffer.append(line); } // 将读取的---请求体---数据转换为字符串 String data = buffer.toString(); System.out.println("get request 请求体: " +data); } @Override public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException { System.out.println("doPost "+ req.toString()); BufferedReader streamReader = new BufferedReader( new InputStreamReader(req.getInputStream(), "UTF-8")); StringBuilder responseStrBuilder = new StringBuilder(); String inputStr; while ((inputStr = streamReader.readLine()) != null) responseStrBuilder.append(inputStr); System.out.println("doPost "+responseStrBuilder.toString()); //允许跨域 res.setHeader("Access-Control-Allow-Origin", "*"); res.setHeader("Access-Control-Allow-Methods", "*"); res.setHeader("Access-Control-Max-Age", "3600"); res.addHeader("Access-Control-Allow-Headers", "*"); res.setHeader("Access-Control-Allow-Credentials", "*"); } }
pom.xml
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.4.4.v20170414</version> <type>jar</type> </dependency> </dependencies>
标签:HTTP,Get,jetty,server,new,import,JettyServer,com From: https://www.cnblogs.com/hailexuexi/p/18345045