原生ajax

前台:
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>
<%
String basePath = request.getScheme() + “😕/” + request.getServerName() + “:” + request.getServerPort()
+ request.getContextPath() + “/”;
%>

Insert title here
<button onclick="dj()">点击</button>

<br />
<br />

<div style="width: 200px; height: 200px; background-color: pink" id="msg">
	
</div>

<br />
<br />

<div style="width: 200px; height: 200px; background-color: pink">
	abcdef
</div>

后台 :

package com.wkcto.servlet;

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

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet1 extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
	System.out.println("进入servlet1");
	
	/*
	 * 
	 * 如果servlet接收的请求为传统的全局刷新的请求,那么我们以下的响应流就是为浏览器响应回数据(html代码)
	 * 
	 * 如果servlet接收的请求为使用ajax技术发送的局部刷新的请求,那么我们以下的响应流就是为ajax提供返回值
	 * 												out.print的内容就是返回值
	 * 
	 */
	
	/*try {
		Thread.sleep(3000);
	} catch (InterruptedException e) {
		e.printStackTrace();
	}*/
	
	String str1 = request.getParameter("str1");
	String str2 = request.getParameter("str2");
	
	System.out.println(str1);
	System.out.println(str2);
	
	//为ajax 响应数据
	PrintWriter out = response.getWriter();
	out.print(str1+";"+str2);
	out.close();
	
	
	
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
	this.doGet(request, response);

}

}

发布了266 篇原创文章 · 获赞 33 · 访问量 7万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览