怎么才能学好ajax,ajax - 如何学好java - BlogJava
網(wǎng)上的 Jquery ajax Demo 大多都是基于php
很少 有java的 今天就把自己的Demo貼出來 和大家共同學(xué)習(xí)
現(xiàn)在就? Jquery ajax 的 $.ajax(),$.post(),$.get();
首先是? 服務(wù)端的Servlet 演示這三個函數(shù)的用法對都是用的同一個 服務(wù)端
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
請輸入用戶名 :
總結(jié)
以上是生活随笔為你收集整理的怎么才能学好ajax,ajax - 如何学好java - BlogJava的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: css expressionr,CSS自
- 下一篇: 服务器系统一键功能安装,一键安装服务器系