Java 用jetty實現HTTP伺服器 獲取 Get 請求體

海乐学习發表於2024-08-06

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>

相關文章