类似公司框架Ajax与se…

前端之家收集整理的这篇文章主要介绍了类似公司框架Ajax与se…前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

http://www.cnblogs.com/yidong/archive/2009/08/21/1551208.html

Ajax与servlet交互的实例

1.JavaScript部分

复制代码

varreq;

varurl;
functiongetResult()
{
varf
= document.getElementByIdx_x( " form_pub " );
varkey
= f.s.options[f.s.selectedIndex].text; //获取对select中文本的引用
if (window.XMLHttpRequest)
{
req
= new XMLHttpRequest();
url
= " ajaxServlet?action= " + key + " &bm=UTF-8 " ;
}
else if (window.ActiveXObject)
{

req
= new ActiveXObject( " Microsoft.XMLHTTP " );
url
= " ajaxServlet?action= " + key + " &bm=gbk " ;
}
if (req)
{
req.open(
" GET " ,url, true );
req.setRequestHeader(
" Content-Type " , " text/html;charset=UTF-8 " );
// 这里如果不设定头部则会导致firfox发送数据错误,servlet接受到的参数为乱码,在IE中正常
req.onreadystatechange = complete;
req.send(
null );
// req.setRequestHeader("Content-Type","text/xml;charset=UTF-8");
}
}

functioncomplete(){
if (req.readyState == 4 )
{
if (req.status == 200 )
{
varitems
= document.getElementByIdx_x( " belong " );
//以下为解析返回的XML文档
varxmlDoc
= req.responseXML;
varNode
= xmlDoc.getElementsByTagName_r( " type_name " );
// varstr=newArray();
varstr = null ;
// 清空工作
items.innerHTML = "" ; //删除一个 select内的全部内容
for (vari = 0 ;i <</SPAN>Node.length;i++)
{

str=Node[i];
//alert(str.childNodes[0].nodeValue);
varobjectOption=document.createElement_x("option");
items.options.add(objectOption);
//firfox不支持innerText必须用textContent代替
if(window.ActiveXObject)
{objectOption.innerText
=str.childNodes[0].nodeValue;}
else
{objectOption.textContent
=str.childNodes[0].nodeValue;}
}
}
}
}

复制代码

servlet端:

复制代码

package ajax;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Data_GetConn.GetConn; // 这个包是自己写的 获取MysqL的引用
import java.sql. * ; // 这个包必须有!!




public class ajaxServlet extends HttpServlet{
// privatestaticfinalStringCONTENT_TYPE="text/xml;charset=UTF-8"; // 这里最好统一用UTF-8进行编码
public void init() throws ServletException{}
public void doGet(HttpServletRequestrequest,HttpServletResponseresponse) throws ServletException,IOException
{
response.setContentType(
" text/xml;charset=UTF-8 " );
//以下两句为取消在本地的缓存
response.setHeader(
" Cache-Control " , " no-cache " );
response.setHeader(
" Pragma " , " no-cache " );
PrintWriterout
= response.getWriter();
Stringaction
= request.getParameter( " action " );
Stringbm
= request.getParameter( " bm " );

if (( " gbk " ).equals(bm))
{
action
= new String(action.getBytes( " ISO-8859-1 " ), " gbk " ); // 将获得的数据用gbk从新编码!(感谢董卫老师)
}
else
{
action
= new String(action.getBytes( " ISO-8859-1 " ), " gbk " );
}
try
{
GetConnwq
= new GetConn();
Connectioncon
= wq.getCon();
Statementstmt
= con.createStatement();
ResultSetrs
= stmt.executeQuery( " selectitemsfromclasswheremain=' " + action + " ' " );
StringBuffersb
= new StringBuffer();
sb.append(
" " );
while (rs.next())
{

sb.append(
" " + rs.getString( 1 ) + " " );

}
// sb.append(""+action+"");
sb.append( " " );
out.write(sb.toString());//注意这里向jsp输出的流,在script中的截获方法
out.close();
stmt.close();
con.close();
}
catch (Exceptionex) { } } }
原文链接:https://www.f2er.com/ajax/163961.html

猜你在找的Ajax相关文章