JSP + Servlet 实现 AJAX(纯JS版)

==============java 文件,包名org.rain.servlet

package org.rain.servlet;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.List;

import javax.servlet.ServletConfig;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.rain.bean.PageSystemBean;

@WebServlet("/mulAJAXPageSystem")

public class MulAJAXPage extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

try (PrintWriter out = response.getWriter()) {

out.println("<b> SmallRain AjAX 请求返回数据 </b");

}

}

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request, response);

}

}

======================JSP文件

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<html>

<head>

<meta charset="UTF-8">

<title>晓雨 JSP + Servlet 实现 AJAX</title>

<script>

function smallRainShowData() {

var smallrainAjAXRequest = new XMLHttpRequest();

smallrainAjAXRequest.open('GET', 'mulAJAXPageSystem', true);

smallrainAjAXRequest.send();

smallrainAjAXRequest.onreadystatechange = function() {

if (smallrainAjAXRequest.readyState === 4

&& smallrainAjAXRequest.status === 200) {

var showData = smallrainAjAXRequest.responseText;

document.getElementById('smallrainContent').innerHTML = showData;

}

}

}

</script>

</head>

<body>

<h1>晓雨 JSP + Servlet 实现 AJAX</h1>

<button οnclick="smallRainShowData()">AJAX 请求</button>

<div id="smallrainContent"></div>

</body>

</html>

相关推荐
拉不动的猪2 小时前
刷刷题50(常见的js数据通信与渲染问题)
前端·javascript·面试
拉不动的猪2 小时前
JS多线程Webworks中的几种实战场景演示
前端·javascript·面试
uhakadotcom3 小时前
Astro 框架:快速构建内容驱动型网站的利器
前端·javascript·面试
uhakadotcom3 小时前
了解Nest.js和Next.js:如何选择合适的框架
前端·javascript·面试
uhakadotcom3 小时前
Remix 框架:性能与易用性的完美结合
前端·javascript·面试
uhakadotcom4 小时前
Node.js 包管理器:npm vs pnpm
前端·javascript·面试
咖啡教室5 小时前
前端开发日常工作每日记录笔记(2019至2024合集)
前端·javascript
咖啡教室5 小时前
java日常开发笔记和开发问题记录
java
咖啡教室5 小时前
java练习项目记录笔记
java
咖啡教室5 小时前
前端开发中JavaScript、HTML、CSS常见避坑问题
前端·javascript·css