我的漫漫程序之旅

专注于JavaWeb开发

Prototype1.6 实战11 (Ajax)

jsp:
<%@ page language="java" pageEncoding="gbk"%>
<html>
  
<head>
    
<title>Ajax Test</title>
  
<script type="text/javascript" src="prototype.js"></script>
 
<script>
     
//你的action,当然也可以传参
     var url = "${pageContext.request.contextPath}/ServiceServlet";
     
     
function test()
     
{
         
//Ajax request
         new Ajax.Request(url,
         
{
             method: 
"post",  //post method
             onSuccess : function(response)     //callback function
             {
                 $(
"d").innerHTML = response.responseText;
             }

         }

         );
     }
    
     
     
//注册响应事件
     Ajax.Responders.register(
     
{
         
//请求时事件
         onCreate : function()
         
{
             Element.show(
"loading");
         }

         ,
         onComplete : 
function()
         
{
             
//活动的请求全部完成时时
             if(Ajax.activeRequestCount == 0)
             
{
                 Element.hide(
"loading");
             }

         }

     }
);
     
 
</script> 
  
  
</head>
  
<body>
  
<input type="button" onclick="test();" title="注意观察页面刷新了吗?" value="点我试试"/>
  
<div id="loading" style="display: none;"></div>
  
<div id="d"></div>
  
</body>
</html>

servlet:
package com.test;

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;

@SuppressWarnings(
"serial")
public class ServiceServlet extends HttpServlet
{

    
public void doGet(HttpServletRequest request, HttpServletResponse response)
            
throws ServletException, IOException
    
{
        
this.doPost(request, response);
    }


    
public void doPost(HttpServletRequest request, HttpServletResponse response)
            
throws ServletException, IOException
    
{
        String msg 
= "如果您看到此消息说明已经成功了!";
        response.setContentType(
"text/html");
        response.setCharacterEncoding(
"gbk");
        PrintWriter out 
= response.getWriter();
        out.print(msg);
    }


}

posted on 2008-08-04 15:52 々上善若水々 阅读(144) 评论(2)  编辑  收藏 所属分类: JavaScript

评论

# re: Prototype1.6 实战11 (Ajax) 2008-08-19 10:38 技术

我想聘用你 怎么联系你 你写的很好 看来技术很强  回复  更多评论   

# re: Prototype1.6 实战11 (Ajax) 2008-08-19 10:43 々上善若水々

刚刚工作2年,暂时不想离职。呵呵。谢谢支持~  回复  更多评论   


标题  
姓名  
主页
验证码 *  
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
 
 

相关链接:
网站导航:

导航

<2008年12月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

统计

留言簿(12)

随笔档案(24)

文章分类(277)

文章档案(228)

搜索

最新评论

阅读排行榜

评论排行榜