fetch异步简单版本(Tomcat 9)

smallRainFetch.jsp

html 复制代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>fetch</title>
</head>
<body>

</body>
</html>

<input type="text" id="inputone" name="inputNameOne"
	value="fetchGet服务" />
<button onclick="getFromServer()">fetch和服务器交互</button>

<div id="smallRainShow" style="width: 400px; height: 300px;"></div>

<script type="text/javascript">
function getFromServer() {

	const valueTo = document.getElementById('inputone').value;
	fetch('<%=request.getContextPath()%>/smallRainFetchServlet?inputNameOne=' + valueTo)
		.then(smallRainRes => {
			return smallRainRes.text();
		})
		.then(smallRainData => {
			document.getElementById("smallRainShow").innerHTML = smallRainData;
		})
		.catch(error => {
			alert("error");
		});
}
  </script>

SmallRainFetchServlet.java java文件

java 复制代码
package org.rain.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/smallRainFetchServlet")
public class SmallRainFetchServlet extends HttpServlet {
	@Override
	public void doPost(HttpServletRequest smallrainReq, HttpServletResponse smallRainRes) throws ServletException, IOException {
		doGet(smallrainReq, smallRainRes);
	}
	@Override
	public void doGet(HttpServletRequest smallrainReq, HttpServletResponse smallRainRes) throws ServletException, IOException {
		smallRainRes.setCharacterEncoding("UTF-8");
		PrintWriter smallrainOut = smallRainRes.getWriter();
		String smallRainWeb=smallrainReq.getParameter("inputNameOne");
		smallrainOut.print("得到服务器返回:"+smallRainWeb);
	}
}
相关推荐
我爱加班、、10 小时前
new Map()+Array.from()整理elementPlus的级联器数据
linux·前端·javascript
Hx_Ma1610 小时前
Map集合的5种遍历方式
java·前端·javascript
css趣多多10 小时前
render函数
前端·javascript·vue.js
小手cool10 小时前
Java 列表中查找最小值和最大值最有效率的方法
java
web打印社区10 小时前
前端开发实现PDF打印需求:从基础方案到专业解决方案
前端·vue.js·react.js·electron·pdf
时光追逐者10 小时前
使用 MWGA 帮助 7 万行 Winforms 程序快速迁移到 WEB 前端
前端·c#·.net
惊讶的猫10 小时前
多线程同步问题及解决
java·开发语言·jvm
wfsm10 小时前
工厂模式创建动态代理实现类
java·开发语言
好好研究10 小时前
总结SSM设置欢迎页的方式
xml·java·后端·mvc
Hui Baby10 小时前
java -jar 启动原理
java·pycharm·jar