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);
	}
}
相关推荐
TOPGUS18 小时前
谷歌Chrome浏览器即将对HTTP网站设卡:突出展示“始终使用安全连接”功能
前端·网络·chrome·http·搜索引擎·seo·数字营销
隐退山林18 小时前
JavaEE:多线程初阶(一)
java·开发语言·jvm
C_心欲无痕18 小时前
ts - 模板字面量类型与 `keyof` 的魔法组合:`keyof T & `on${string}`使用
linux·运维·开发语言·前端·ubuntu·typescript
最贪吃的虎18 小时前
Redis其实并不是线程安全的
java·开发语言·数据库·redis·后端·缓存·lua
一勺菠萝丶18 小时前
Java 后端想学 Vue,又想写浏览器插件?
java·前端·vue.js
xie_pin_an18 小时前
C++ 类和对象全解析:从基础语法到高级特性
java·jvm·c++
Tao____18 小时前
企业级物联网平台
java·网络·物联网·mqtt·网络协议
山峰哥18 小时前
数据库工程与SQL调优实战:从原理到案例的深度解析
java·数据库·sql·oracle·性能优化·编辑器
kaico201819 小时前
远程调用组件openfeign
java·spring cloud