java开发中jquery+ajax请求servlet用法总结
来源:互联网 发布:ubuntu 配置ssh服务器 编辑:程序博客网 时间:2024/06/01 22:46
本文总结了jquery和ajax请求servlet的基本用法,其中包括ajax的POST和GET请求方法,getJSON方法的使用
1.后台servlet的代码
package com.oracle.servlet;import java.io.IOException;import java.io.PrintWriter;import java.text.MessageFormat;import java.util.ArrayList;import java.util.HashMap;import java.util.Map;import java.util.Properties;import javax.mail.MessagingException;import javax.mail.Session;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.google.gson.Gson;import com.oracle.domain.User;import com.oracle.service.BaseServlet;import com.oracle.service.UserService;import org.junit.Test;import cn.itcast.commons.CommonUtils;import cn.itcast.mail.Mail;import cn.itcast.mail.MailUtils;public class TestServlet extends BaseServlet {public String testAjax(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {ArrayList<String> name = new ArrayList<>();name.add("1");name.add("2");name.add("3");req.setAttribute("list", name);return "f:/jsps/test2.jsp";}public void testAjax2(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String name = req.getParameter("name");String password = req.getParameter("password");System.out.println(name + ":" + password);PrintWriter out = resp.getWriter();out.print("1");out.flush();out.close();}public void testAjax3(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("t3");User user = new User();user.setUsername("zhangsan");user.setEmail("111");Gson gs = new Gson();String json = gs.toJson(user);resp.setContentType("text/x-javascript;charset=utf-8");resp.setHeader("cache-control", "no-cache");PrintWriter out = resp.getWriter();out.print(json);out.flush();out.close();}public void testAjax4(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("t4");String code = req.getParameter("code");System.out.println(code);ArrayList<User> list=new ArrayList<>();User user = new User();user.setUsername("zhangsan");list.add(user); user = new User();user.setUsername("lisi");list.add(user); user = new User();user.setUsername("wangwu");list.add(user);Gson gs = new Gson();String json = gs.toJson(list);resp.setContentType("text/x-javascript;charset=utf-8");resp.setHeader("cache-control", "no-cache");PrintWriter out = resp.getWriter();out.print(json);out.flush();out.close();}}2.前端jsp页面的代码
1)ajax post请求的javascript代码段
<pre name="code" class="html"> var name="zhangsan"; var password="1230";$(document).ready(function () {$.ajax({type : "POST",url : <span style="color:#ff6666;">"<%=path%>/TestServlt?method=testAjax2"</span>,data : {name:name,password:password}, success: function(msg) {alert(msg);},error: function(XMLHttpRequest, textStatus, errorThrown) {alert(XMLHttpRequest.status);}});});*tip (上面红字部分)记得要前面加入项目的请求域上下文路径,不然servlet接收不到请求
<%String path = request.getContextPath();%>
2)ajax getJSON方法请求的javascript代码段(返回的json为简单user对象)
$(document).ready(function () {$.getJSON("<%=path%>/TestServlet?method=testAjax3", function(data){var name=data.username; alert("JSON Data: " + data.username); });});3)ajax getJSON方法请求的javascript代码段(返回的json为集合list对象)
$(document).ready(function () {var code="1";$.getJSON("<%=path%>/TestServlet?method=testAjax4",{code:code},function(data){ $.each(data, function(i,item){ $("#s3").after("<p>"+item.username+"</p>"); }); });});
0 0
- java开发中jquery+ajax请求servlet用法总结
- java jquery ajax Servlet jsp页面异步请求验证用户名
- js中showModalDialog用法及jquery ajax请求的用法
- JQuery中ajax请求
- Jquery中ajax请求
- jQuery Ajax用法总结
- 前后端交互问题总结--JAVA/Servlet + HTML/Jquery/Ajax
- jquery ajax请求的用法($.ajax, $.getJSON)
- jquery的ajax请求servlet与响应
- jquery的ajax请求servlet与响应
- jquery中ajax用法
- jquery中发送ajax请求
- jQuery中终止Ajax请求
- jquery中ajax请求全过程
- jquery中ajax请求分类
- 关于Jquery中ajax方法data参数用法的总结
- 关于Jquery中ajax方法data参数用法的总结
- 关于Jquery中ajax方法data参数用法的总结
- 一条命令解决500 Error: Cannot find module 'XXX'
- 数据链路层
- 前端实现一个简单的表格分页
- 【转】linux 学习之路(学linux必看)
- 嵌入式学习--work8 SPI-FLASH(N25Q系列)FLASH
- java开发中jquery+ajax请求servlet用法总结
- Serialize and Deserialize Binary Tree
- tarjan算法讲解
- Hibernate update 只更新被修改字段
- 默然相愛.寂靜歡喜
- JAXB的工作原理与基本用法
- Linux ALSA声卡驱动之二:声卡的创建
- Mybatis int 判断非空的坑
- Hibernate笔记