原生js封装ajax

      AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML) , 是一种用于建立快速动态网页的手艺 。
     原生js实现ajax功能本家儿要依靠于window.XMLHttpRequest(非IE浏览器利用)和window.ActiveXObject(IE浏览器利用)来标的目的后台发送请求 。

需要这些哦
电脑
myeclipse或者eclipse
第一
:新加一个javaweb项目1第一步:利用myeclipse新建javaweb项目 。
打开myeclipse file --》new--》web project 
具体操作如下图所示:

原生js封装ajax

文章插图

原生js封装ajax

文章插图

原生js封装ajax

文章插图

原生js封装ajax

文章插图

2第二步:编写servlet 。
package com.test.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestServlet exts HttpServlet {
private static final long serialVersionUID = 5181663133516569754L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}

原生js封装ajax

文章插图

3第三步:在web中设置装备摆设servlet 。
<!-- 设置装备摆设servlet 起头-->
  <servlet>
  <servlet-name>testServlet</servlet-name>
  <servlet-class>com.test.servlet.TestServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>testServlet</servlet-name>
  <url-pattern>/testServlet.do</url-pattern>
  </servlet-mapping>
  <!-- 设置装备摆设servlet 竣事-->

原生js封装ajax

文章插图

第二
:编写测试功能代码1第一步:编写servlet营业代码 。
package com.test.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestServlet exts HttpServlet {
private static final long serialVersionUID = 5181663133516569754L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 避免呈现乱码的问题
response.setContentType("application/json;charset=utf-8");
request.setCharacterEncoding("UTF-8");
String id = request.getParameter("id");
String name = request.getParameter("name");
String sex = request.getParameter("sex");
String json = "{'id':" + id + ",'name':'" + name + "','sex':'" + sex
+ "'}";
response.getWriter().print(json);
}

推荐阅读