您好,登錄后才能下訂單哦!
這篇文章主要講解了“如何通過Java代碼實現Jetty的Http服務器”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“如何通過Java代碼實現Jetty的Http服務器”吧!
Jetty是一個輕量級的高度可擴展的基于 java的web服務器和servlet引擎。
下面是 使用 Intellij IDEA 的maven項目 通過Java代碼,實現 Jetty 的 Http服務器 從而獲取到 Http 的 Get 和 Post 請求
<!-- Jetty。要注意版本,版本匹配不對時,運行Jetty的應用將會報錯--> <dependency> <groupId>org.eclipse.jetty.aggregate</groupId> <artifactId>jetty-all</artifactId> <version>8.0.4.v20111024</version> </dependency>
點擊 刷新 圖標 系統會自動 加載 jetty
加載完成后 就會 出現org.eclipse.jetty.aggregate:jetty-all:8.0.4.v20111024
package com.SinaServer; 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; /** * @title: AttendentConsoleHttpServlet * @Author wxj * @Date: 2021/6/24 10:30 * @Version 1.0 */ public class AttendentConsoleHttpServlet extends HttpServlet { @Override public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException { System.out.println(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(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", "*"); // //Map<String ,String> map = JSON.parseObject(Base64.decode(responseStrBuilder.toString()),Map.class); //this.doGet(req, res); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) { response.setContentType("text/html;charset=utf-8"); response.setStatus(HttpServletResponse.SC_OK); //允許跨域 response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Access-Control-Allow-Methods", "*"); response.setHeader("Access-Control-Max-Age", "3600"); response.addHeader("Access-Control-Allow-Headers", "*"); response.setHeader("Access-Control-Allow-Credentials", "*"); // System.out.println("get request:"); Map<String, String[]> maps = request.getParameterMap(); HashMap<String,String> mapAction = new HashMap<>(); for (Map.Entry<String, String[]> entry : maps.entrySet()) { //String cs = entry.getKey() + ":"+ Arrays.toString(entry.getValue())+";"; String cs = entry.getKey() + ":"+ entry.getValue()[0]+";"; System.out.println(cs); mapAction.put(entry.getKey(),entry.getValue()[0]); } //如果包含action請求 String strResponse=""; if (mapAction.containsKey("action")){ switch (mapAction.get("action")){ case "query": strResponse=this.query(); break; default: } } // try { response.getWriter().println(strResponse); } catch (IOException e) { e.printStackTrace(); } } public String query(){ String json="Hello Word! 已收到請求"; return json; } }
package com.SinaServer; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.handler.ContextHandlerCollection; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; /** * Hello world! * */ public class App { public static void main( String[] args ) { System.out.println( "Hello World!111222" ); //jetty的處理集合 ContextHandlerCollection contexts = new ContextHandlerCollection(); //jetty http請求事件 ServletContextHandler context1 = new ServletContextHandler(ServletContextHandler.SESSIONS); //httpapi AttendentConsoleHttpServlet httpServletCli= new AttendentConsoleHttpServlet(); context1.addServlet(new ServletHolder(httpServletCli), "/httpCli");//httpapi contexts.addHandler(context1); //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(); System.out.println(e.getMessage()); } } }
啟動成功后 用瀏覽器 訪問
http://192.168.1.143:5011/httpCli?action=query
感謝各位的閱讀,以上就是“如何通過Java代碼實現Jetty的Http服務器”的內容了,經過本文的學習后,相信大家對如何通過Java代碼實現Jetty的Http服務器這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。