栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

024

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

024

1. fetch更加简单的数据获取方式, 功能更加强大、更加灵活, 是基于Promise实现的。

2. fetch语法结构

fetch(url).then(fun1)
	.then(fun2)
	......
	.catch(fun)

3. 新建一个名为Promise的动态Web工程

3.1. 编写FetchAjax.java

package com.bjbs.fa;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FetchAjax extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String id = req.getParameter("id");
		resp.getWriter().write("Fetch Ajax get Request...   id = " + id);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}

3.2. 编写FetchAjaxPost.java

package com.bjbs.fa;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FetchAjaxPost extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String uname = req.getParameter("uname");
		String pwd = req.getParameter("pwd");
		resp.getWriter().write("Fetch Ajax post Request...   uname = " + uname + ", pwd = " + pwd);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}

3.3. 编写FetchAjaxPostJson.java

package com.bjbs.fa;

import java.io.BufferedReader;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FetchAjaxPostJson extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		BufferedReader br = req.getReader();
		String result = "", rl = null;
		while((rl = br.readLine()) != null) {
			result += rl;
		}
		System.out.println("Fetch Ajax post Request Json Param...   result = " + result);
		
		String res = "{"code":1,"result":"success."}";
		resp.getWriter().write(res);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}

3.4. 修改web.xml

 3.5. 编写FetchGet.html



	
		
		Fetch基本用法-get
	
	
		
	

3.6. 编写FetchPost.html



	
		
		Fetch基本用法-post
	
	
		
	

3.7. 运行项目, 访问FetchGet.html

3.8. 运行项目, 访问FetchPost.html 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/736635.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号