html中用ajax调用servlet,jquery请求servlet实现ajax异步请求的示例

ajax可以发送异步请求实现无刷新效果,但是使用javascript比较麻烦,就query提供了一些封装的方法 ,可以使得操作更为简单:

$.ajax()方法:

function sendRequest() {

$.ajax({

url: "Hello",

type: "GET",

dataType: "txt",

data: "name=zhangsan",

complete: function(result){

alert(result.responseText);

}

});

}

$.get()方法:

function sendRequestByGet(){

$.get("Hello","name=lisi",function(result){

alert(result);

});

}

$.post()方法:

function sendRequestByPost(){

$.post("Hello","name=wangwu",function(result){

alert(result);

});

}

$.load()方法:

//load代码等效使用如下$get()方法

/*

$get(url, data, function(result){

//将result数据填充到页面元素中

$(“#h2”).html(result);

});

*/

function load(){

$("h2").load("Hello","name=hahaha");

}

以上异步请求的Hello文件:

protected void doPost(HttpServletRequest req, HttpServletResponse resp)

throws ServletException, IOException {

String name=req.getParameter("name");

resp.getWriter().print(name);

}

以上这篇jquery请求servlet实现ajax异步请求的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。 ad51e517755f8fd6a7ec83ced4ecfaf3.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Servlet中发送POST请求可以使用以下步骤: 1. 创建一个URL对象,指定请求的目标地址。 ``` URL url = new URL("http://example.com/path/to/target"); ``` 2. 打开URL连接并设置请求方法为POST。 ``` HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); ``` 3. 设置请求头信息(可选)。 ``` connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); ``` 4. 设置请求体内容(如果有)。 ``` String requestBody = "param1=value1&param2=value2"; connection.setDoOutput(true); OutputStream outputStream = connection.getOutputStream(); outputStream.write(requestBody.getBytes()); outputStream.flush(); outputStream.close(); ``` 5. 获取响应结果。 ``` int responseCode = connection.getResponseCode(); String responseBody = ""; if (responseCode == HttpURLConnection.HTTP_OK) { InputStream inputStream = connection.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = reader.readLine()) != null) { responseBody += line; } reader.close(); inputStream.close(); } ``` 完整的示例代码如下: ``` protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String targetUrl = "http://example.com/path/to/target"; String requestBody = "param1=value1&param2=value2"; URL url = new URL(targetUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setDoOutput(true); OutputStream outputStream = connection.getOutputStream(); outputStream.write(requestBody.getBytes()); outputStream.flush(); outputStream.close(); int responseCode = connection.getResponseCode(); String responseBody = ""; if (responseCode == HttpURLConnection.HTTP_OK) { InputStream inputStream = connection.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = reader.readLine()) != null) { responseBody += line; } reader.close(); inputStream.close(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值