Ajax小程序(ajaxTest.jsp,AjaxServer.java)

文件放置位置明细:

 

*************ajaxTest.jsp************************************

 

<%@ page contentType="text/html;charset=GBK"%>
<html>
 <head>
  <title>ajaxTest</title>
  <script language="JavaScript">
  var xmlhttp;
  function submit() {    
  if (window.XMLHttpRequest) {
  //IE7,IE8,FireFox,Mozilla,Opera,Safari都会创建成功;
   //alert("IE7,IE8,FireFox,Mozilla,Opera,Safari");
   xmlhttp = new XMLHttpRequest();
   //如果服务器返回来的头不是text/html则忽略
   if (xmlhttp.overrideMimeType) {
    xmlhttp.overrideMimeType("text/xml");
   }
   }
  else if (window.ActiveXObject){
   //如果条件成立,则是IE6,IE5.5,IE5
   //alert("IE6,IE5.5,IE5");
   var activexName=["MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.5.0",
   "MSXML2.XMLHTTP.4.0","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Miscrosoft.XMLHTTP",];
   for(var i=0;i<activexName.length;i++)
   {
    try{
     xmlhttp=new ActiveXObject(activexName[i]);
    break;
    }
    catch(e){}
   }
  }
  else if(xmlhttp==null||xmlhttp==undefind){
  //alert("当前浏览器不支持XMLHttpRequest对象,请更换浏览器!");
  }
  //alert(xmlhttp);
  //注册回调方法
  xmlhttp.onreadystatechange=callback;//注意写的是方法名,不需要加()
  var username=document.getElementById("UserName").value;
  //,设置与服务器端交互参数,用GET方法,true表示异步交互。
  xmlhttp.open("GET","/AjaxServer?name="+username,true);
  xmlhttp.send(null);
 }
 function callback()
 //判断服务器的交互是否完成,判断服务器是否正确返回数据
 {
  if(xmlhttp.readyState==4)
  { 
   //表示和服务器端交互已完成  
   if(xmlhttp.status==200)
   //表示服务器的相应代码是200,正确返回了数据
   //纯文本数据接收方法
   {
    var message=xmlhttp.responseText;
    //xml对应的dom对象接收方法
    //使用前提是服务器端需要设置contentType为text/xml
    //var domXml=xmlhttp.responseText;
    
    //向div标签中添加纯文本内容的方法
    var div=document.getElementById("message");
    div.innerHTML=message;
   }
  }
 }
  </script>
 </head>
 <body>
  <input type="text" id="UserName"></input>
  <input type="button" value="校验用户名" οnclick="submit()"></input>
  <div id="message"></div>
 </body>

</html>

 

*************AjaxServer.java***********************************************************

package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AjaxServer extends HttpServlet {

 /**
  * Constructor of the object.
  */
 public AjaxServer() {
  super();
 }

 /**
  * Destruction of the servlet. <br>
  */
 public void destroy() {
  super.destroy(); // Just puts "destroy" string in log
  // Put your code here
 }

 /**
  * The doGet method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to get.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  this.doPost(request, response);
 }

 /**
  * The doPost method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to post.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
        try{
//          request.setCharacterEncoding("UTF-8");
//          response.setContentType("text/html;charset=gb18030");

          response.setContentType("text/html;charset=utf-8");
          PrintWriter out = response.getWriter();

          Integer inte = (Integer) request.getSession().getAttribute("total");
          int temp = 0;
          if (inte == null) {
              temp = 1;
          } else {
              temp = inte.intValue() + 1;
          }
          request.getSession().setAttribute("total",temp);

          //1.取参数
          String old = request.getParameter("name");
          //String name = new String(old.getBytes("iso8859-1"),"UTF-8");
          String name = URLDecoder.decode(old,"UTF-8");
          //2.检查参数是否有问题
          if(old == null || old.length() == 0){
              out.println("用户名不能为空");
          } else{
//              String name = URLDecoder.decode(old,"UTF-8");
//              byte[] by = old.getBytes("ISO8859-1");
//              String name = new String(by,"utf-8");
//              String name = URLDecoder.decode(old,"utf-8");
              //3.校验操作
//              String name = old;
              if(name.equals("robin")){
                  //4。和传统应用不同之处。这一步需要将用户感兴趣的数据返回给页面段,而不是将一个新的页面发送给用户
                  //写法没有变化,本质发生了改变
                  out.println("用户名[" + name + "]已经存在,请使用其他用户名, " + temp);
              } else{
                  out.println("用户名[" + name + "]尚未存在,可以使用该用户名注册, " + temp);
              }
          }
      } catch(Exception e){
          e.printStackTrace();
      }
  }


 /**
  * Initialization of the servlet. <br>
  *
  * @throws ServletException if an error occurs
  */
 public void init() throws ServletException {
  // Put your code here
 }

}


**********web.xml****************************************************************

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 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">
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>ClassicServer</servlet-name>
    <servlet-class>servlet.ClassicServer</servlet-class>
  </servlet>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>AjaxServer</servlet-name>
    <servlet-class>servlet.AjaxServer</servlet-class>
  </servlet>


  <servlet-mapping>
    <servlet-name>ClassicServer</servlet-name>
    <url-pattern>/servlet/ClassicServer</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>AjaxServer</servlet-name>
    <url-pattern>/servlet/AjaxServer</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值