这篇文章主要讲解了“如何通过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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。