ajax终结篇

Ajax中post和get的区别

在ajax中有这个方法

xmlreq.open("post","servlet/MyServlet?time="+newDate().getTime(),true);

这里的第一个参数表示的是提交方式。可以是post和get

这两个是有区别的。

Get处理数据效率高,但是传输量小,一般在1K左右。Post相反。

如果是get那么xmlreq.open(null)open只能是空。

如果是post那么xmlreq在open的时候,得先加上xmlreq.setRequestHeader("Content-Type","application/x-www-form-urlencoded") ;

那么在open()里面是可以传数据的。

现在我写一个关于用post,与servlet的相互数据传递。

第一个是:index.jsp

<%@ page language="java" import="java.util.*"pageEncoding="UTF-8"%>

<%

Stringpath = request.getContextPath();

StringbasePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

   

    <title>My JSP 'index.jsp' starting page</title>

   

    <script type="text/javascript"src="js/fish.js"></script>

  </head>

 

  <body>

   <input id="mybutton"type="button" value="加载数据" />

  </body>

</html>

第二个:fish.Js

window.onload= function() {

     

      function createXMLHttpRequest() {

          var xmlhttp=null;//请求对象   

        try {//IE浏览器中创建此对象

           xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");

        } catch (e) {

           try { //IE浏览器中创建此对象

               xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

           } catch (e) {

               try {

                    //   Firefox,Chrome, Opera, Safari浏览器中创建此对象

                    xmlhttp = new XMLHttpRequest();

                    if (xmlhttp.overrideMimeType) {

                        xmlhttp.overrideMimeType("text/xml");

                    }

               } catch (e) { alert("浏览器不支持xmlHttpRequest!");         }

           }

        }

        return xmlhttp;

    }

     

   

    document.getElementById("mybutton").onclick = function() {

       

        //得到xmlhttprequest对象

        var xmlreq=createXMLHttpRequest();

        xmlreq.onreadystatechange=function(){

   

            alert(xmlreq.status);

            alert(xmlreq.responseText);//这是获取servlet的文本属性的,就是获取out的值。他如果这里是xml形式那么就用responseXml

           

       

        }

   

        //打开与服务器的连接

        //xmlreq.open("get","servlet/MyServlet?time="+newDate().getTime(),true);

        xmlreq.open("post","servlet/MyServlet?time="+new Date().getTime(),true);

        //发送数据

        xmlreq.setRequestHeader ("Content-Type","application/x-www-form-urlencoded") ;

       

        xmlreq.send("b=45&a=33");

        //服务器响应

   

    }

}

第三个:packagecom.fish;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet{

       publicvoid doGet(HttpServletRequest request, HttpServletResponse response)

                     throwsServletException, IOException {

      

              PrintWriterout = response.getWriter();

              System.out.println("**************");

              out.print("aa");

              out.print("bb");

              //String a= request.getParameter("fish");

              //System.out.println(a);

              //

       }

       publicvoid doPost(HttpServletRequest request, HttpServletResponse response)

                     throwsServletException, IOException {

      

      

              PrintWriterout = response.getWriter();

              System.out.println("**************");

              System.out.println(request.getParameter("a"));//这个和上面的js里面的send方法想对应。这样就可以输出a的值:33

              System.out.println(request.getParameter("b"));

              out.print("aa");//这样传给js

              out.print("bb");//这样传给js

              //String a= request.getParameter("fish");

              //System.out.println(a);

              //

       }

}

4,配置的servlet的web.xml

<?xml version="1.0"encoding="UTF-8"?>

<web-app version="2.5"

    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">

  <display-name></display-name>

  <servlet>

   

    <servlet-name>MyServlet</servlet-name>

    <servlet-class>com.fish.MyServlet</servlet-class>

  </servlet>

 

 

 

  <servlet-mapping>

    <servlet-name>MyServlet</servlet-name>

    <url-pattern>/servlet/MyServlet</url-pattern>

  </servlet-mapping>

 

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

</web-app>

原文地址:https://www.cnblogs.com/snake-hand/p/3151337.html