選擇不同的郵遞區號,省份和城市動態變化
SQL:
create table zipCode(
id number primary key,
code varchar2(200),
province varchar2(200),
city varchar2(200)
)
insert into zipcode values(1001,'412400','湖南','茶陵')
insert into zipcode values(1002,'100083','北京','北京科技大學')
Connector .java
/**
*
*/
package com.cn.ajax;
import java.sql.Connection;
import java.sql.DriverManager;
/**
* @author Chenlly
*
*/
public class Connector {
public static Connection getCon() {
Connection con = null;
try {
// 載入 Oracle jdbc thin 驅動程式
Class.forName("oracle.jdbc.driver.OracleDriver");
// Oracle thin jdbc URL
String url = "jdbc:oracle:thin:@localhost:1521:oracle92";
con = DriverManager.getConnection(url, "scott", "tiger");
} catch (Exception ex) {
ex.printStackTrace();
}
return con;
}
}
//servlet
AjaxDemoServlet.java
/**
*
*/
package com.cn.ajax;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.cn.ajax.Connector;
/**
* @author Chenlly
*
*/
public class AjaxDemoServlet extends HttpServlet {
public AjaxDemoServlet() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doAny(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doAny(request,response);
}
public void doAny(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String zipCode=request.getParameter("zipCode");
Connection con=Connector.getCon();
String sql="select * from ZIPCODE t where t.code='"+zipCode+"'";
try {
ResultSet rs= con.createStatement().executeQuery(sql);
if(rs.next()){
String province=rs.getString("province");
String city=rs.getString("city");
PrintWriter out=response.getWriter();
String str=province+","+city;
//轉碼,防止頁面中文亂碼
String str_utf=new String(str.getBytes("UTF-8"),"ISO-8859-1");
out.write(str_utf);
out.flush();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void init() throws ServletException {
}
}
ajax.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'ajax.jsp' starting page</title>
<script language="javaScript">
var xmlHttpRequest;
function createXML(){
if(window.ActiveXObject){
xmlHttpRequest=new ActiveXObject("Microsoft.XMLHTTP");
}else{
xmlHttpRequest=new XMLHttpRequest();
}
}
function zipCode(zipCode){
createXML();
var url="servlet/AjaxDemoServlet?zipCode="+zipCode+"";
xmlHttpRequest.onreadystatechange=handlestatechange;
xmlHttpRequest.open("GET",url,true);
xmlHttpRequest.send(null);
}
function handlestatechange(){
if(xmlHttpRequest.readyState==4){//HTTP就緒狀態,響應已完成,可以訪問伺服器響應並使用它
if(xmlHttpRequest.status==200){//伺服器狀態代碼
var response=xmlHttpRequest.responseText.split(",");
document.getElementById("province").value=response[0];
document.getElementById("city").value=response[1];
}
}
}
</script>
</head>
<body>
<table align="center" width="500">
<h2>Please Enter Local Data</h2>
<tr>
<td>ZipCode:</td>
<td><select name="select" onchange="zipCode(this.value)" >
<option value="-1">請選擇</option>
<option value="412400">412400</option>
<option value="100083">100083</option>
</select>
</td>
</tr>
<tr>
<td>Province:</td>
<td><input id="province" type="text"/></td>
</tr>
<tr>
<td>City:</td>
<td><input id="city" type="text"/></td>
</tr>
</table>
</body>
</html>