html ajax java 问题。 我想让html 通过ajax与java通信,是下面这种方法吗?
html:<!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/ht...
html:<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Ajax测试</title> <script type="text/javascript"> var xmlHttp; function createXmlHttp(){ if(window.XMLHttpRequest) { //针对 Firefox, Chrome, Opera, Safari,IE7,IE8 xmlHttp = new XMLHttpRequest(); } else if(window.ActiveXObject) { //针对IE6,IE5 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } } function verify() { var username = document.getElementById("username").value; //第一步:创建XMLHttpRequest对象 createXmlHttp(); //第二步:注册回调函数 xmlHttp.onreadystatechange = callBack; //第三步:设置连接信息 xmlHttp.open("GET","Hello_02/src/com/zyh/first/AjaxServlet?username="+username,true); //第四步:向服务器发送数据 xmlHttp.send(null); } function callBack() { //第五步:接收服务器返回数据 //判断对象的是否交互完成 if(xmlHttp.readyState == 4) { //判断Http的交互是否成功 if(xmlHttp.status == 200) { //获取服务器返回数据 //获取服务器输出的纯文本格式 var responseText = xmlHttp.responseText; //将数据显示在页面 var usernameMsg = document.getElementById("usernameMsg"); usernameMsg.innerHTML = responseText; } } } </script> </head> <body> <form method="post" action="servlet/AjaxServlet"> 用户名:<input type="text" name="username" id="username" /><span id="usernameMsg"></span><br/> <input type="button" value="测试" onclick="verify();" /> </form> </body></html>
展开
展开全部
ajax调用java后台的方法,其实是通过url链接来访问,示例如下:
package com.xxxx.xxxx.servlet;
import 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;
package com.xxxx.xxxx.servlet;
import 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;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询