(XMLHttpRequest对象,一般称之为ajax对象)异步地(当ajax对象发请求时,浏览
器不会销毁当前页面,用户任然可以对当前页面做其他操作)向服务器发送请求,
服务器送回部分数据(并不是一个完整的页面),利用这些数据更新当前页面。整
个过程,页面无刷新,不打断用户的操作
编程步骤:
step1,获得ajax对象
比如:
var xhr=getXhr()
step2,发请求:
方式一:get请求
xhr.open('get','check_username.do?username=zs',true)
请求参数、请求资源路径、是否异步
注意:
a,get请求必须将请求参数添加到请求资源路径的后面。
b,true表示异步请求、false表示同步请求。
异步请求:发请求时,浏览器不会销毁当前页面,用户可以对当前页面做
其他操作。
同步请求:发送请求时,浏览器不会销毁当前页面,用户不可以对当前页面
做其他操作。
xhr.onreadystatechange=f1
xhr.send(null)
方式二:post请求
xhr.open('post','','')
step3,编写服务器端的处理程序,一般服务器只需要返回部分的数据。
step4,编写事件处理函数。
function f1(){
if(xhr.readyState==4){
var txt=xhr.responseText
使用txt更新当前页面...
}
}
java服务器端处理ajax发送的请求,和处理其他请求是一样的,只是在客户端页面表现的不同,比如:执行页面发送删除请求,服务器端在执行删除后,页面是需要刷新的。
ajax最经典的用法是验证码,注册页面如果因为验证码输入错误要刷新,之前的信息重填,估计用户会崩溃,采用ajax异步发送请求,就不会影响之前填写的信息
ajax调用java后台的方法,其实是通过url链接来访问,示例如下:package com.xxxx.xxxx.servletimport java.io.IOException
import java.sql.Connection
import java.sql.PreparedStatement
import java.sql.ResultSet
import java.sql.SQLException
import javax.naming.Context
import javax.naming.InitialContext
import javax.servlet.ServletException
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import javax.sql.DataSource
public class oaLoginLimitedServlet extends HttpServlet {
private static final long serialVersionUID = 1L
private static Connection conn=null
private static PreparedStatement pstmt=null
public oaLoginLimitedServlet() {
super()
}
public void destroy() {
super.destroy()
}
public static String getCount(String userid)
{
String v_sql="....."
String v_count=""
try {
pstmt = conn.prepareStatement(v_sql)
pstmt.setString(1, userid)
ResultSet rs = pstmt.executeQuery()
while(rs.next()){
v_count = rs.getString(1)
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}finally{
try {
pstmt.close()
conn.close()
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
return v_count
}
public static Connection getConnection(){
Context ctx = null
try {
ctx = new InitialContext()
DataSource ds = (DataSource)ctx.lookup("jndiname")
conn = ds.getConnection()
} catch (Exception e) {
e.printStackTrace()
}
return conn
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String v_userid=request.getParameter("userid")
System.out.println(v_userid)
getConnection()
String v_count=getCount(v_userid)
response.setCharacterEncoding("UTF-8")
response.getWriter().write(v_count)
response.flushBuffer()
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response)
}
}
如果要前端能够访问到该servlet,需要将该servlet注册到 web.xml文件中。需要在web.xml文件中添加以下内容
[html] view plaincopy
<servlet>
<servlet-name>oaLoginLimitedServlet</servlet-name>
<servlet-class>com.xxxx.xxxx.servlet.oaLoginLimitedServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>oaLoginLimitedServlet</servlet-name>
<url-pattern>/oaLoginLimitedServlet</url-pattern>
</servlet-mapping>
重启相关服务。
通过ajax就可以调用了。
[html] view plaincopy
var msg = $.ajax({
type: "post",
url: ....+'/oaLoginLimitedServlet?userid='+ $('#act').val(),
async:false
}).responseText
https://zhidao.baidu.com/question/2201763852265627548.html