JQuery ajax Demo 实例

今天就把自己的Demo贴出来 和大家共同学习
现在就 Jquery ajax 的 $.ajax(),$.post(),$.get();

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


JAVA代碼 jqueryAjaxServer.java

package cn.com.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;

import cn.com.jdbc.Jdbc;

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");
String pass=request.getParameter("pass");
System.out.println("pass:"+pass);
Jdbc jdbc = new Jdbc();
int a = jdbc.FindName(account,pass);
if( a == 1){
out.print("Sorry,此用户名已经存在");
}
else{
out.print("此用户可以使用");
}
out.close();
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws IOException ,ServletException {
this.doGet(request, response);
}
}








JAVA代碼 Jdbc .java

package cn.com.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import cn.com.vo.SysOper;

public class Jdbc {

/**
* @param args
*/
private static Connection conn = null;//声明Connection对象
private Statement stmt = null;//声明Statement对象
private ResultSet rs = null;//声明ResultSet对象
static{

// 加载数据库驱动类
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 访问数据库的地址

}
public Jdbc(){

}

public static Connection getConnection(){
String url = "dbc:oracle:thin:@127.0.0.1:1521:myoracle";
//创建Connection对象
try {
conn = DriverManager.getConnection(url, "zj", "zj");


} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 创建Statement对象
return conn;
}

public int FindName(String name,String pass){
String sql ="select t.login_code,t.login_pass from t_sys_oper t where t.login_code = '"+name+"' and t.login_pass = '"+pass+"'";
System.out.println(sql);
int i = 0;
try {
conn = Jdbc.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
SysOper oper = null;

while(rs.next()){
oper = new SysOper();
oper.setName(rs.getString("login_code"));
oper.setPass(rs.getString("login_pass"));
i = 1;
}

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 执行SQL语句,返回记录集
return i;
}

}





下面是WEB.XML


Xml代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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">
<servlet>
<servlet-name>jqueryAjaxServer</servlet-name>
<servlet-class>com.june.servlet.jqueryAjaxServer</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>jqueryAjaxServer</servlet-name>
<url-pattern>/jqueryAjax</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>


下面是Jsp页面 第一个是 jqueryAjax.jsp 本页使用的是$.ajax()
<%@ page language="java" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>jquery ajax</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript" src="jquery/jquery-1.3.2.min.js"></script>
<script language="javascript">
$(function(){
$('.sumbit').click(function(){
if($('#account').val().length==0){
$('.hint').text("用户名不能位空").css({"background-color":"green"});
}
else{
$.ajax({
url:'jqueryAjax',
data:{account:$('#account').val(),pass:$('#pass').val()},
error:function(){
alert("error occured!!!");
},
success:function(data){
$('body').append("<div>"+data+"</div>").css("color","red");

}

});}
});
});



</script>
</head>

<body>
<h3 align="center">jquery AjaX</h3>
<hr>
<label>请输入用户名 :</label>
<input id="account" name="account" type="text">
<br>
<label>请输入密码 :</label>
<input id="pass" name="pass" type="text">
<input class="sumbit" type="button" value="检测">
<div class="hint">
</div>
</body>
</html>


第二个用的是 $.post()

Html代码
<%@ page language="java" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>jquery ajax</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript" src="jquery/jquery-1.3.2.min.js"></script>

<script language="javascript">
$(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"});
})
}
});
});
</script>
</head>

<body>
<h3 align="center">jquery Ajax</h3>
<hr>
<label>请输入用户名 :</label>
<input id="account" name="account" type="text">
<input class="sumbit" type="button" value="检测">
<div class="hint">
</div>
</body>
</html>

<%@ page language="java" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>jquery ajax</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script src="js/jquery-1.2.6.js" type="text/javascript"></script>
<script language="javascript">
$(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"});
})
}
});
});
</script>
</head>

<body>
<h3 align="center">jquery Ajax</h3>
<hr>
<label>请输入用户名 :</label>
<input id="account" name="account" type="text">
<input class="sumbit" type="button" value="检测">
<div class="hint">
</div>
</body>
</html>


第三个是用的$.get()

Html代码
<%@ page pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>jquery get</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript" src="jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
$(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"});
});
}
});
});
</script>

</head>

<body>
<h3 align="center">jquery AjaX</h3>
<hr>
<label>请输入用户名 :</label>
<input id="account" name="account" type="text">
<input class="sumbit" type="button" value="检测">
<div class="hint">
</div>
</body>
</html>

<%@ page pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>jquery get</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script src="js/jquery-1.2.6.js" type="text/javascript"></script>
<script type="text/javascript">
$(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"});
});
}
});
});
</script>

</head>

<body>
<h3 align="center">jquery AjaX</h3>
<hr>
<label>请输入用户名 :</label>
<input id="account" name="account" type="text">
<input class="sumbit" type="button" value="检测">
<div class="hint">
</div>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值