怎么才能学好ajax,ajax - 如何学好java - BlogJava

网上的 Jquery ajax Demo 大多都是基于php

很少 有java的 今天就把自己的Demo贴出来 和大家共同学习

现在就  Jquery ajax 的 $.ajax(),$.post(),$.get();

首先是  服务端的Servlet 演示这三个函数的用法对都是用的同一个 服务端

Java代码

package com.june.servlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpServletRequest;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

public class jqueryAjaxServer extends HttpServlet {

public jqueryAjaxServer(){

super();

}

public void doGet(HttpServletRequest request,HttpServletResponse response)

throws IOException ,ServletException {

response.setContentType("text/html;charset=utf-8");

PrintWriter out=response.getWriter();

String account=request.getParameter("account");

if("iamcrzay".equals(account)){

out.print("Sorry,the user is exist");

}

else{

out.print("Congratulation,this accont you can use!!!!");

}

out.close();

}

public void doPost(HttpServletRequest request,HttpServletResponse response)

throws IOException ,ServletException {

this.doGet(request, response);

}

}

package com.june.servlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpServletRequest;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

public class jqueryAjaxServer extends HttpServlet {

public jqueryAjaxServer(){

super();

}

public void doGet(HttpServletRequest request,HttpServletResponse response)

throws IOException ,ServletException {

response.setContentType("text/html;charset=utf-8");

PrintWriter out=response.getWriter();

String account=request.getParameter("account");

if("iamcrzay".equals(account)){

out.print("Sorry,the user is exist");

}

else{

out.print("Congratulation,this accont you can use!!!!");

}

out.close();

}

public void doPost(HttpServletRequest request,HttpServletResponse response)

throws IOException ,ServletException {

this.doGet(request, response);

}

}

下面是WEB.XML

Xml代码

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

jqueryAjaxServer

com.june.servlet.jqueryAjaxServer

jqueryAjaxServer

/jqueryAjax

index.jsp

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

jqueryAjaxServer

com.june.servlet.jqueryAjaxServer

jqueryAjaxServer

/jqueryAjax

index.jsp

下面是Jsp页面

第一个是 jqueryAjax.jsp  本页使用的是$.ajax()

Html代码

jquery ajax

$(function(){

$('.sumbit').click(function(){

if($('#account').val().length==0){

$('.hint').text("用户名不能位空").css({"background-color":"green"});

}

else{

$.ajax({

url:'jqueryAjax',

data:{account:$('#account').val()},

error:function(){

alert("error occured!!!");

},

success:function(data){

$('body').append("

"+data+"
").css("color","red");

}

});}

});

});

jquery AjaX


请输入用户名 :

jquery ajax

$(function(){

$('.sumbit').click(function(){

if($('#account').val().length==0){

$('.hint').text("用户名不能位空").css({"background-color":"green"});

}

else{

$.ajax({

url:'jqueryAjax',

data:{account:$('#account').val()},

error:function(){

alert("error occured!!!");

},

success:function(data){

$('body').append("

"+data+"
").css("color","red");

}

});}

});

});

jquery AjaX


请输入用户名 :

第二个用的是  $.post()

Html代码

jquery ajax

$(function(){

$('.sumbit').click(

function(){

if($('#account').val().length==0){

$('.hint').text("The account is cant't be null").css({"color":"red","background-color":"yellow"});

}

else{

$.post("jqueryAjax","account="+$('#account').val(),function(data){

$('.hint').text(data).css({"color":"red","background-color":"yellow"});

})

}

});

});

jquery Ajax


请输入用户名 :

jquery ajax

$(function(){

$('.sumbit').click(

function(){

if($('#account').val().length==0){

$('.hint').text("The account is cant't be null").css({"color":"red","background-color":"yellow"});

}

else{

$.post("jqueryAjax","account="+$('#account').val(),function(data){

$('.hint').text(data).css({"color":"red","background-color":"yellow"});

})

}

});

});

jquery Ajax


请输入用户名 :

第三个是用的$.get()

Html代码

jquery get

$(function(){

$('.sumbit').click(function(){

if($('#account').val().length==0){

$('.hint').html("用户名不能位空!!!").css({"color":"#ffoo11","background":"blue"});

}

else{

$.get("jqueryAjax","account="+$('#account').val(),

function(data){

$('.hint').html(data).css({"color":"#ffoo11","background":"green"});

});

}

});

});

jquery AjaX


请输入用户名 :

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值