Jsp:
<%@ page language="java" contentType="text/html; charset=GB2312" import="java.sql.*,java.util.*,publicUse.*,com.web.*" errorPage=""
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<head>
<style type="text/css">
body{
background:url(../image/abc.gif);
}
</style>
<script type="text/javascript">
var xmlHttp=false;
function createXMLHttpRequest()
{
if(window.XMLHttpRequest)
{
xmlHttp=new XMLHttpRequest();
}
else if(window.ActiveXObject)
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
if(xmlHttp==false)
alert("创建XMLHttpRequest对象失败");
}
function updateCity()
{
var province=encodeURI(encodeURI(document.all.province.value));
createXMLHttpRequest();
xmlHttp.onreadystatechange=processor;
xmlHttp.open("POST","<%=basePath%>servlet/City?province="+province);
xmlHttp.send(null);
}
function processor()
{
var cities;
if(xmlHttp.readyState==4)//response successfully
{
if(xmlHttp.status==200)//return successfully
{
cities=xmlHttp.responseXML.getElementsByTagName("city");
while(document.all.city.options.length>0)
{
document.all.city.removeChild(document.all.city.childNodes[0]);
}
}
for(var i=0;i<cities.length;i++)
{
var option=document.createElement("OPTION");
option.text=cities[i].getElementsByTagName("cityname")[0].firstChild.data;
//alert(option.text);
option.value=cities[i].getElementsByTagName("cityvalue")[0].firstChild.data;
document.all.city.options.add(option);
}
}
}
</script>
<script language="javascript">
function check(){
for ( var i = 0; i < 2; i++) {
if (form.elements[i].value == "") {
alert("选择数据!");
return false;
}
}
if(window.confirm("确定要删除选中数据吗?")){
return true;
}
else{
return false;
}
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
DBConnect connect = new DBConnect();
String GetInfoSql = "select distinct gsname from caiwu";
System.out.println(GetInfoSql);
ResultSet rs = connect.executeQuery(GetInfoSql);
%>
<form name="form" method="post" action="<%=basePath%>servlet/DbDel" onSubmit="return check()">
<p> </p>
<p> </p>
<table width="50%" align="center" cellpadding="1" cellspacing="0">
<tr><td width="5%" nowrap="nowrap"><div align="right"><span class="STYLE2">选择公司:</span></div></td>
<td width="10%" align="right">
<select name="select" id="province" onChange="updateCity();">
<option value="">请选择</option>
<%try{
while(rs.next()){
%>
<option value=<%=rs.getString("gsname")%>><%=rs.getString("gsname")%></option>
<%
}
rs.close();
}
catch(SQLException ex){}
%>
</select></td>
<td width="10%" align="right">选择报表:</td>
<td width="10%" align="right">
<select id="city" name="time">
<option value="">请选择</option>
</select>
</td>
<tr><td></td>
<td align="right"><input name="deldb" type="submit" value="删除数据"/></td>
<td align="right"> </td>
<td align="right"> </td></tr>
</table>
</form>
</body>
</html>
selrvlet:
package com.web;
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;
import publicUse.DBConnect;
import java.sql.ResultSet;
import java.sql.SQLException;
public class City extends HttpServlet {
public void destroy() {
super.destroy();
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/xml");
response.setCharacterEncoding("UTF-8");
String province= URLDecoder.decode(request.getParameter("province"), "UTF-8");
PrintWriter out = response.getWriter();
out.println("<response>");
DBConnect connect = new DBConnect();
Boolean a;
String GetInfoSql;
try {
int num=Integer.valueOf(province);//把字符串强制转换为数字
a= false;//如果是数字,返回True
} catch (Exception e) {
a= true;//如果抛出异常,返回False
}
if(a){
GetInfoSql = "select time from caiwu WHERE gsname = '"+province+"'";
}else{
GetInfoSql = "select gsname from caiwu WHERE time = '"+province+"'";
}
System.out.println(GetInfoSql);
ResultSet rs = connect.executeQuery(GetInfoSql);
try{
while(rs.next()){
String abc;
if(a){
abc = rs.getString("time");
}else{
abc = rs.getString("gsname");
}
System.out.println(abc);
out.println("<city>");
out.println("<cityname>"+abc+"</cityname>");
out.println("<cityvalue>"+abc+"</cityvalue>");
out.println("</city>");
}
} catch(SQLException ex){}
out.println("</response>");
out.flush();
out.close();
}
public void init() throws ServletException {
// Put your code here
}
}