博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jquery ajax例子
阅读量:5782 次
发布时间:2019-06-18

本文共 3495 字,大约阅读时间需要 11 分钟。

(1)取得服务端当前时间

jquery对象.load(url,sendData,function(backData,textStatus,xhr){... ...})
load():如果无参的话,就以GET方式发送
如果有参的话,就以POST方式发送
(2)检查注册用户名是否存在
$.get(url,sendData,function(backData,textStatus,xhr){... ...})
$.post(url,sendData,function(backData,textStatus,xhr){... ...})

getTime.jsp

1 <%@ page language="java" pageEncoding="UTF-8"%> 2  3  4    5        6    7    8        9       

10
11 27 28

TimeServlet.java

1 package cn.itcast.web.servlet; 2  3 import java.io.IOException; 4 import java.text.DateFormat; 5 import java.util.Date; 6 import java.util.Locale; 7 import javax.servlet.ServletException; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest;10 import javax.servlet.http.HttpServletResponse;11 12 public class TimeServlet extends HttpServlet {13     public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {14         DateFormat df = DateFormat.getDateTimeInstance(15                 DateFormat.FULL,16                 DateFormat.DEFAULT, 17                 Locale.CHINA);18         String now = df.format(new Date());19         response.setContentType("text/html;charset=UTF-8");20         response.getWriter().write(now);21     }22     public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {23         request.setCharacterEncoding("UTF-8");24         response.setContentType("text/html;charset=UTF-8");25         String ip = request.getRemoteAddr();26         if(ip.equals("127.0.0.1")){27             String username = request.getParameter("username");28             String password = request.getParameter("password");29             response.getWriter().write(username+":"+password);30         }31     }32 }

 

ajax_2.jsp  调用$.get()方法

1 <%@ page language="java" pageEncoding="UTF-8"%> 2  3  4    5        6    7    8     用户名[GET]: 9      40   41 

ajax_3.jsp调用 $.post()方法

1 <%@ page language="java" pageEncoding="UTF-8"%> 2  3  4    5        6    7    8     用户名[POST]: 9     10      42   43 

UserServlet.java

1 package cn.itcast.web.servlet; 2  3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8  9 public class UserServlet extends HttpServlet {10     public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {11         String username = request.getParameter("username");12         byte[] buf = username.getBytes("ISO8859-1");13         username = new String(buf,"UTF-8");14         String imagePath = "images/MsgSent.gif";15         if("哈哈".equals(username)){16             imagePath = "images/MsgError.gif";17         }18         response.setContentType("text/html;charset=UTF-8");19         response.getWriter().write(imagePath);20     }21     public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {22         request.setCharacterEncoding("UTF-8");23         String username = request.getParameter("username");24         String imagePath = "images/MsgSent.gif";25         if("哈哈".equals(username)){26             imagePath = "images/MsgError.gif";27         }28         response.setContentType("text/html;charset=UTF-8");29         response.getWriter().write(imagePath);30     }31 }

 

转载于:https://www.cnblogs.com/friends-wf/p/3810601.html

你可能感兴趣的文章
使用GitHub的十个最佳实践
查看>>
脱离“体验”和“安全”谈盈利的游戏运营 都是耍流氓
查看>>
慎用!BLEU评价NLP文本输出质量存在严重问题
查看>>
基于干净语言和好奇心的敏捷指导
查看>>
Node.js 2017企业用户调查结果发布
查看>>
“软”苹果水逆的一周:杂志服务崩溃,新机型遭泄露,芯片首架离职
查看>>
JAVA的优势就是劣势啊!
查看>>
ELK实战之logstash部署及基本语法
查看>>
帧中继环境下ospf的使用(点到点模式)
查看>>
BeanShell变量和方法的作用域
查看>>
LINUX下防恶意扫描软件PortSentry
查看>>
由数据库对sql的执行说JDBC的Statement和PreparedStatement
查看>>
springmvc+swagger2
查看>>
软件评测-信息安全-应用安全-资源控制-用户登录限制(上)
查看>>
我的友情链接
查看>>
Java Web Application 自架构 一 注解化配置
查看>>
如何 debug Proxy.pac文件
查看>>
Python 学习笔记 - 面向对象(特殊成员)
查看>>
Kubernetes 1.11 手动安装并启用ipvs
查看>>
Puppet 配置管理工具安装
查看>>