随笔 - 20  文章 - 2  trackbacks - 0
<2009年3月>
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234

常用链接

留言簿(1)

随笔档案

相册

搜索

  •  

最新评论

阅读排行榜

评论排行榜

如题,废话不多说。

JSP页面

hello.jsp

<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>AJAX提交页面</title>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<script type="text/javascript" src="ajax.js"></script>
   <script type="text/javascript">
   function doso(){
     var url = "actAction.do";
     var s="武晓强斯蒂芬34-*(&_(*!$^_*!&#!^$+!*#&&";
     s=encodeURIComponent(s);      //转换特殊字符
     var parameter ="name="+s+"&email=abc@abc.com&www=http://wxq594808632.blog.163.com/";
     var method = "POST";
     function callBack(text){           //回调函数
      alert("调用成功!\n名字为:"+text);
     }
   new Ajax().ajaxRequest(url,parameter,method,callBack);     //调用方法发送Request
   }
   </script>
</head>
<body>
<input type="button" value="提交" onclick="doso()"/>
   </body>
</html>

 

java类

ActAction.java

package ajaxpost;

import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.Action;
import java.io.UnsupportedEncodingException;
import java.io.IOException;
import java.io.PrintWriter;

public class ActAction extends Action {

    public ActionForward execute(ActionMapping mapping, ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws
            UnsupportedEncodingException {
        //设置字符编码返回的编码
        response.setContentType("text/html;charset=UTF-8");
        //接收字符的编码
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        String name = request.getParameter("name");
        String email = request.getParameter("email");
        String www = request.getParameter("www");
        System.out.println(email);
        System.out.println(name);
        System.out.println(www);
     //   out.print(name+"\n"+email+"\n"+www);
        out.print(name);         //返回值
        return null;
    }
}

最重要滴

ajax.js

我用别人的。感谢这位大哥。。

/*
author zhangshuling
email 
zhangshuling1214@126.com
*/
function  Ajax(){
    var _xmlHttp = null;
 this.createXMLHttpRequest = function(){
  try{
   if (window.ActiveXObject) {                                                     
    _xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");                                     
   }                                                                               
   else if (window.XMLHttpRequest) {                                                   
    _xmlHttp = new XMLHttpRequest();                                               
   }
  }catch(e){
     alert(e.name +" : " + e.message);
  }
 }
 
 this.backFunction = function(_backFunction){
  if(_xmlHttp.readyState == 4) {
   if(_xmlHttp.status == 200) {
    _backFunction(_xmlHttp.responseText);//这里可以设置返回类型
   }
  }
   _xmlHttp.onreadystatechange = null;
 }

 this.doPost = function(_url,_parameter,_backFunction){
     try{
      _xmlHttp.open("POST",_url, false);
   _xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   _xmlHttp.send(_parameter);
   }catch(e){
    alert(e.name +" : " + e.message);
     }
 }
 
 this.doGet = function(_url,_parameter,_backFunction){
    try{
        var _random = Math.round(Math.random()*10000);
        _xmlHttp.open("GET", (_url+"?random=" +_random +"&" + _parameter), false);
     _xmlHttp.send(null);
   }catch(e){
      alert(e.name +" : " + e.message);
   }
 }
 
    this.ajaxRequest = function(_url,_parameter,_method,_backFunction){
          try{
            this.createXMLHttpRequest();
         if(_method.toLowerCase() == "post"){
            this.doPost(_url,_parameter,_backFunction);
         }else{
            this.doGet(_url,_parameter,_backFunction); 
         }
         try{
           _xmlHttp.onreadystatechange = this.backFunction(_backFunction);
         }catch(err){
            //??????IE?????????????????
         }
      }catch(e){
      alert(e.name +" : " + e.message);
   }
     }

}

/*
 var url = "ajax.do";
 var parameter = "parameter=parameter";
 var method = "post"
 
 function callBack(text){
  ....
 }
 
 new Ajax().ajaxRequest(url,parameter,method,callBack);

*/

 

经过本人FF,IE6,IE7,测试。完全好用


文章来源:http://wxq594808632.blog.163.com/blog/static/109079755200923121054225
posted on 2009-03-31 14:11 武志强 阅读(827) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: