ajax + servlet实现页面局部刷新

jsp页面部分代码:
<form name="myForm" method="get">
<input type="text" name="input1" />
<input type="button" value="Click" οnclick="ajaxFunctio();" />
<input type="text" name="output1" />
</form>


ajaxFunction所在的js代码,其中就有ajax的内容:
function ajaxFunction()
{

var xmlHttp;

try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{

// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{

try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("您的浏览器不支持AJAX!");
return false;
}
}
}

url="output.action?salt"+new Date();
xmlHttp.open("get",url,false);
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{ if(xmlHttp.status==200){
document.myForm.output1.value=xmlHttp.responseText.toString();
}
}
}
xmlHttp.send();
}


action中的get方法:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("here is the get function");
PrintWriter out = response.getWriter();
response.setContentType("text/html");
String input1=request.getParameter("input1");
System.out.println(input1);
if("mage".equalsIgnoreCase(input1)){
System.out.println("you have entered mage"); //不要怪我,我也是v大迷。
out.print("vurtne");
out.flush();
out.close();}else{
System.out.println("you have not entered mage");
out.print("drakedog"); //也不要怪我,我觉得dd打的也不错。
out.flush();
out.close();
}
}


web.xml配置内容:
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>OutputAction</servlet-name>
<servlet-class>com.test.action.OutputAction</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>OutputAction</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值