<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'ajaxTest.jsp' starting page</title>
<script type="text/javascript" src="ajaxTest.js"></script>
</head>
<body>
enter :
<input type="text" name="sth" id="sth" />
<input type="button" value="查看输入的内容" οnclick="myRequest()"/>
<br />
<label id="out"></label>
</body>
</html>
ajaxTest.js
var http_request = false;
function sendRequest(method, url, content, responseType, callback) {
http_request = false;
if (window.XMLHttpRequest) {
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType("text/xml");
}
} else {
if (window.ActiveXObject) {
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
}
}
}
}
if (!http_request) {
window.alert("\不\能\创\建XMLHttpRequest\对\象\实\例");
return false;
}
if (responseType.toLowerCase() == "text") {
http_request.onreadystatechange = callback;
} else {
if (responseType.toLowerCase() == "xml") {
http_request.onreadystatechange = callback;
} else {
window.alert("\相\应\参\数\类\别\错\误");
return false;
}
}
if (method.toLowerCase() == "get") {
http_request.open(method, url, true);
} else {
if (method.toLowerCase() == "post") {
http_request.open(method, url, true);
http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
} else {
window.alert("http\请\求\类\别\参\数\错\误");
return false;
}
}
http_request.send(content);
}
function processResponse() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
var msg = document.getElementById("out");
if (msg != undefined) {
msg.innerText = http_request.responseText;
}
} else {
alert("\您\所\请\求\的\页\面\有\异\常");
}
}
}
function myRequest(path) {
var name;
if (document.getElementById("sth")!= undefined) {
name = document.getElementById("sth").value;
}
var url = "servlet/AjaxTest?time=" + Math.random();
var content = "sth=" + name;
sendRequest("POST", url, content, "TEXT", processResponse);
}
servlet
package com.bonree.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AjaxTest extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
String name = request.getParameter("sth");
response.getWriter().write("输入的内容为:" + name);
}
}
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Ajax get请求示例
正好早上有时间,代码:$.ajax({ type:'GET', url:'proinfo',
ajax json数据 数据 -
Jquery Ajax 请求示例
目录一、引入jquery文件二、完整ajax例子一、引入jquery文件在使用jquery ajax之前,需要先引入jquery
jquery ajax javascript json -
Ajax异步请求登录
Ajax异步请求登录,用到了第三方的一个JqueryUI封装好了的方法,用动软生成的数据
数据 Ajax异步请求登录 -
Ajax系列——XMLHttpRequest实现Ajax异步请求
1、用原生的JS代码实现AJAX页面关键部分的JS如下://创建XMLH
WEB前端 xml 服务器 请求参数 -
311 同步异步概述,Ajax 异步请求
09.Ajax异步请求.html
ajax html 数据 javascript 异步请求 -
teslak80对比1080
目录1. 介绍2.准备工作3.步骤3.1 进入 google driver:https://drive.google.com3.2 右键上传项目所在的文件夹:3.3 进入 colab:点击左上角 “新建” --> “更多” --> “Colaboratory”3.4 “Edit” --> “Notebook settings” -->3.5 代码段里面输入3.6 代码段里
teslak80对比1080 Google Colab Google Driver Pytorch 上传