struts1+ajax

先写贴一个jsp页面:

String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



 
   
   
    My JSP 'index.jsp' starting page
   
   
       
   
   
    <!--
   
    --&gt
   
   
   
        var xmlHttpRequest = null;
        function ajaxSubmit(){
            if(window.ActiveXObject){
                    xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                }else if(window.XMLHttpRequest){
                    xmlHttpRequest = new XMLHttpRequest();
                    }
            if(xmlHttpRequest!=null){
                var v1 = document.getElementById("value1ID").value;
                var v2 = document.getElementById("value2ID").value;
                    //用get方法传递   AjaxServlet 为web.xml中配置的请求 
                    //   v1="+v1+"&v2="+v2  为传递参数 如果是两个参数,别忘记写&
                    //xmlHttpRequest.open("GET","AjaxServlet?v1="+v1+"&v2="+v2,"true");
                    //xmlHttpRequest.send(null);
                   
                    //一下四行是当用POST时要用到的 AjaxServlet是web.xml中配置的请求
                    xmlHttpRequest.open("POST"," AjaxServlet","true");
                    xmlHttpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");//POST发送
                    xmlHttpRequest.send("v1="+ v1 + "&v2=" + v2);//POST
                    xmlHttpRequest.onreadystatechange = ajaxCallback;
                }
           
            }

        function ajaxCallback(){
                if(xmlHttpRequest.readyState == 4){
                    if(xmlHttpRequest.status == 200){
                            var responseText = xmlHttpRequest.responseText;
                            document.getElementById("div1").innerHTML = responseText;
                        }
                    }
            }

       
   
 
 
 
   
   
   
   

   

 


web.xml
<?xml version="1.0" encoding="UTF-8"?>
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
   
        ajax
        com.cd.servlet.AjaxServlet// AjaxServlet类的路径
   
   
   
        ajax
        /AjaxServlet
   
   
 
    index.jsp
 


AjaxServlet类的代码:

package com.cd.servlet;

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;

public class AjaxServlet extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        /*try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        resp.setHeader("pragma", "no-cache");//清除缓存
        resp.setHeader("cache-control", "no-cache");
        String v1 = req.getParameter("v1");
        String v2 = req.getParameter("v2");   
        String v3 = String.valueOf(Integer.valueOf(v1)+Integer.valueOf(v2));
        System.out.println("v1=" + v1 +"   v2="+v2);
        PrintWriter pw = resp.getWriter();
        pw.println(v3);
        pw.flush();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
System.out.println("doPost");
        doGet(req,resp);
    }
   
}
此处不能上传源代码,我会放在我空间的其他地方,名字为ajax

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26668320/viewspace-723284/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/26668320/viewspace-723284/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值