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);
	}
}
相关推荐
冰暮流星7 分钟前
javascript之数组
java·前端·javascript
Re.不晚11 分钟前
JAVA进阶之路——无奖问答挑战3
java·开发语言
不倒翁玩偶29 分钟前
IDEA导入新的SpringBoot项目没有启动按钮
java·spring boot·intellij-idea
晚霞的不甘34 分钟前
Flutter for OpenHarmony天气卡片应用:用枚举与动画打造沉浸式多城市天气浏览体验
前端·flutter·云原生·前端框架
小小小米粒1 小时前
Maven Tools
java
xkxnq1 小时前
第五阶段:Vue3核心深度深挖(第74天)(Vue3计算属性进阶)
前端·javascript·vue.js
三小河1 小时前
Agent Skill与Rules的区别——以Cursor为例
前端·javascript·后端
kali-Myon1 小时前
2025春秋杯网络安全联赛冬季赛-day1
java·sql·安全·web安全·ai·php·web
Hilaku1 小时前
不要在简历上写精通 Vue3?来自面试官的真实劝退
前端·javascript·vue.js
我是咸鱼不闲呀1 小时前
力扣Hot100系列20(Java)——[动态规划]总结(下)( 单词拆分,最大递增子序列,乘积最大子数组 ,分割等和子集,最长有效括号)
java·leetcode·动态规划