j2ee之AJAX二级联动效果

所属分类: 软件编程 / java 阅读数: 15
收藏 0 赞 0 分享

本文实例为大家分享了AJAX二级联动效果的具体代码,供大家参考,具体内容如下

Ajax.js

var createAjax = function(){
  var ajax = null;
  try{
    ajax = new ActiveXObject("microsoft.xmlhttp");
  }catch(e1){
    try{
      ajax = new XMLHttpRequest();
    }catch(e2){
      alert("请换掉你的浏览器");
    }
  }
  return ajax;
}

test3.xml

<%@ page language="Java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <script type="text/javascript" src="js/ajax.js"></script>
  <base href="<%=basePath%>" rel="external nofollow" >  
  <title>??</title>    
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">  
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 </head>
 <body>
     <select id="province">
       <option>请选择省份</option>
       <option>江苏</option>
       <option>江西</option>
     </select>
     <select id="city">
       <option>请选择城市</option>
     </select>
     <script type="text/javascript">
    document.getElementById("province").onchange = function(){
      var cityElement = document.getElementById("city");
      cityElement.options.length = 1;
      /* 拿到第一个下拉框中选中的值 */
      var index = this.selectedIndex;
      var optionElement = this[index];
      var optionValue = optionElement.innerHTML;
      /* 把这个值发送给服务器 */
      var ajax = createAjax();
      var url = "${pageContext.request.contextPath }/SelectServlet?time="+new Date().getTime();
      var method = "POST";
      ajax.open(method , url);
      ajax.setRequestHeader("content-type" , "application/x-www-form-urlencoded");
      var content = "province=" + optionValue;
      ajax.send(content);
      /* -----接收相应的数据----- */
      ajax.onreadystatechange = function(){
        if(ajax.readyState == 4 && ajax.status == 200){
          /* 拿到xml */
          var xmlDocument = ajax.responseXML;
          /* 用xml的解析方式拿到城市根据标签名称 */
          var cityArray = xmlDocument.getElementsByTagName("cityOption");
          for (var i = 0; i < cityArray.length; i++){
            /* 用xml的解析方式拿到城市的值 */
            var city = cityArray[i].firstChild.nodeValue;
            var option = document.createElement("option");
            option.innerHTML = city;
            cityElement.appendChild(option);
          }
        }
      }
    }
    
     </script>
 </body>
</html>

SelectServlet.java

package com.newtouch.servlet;

import java.io.IOException;

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

/**
 * Servlet implementation class SelectServlet
 */
@WebServlet("/SelectServlet")
public class SelectServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;

  /**
   * @see HttpServlet#HttpServlet()
   */
  public SelectServlet() {
    super();
    // TODO Auto-generated constructor stub
  }

  /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
   *   response)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    // TODO Auto-generated method stub
    response.getWriter().append("Served at: ").append(request.getContextPath());
  }

  /**
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
   *   response)
   */
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    request.setCharacterEncoding("utf-8");
    // 这里是text/xml是把数据放到了xml中
    response.setContentType("text/xml;charset=utf-8");
    String province = request.getParameter("province");
    response.getWriter().write("<?xml version='1.0' encoding='utf-8' ?>");
    response.getWriter().write("<root>");
    if ("江苏".equals(province)) {
      response.getWriter().write("<cityOption>1</cityOption>");
      response.getWriter().write("<cityOption>2</cityOption>");
      response.getWriter().write("<cityOption>3</cityOption>");
      response.getWriter().write("<cityOption>4</cityOption>");
    } else if ("江西".equals(province)) {
      response.getWriter().write("<cityOption>一</cityOption>");
      response.getWriter().write("<cityOption>二</cityOption>");
      response.getWriter().write("<cityOption>三</cityOption>");
      response.getWriter().write("<cityOption>四</cityOption>");
    }
    response.getWriter().write("</root>");
  }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Java数据类型的规则

这篇文章主要介绍了Java数据类型的规则的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Spring整合TimerTask实现定时任务调度

这篇文章主要介绍了Spring整合TimerTask实现定时任务调度的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解SpringMVC使用MultipartFile实现文件的上传

本篇文章主要介绍了SpringMVC使用MultipartFile实现文件的上传,本地的文件上传到资源服务器上,比较好的办法就是通过ftp上传。这里是结合SpringMVC+ftp的形式上传的,有兴趣的可以了解一下。
收藏 0 赞 0 分享

SpringMVC上传文件的三种实现方式

本篇文章主要介绍了SpringMVC上传文件的三种实现方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

微信公众帐号开发-自定义菜单的创建及菜单事件响应的实例

本篇文章主要介绍了微信公众帐号开发-自定义菜单的创建及菜单事件响应的实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
收藏 0 赞 0 分享

浅析Java中的继承与组合

本文将介绍组合和继承的概念及区别,并从多方面分析在写代码时如何进行选择。文中通过示例代码介绍的很详细,有需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

利用反射获取Java类中的静态变量名及变量值的简单实例

下面小编就为大家带来一篇利用反射获取Java类中的静态变量名及变量值的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java启动线程的3种方式对比分析

这篇文章主要为大家对比分析了java启动线程的3种方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

SpringMVC上传和解析Excel方法

这篇文章主要介绍了SpringMVC上传和解析Excel方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

JAVA中String类与StringBuffer类的区别

这篇文章主要为大家详细介绍了JAVA中String类与StringBuffer类的区别,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多