当前位置: 首页 > news >正文

简单的ajax任务:get和post方式提交前端用户输入信息给服务器

简单的ajax任务:

用户输入用户名和密码,服务器接收后返回给浏览器展示用户名密码

get请求的方式:

package mypackage;

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

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

@WebServlet("/ajaxrequest1")
public class AjaxRequestServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        PrintWriter out = response.getWriter();
        out.print("<font color='red'>"+username+","+password+"</font>");
    }
}

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>发送ajax get请求</title>
</head>
<body>
    <script type="text/javascript">
        window.onload = function (){
            document.getElementById("btn").onclick = function (){
                //1.创建核心对象
                var xhr = new XMLHttpRequest();
                //2.注册回调函数
                xhr.onreadystatechange = function () {
                    if(this.readyState == 4){
                        if(this.status == 200){
                            document.getElementById("mydiv").innerHTML = this.responseText;
                        }else{
                            alert(this.status);
                        }
                    }
                }
                //3.开启通道
                var username = document.getElementById("name").value;
                var password = document.getElementById("pass").value;
                xhr.open("get","/xmm/ajaxrequest1?username="+username+"&password="+password,true);
                //4.发送请求
                xhr.send();
            }

        }
    </script>
    <input type="text" id="name" />
    <input type="password" id="pass"/>
    <button id="btn"> 发送ajax get请求</button>
    <div id="mydiv"></div>
</body>
</html>

post请求的方式:

package mypackage;

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

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

@WebServlet("/ajaxrequest2")
public class AjaxRequestServlet02 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        PrintWriter out = response.getWriter();
        out.print("<font color='red'>"+username+","+password+"</font>");
    }
}

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>发送ajax post请求</title>
</head>
<body>
    <script type="text/javascript">
        window.onload = function (){
            document.getElementById("btn").onclick = function (){
                //1.创建核心对象
                var xhr = new XMLHttpRequest();
                //2.注册回调函数
                xhr.onreadystatechange = function () {
                    if(this.readyState == 4){
                        if(this.status == 200){
                            document.getElementById("mydiv").innerHTML = this.responseText;
                        }else{
                            alert(this.status);
                        }
                    }
                }
                //3.开启通道
                xhr.open("post","/xmm/ajaxrequest2",true);
                //4.发送请求
                //ajax模拟表单提交,post方式必须加这一行,不然数据无法发送,且必须在open语句之后
                xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
                var username = document.getElementById("name").value;
                var password = document.getElementById("pass").value;
                xhr.send("username="+username+"&password="+password);
            }

        }
    </script>
<input type="text" id="name" />
<input type="password" id="pass"/>
<button id="btn"> 发送ajax post请求</button>
<div id="mydiv"></div>
</body>
</html>

相关文章:

  • Qt5开发从入门到精通——第四篇(调色板)
  • MySQL数据库如何线上修改表结构
  • Python利用pandas获取每行最大值和最小值
  • C++设计模式之装饰者模式(结构型模式)
  • 数据结构中,索引存储和散列存储区别较为详细的介绍
  • 基于ssm+vue的邮票收藏鉴赏系统 elementui
  • 去中心化标志符在DID中的核心地位
  • C++设计模式之适配器模式(结构型模式)
  • 3-面试官:说说线程池的 7 大参数
  • 猿创征文|HCIE-Security Day50:网络攻击介绍
  • 一个基于NetCore开发的前后端分离CMS系统
  • centos7安装docker和docker-compose
  • 子查询与内联结分别应该怎么写?
  • Shell编程之第一讲——基础知识认识
  • Java-基于SSM的校园点餐管理系统
  • 【翻译】Mashape是如何管理15000个API和微服务的(三)
  • Angular2开发踩坑系列-生产环境编译
  • C语言笔记(第一章:C语言编程)
  • JavaScript创建对象的四种方式
  • LeetCode18.四数之和 JavaScript
  • leetcode讲解--894. All Possible Full Binary Trees
  • Vue组件定义
  • 测试如何在敏捷团队中工作?
  • 对象管理器(defineProperty)学习笔记
  • 构建工具 - 收藏集 - 掘金
  • 前端技术周刊 2019-01-14:客户端存储
  • 如何利用MongoDB打造TOP榜小程序
  • 微信公众号开发小记——5.python微信红包
  • 硬币翻转问题,区间操作
  • “十年磨一剑”--有赞的HBase平台实践和应用之路 ...
  • CMake 入门1/5:基于阿里云 ECS搭建体验环境
  • HanLP分词命名实体提取详解
  • ​Distil-Whisper:比Whisper快6倍,体积小50%的语音识别模型
  • $$$$GB2312-80区位编码表$$$$
  • $.ajax中的eval及dataType
  • (2021|NIPS,扩散,无条件分数估计,条件分数估计)无分类器引导扩散
  • (39)STM32——FLASH闪存
  • (附源码)spring boot智能服药提醒app 毕业设计 102151
  • (教学思路 C#之类三)方法参数类型(ref、out、parmas)
  • (九)信息融合方式简介
  • (离散数学)逻辑连接词
  • (六) ES6 新特性 —— 迭代器(iterator)
  • (入门自用)--C++--抽象类--多态原理--虚表--1020
  • (十八)用JAVA编写MP3解码器——迷你播放器
  • (四)TensorRT | 基于 GPU 端的 Python 推理
  • (幽默漫画)有个程序员老公,是怎样的体验?
  • **Java有哪些悲观锁的实现_乐观锁、悲观锁、Redis分布式锁和Zookeeper分布式锁的实现以及流程原理...
  • .gitattributes 文件
  • .NET NPOI导出Excel详解
  • [ 第一章] JavaScript 简史
  • [.net] 如何在mail的加入正文显示图片
  • [] 与 [[]], -gt 与 > 的比较
  • [100天算法】-x 的平方根(day 61)
  • [20171102]视图v$session中process字段含义
  • [4.9福建四校联考]