ArcGIS Server 9.3 JavaScript API实战(二)----一个具体的小系统示例介绍

断断续续的学习ArcGIS Server Java也有一段时间了,做个小结,把自己学习过程中做的一个小系统拿来讲讲,说说自己的一些体会,一方面怕时间久了自己会忘,另一方面ArcGIS Server 9.3 for Java的资料还不太多,也希望对别人有点用处。

这个系统是学习ArcGIS Server 9.3 JavaScript API过程中慢慢做起来的,算是我学习的一个见证。先给个图,看看模样:

在数据库中存储人员信息,其中包括人员经纬度坐标;在地图上可以根据人员坐标进行定位;可以进行人员信息删改,添加。

相关配置是:SQLServer 2005 + JDK1.6+Tomcat 6+ArcGIS Server 93。

整个系统基本上分两部分:数据库库操作和地图操作。数据库部分没什么可说的,就是简单的数据插入,删除,修改以及查询而已。

关键在于地图部分。

使用ArcGIS Server JavaScript API进行地图显示操作,前面曾做过一些简单介绍。我的感受就是一个字:烦。这里的烦不是写代码的烦,麻烦的是配置各种服务。ArcGIS的所有功能基本上都以服务的形式提供,要使用它们就得先配置好服务。准备地图需要配置ArcGIS Server 地图服务,用js页面显示的话要配置ArcGIS Server JavaScript API 服务,进行距离面积量算的话要配置GeometryServer服务,如果有其他功能的话得再配置相关服务。这些对熟手可能还好说,对我这个刚接触ArcGIS而且没有详细资料的新手来说,可就难了。地图服务还好说,自己摸索一下就差不多了;可JavaScript API服务就费脑筋了,当初自己一个人闷头闷脑的用jdk1.6+tomcat6配,明明配好了就是不起作用,折腾好几天后一怒之下用jdk1.5+tomcat5,一配就好,真是无语;那个量算的GeometryServe服务,至今还没配好----准确的说是配好了不起作用,看文档也找不到相关说明,使用esri提供的在线GeometryServe服务也不行,真是郁闷死了,所以直到现在量算都没搞定(关于这个量算,曾经一度考虑自己写算法根据经纬度来计算,因为实在太过复杂也就罢了)。

在这里,对esri有各小小的抱怨:能不能出一份配置这些服务的专门文档和详细资料?esri的在线文档里关于这部份实在太模糊不清了。

服务配好了的话,进行地图显示和操作就不是问题了,基本上照esri的在线文档里示例做就可以搞定。

这里要说的是整个系统的“架构”和我的一点小体会。

这个小玩意儿里目前最主要的就是5个页面:

oper.jsp:      进行数据录入,删改等与数据库相关的操作

data.html:    主要是显示,包括记录的详细信息和地图数据。真正的显示是在下面两个页面里完成的。

info.js:         利用Ajax在载入页面时从数据库读取记录并以表格的形式进行显示操作。

map.js:        载入页面时初始化地图。

frame.html:  主框架页面。

数据库处理和Ajax请求响应是在两个类里面完成的:Test_SQLServer.java进行基本的数据库操作,dataServlet.java处理Ajax请求。

 

由于这个玩意是自己用来试验的,不涉及到公司机密,所以贴几个关键页面的实现:

Code

 

Code

 

Code

 

Code

 
/*
  文件:dataServlet.java
  功能:处理Ajax请求
  时间:2008年9月
  作者:饶正锋,权利所有
*/


package OperDB;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.SQLException;

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

/*
 *通过servlet来处理数据的提交,请求操作 
 
*/
public class dataServlet extends HttpServlet{
//  private static  final String CONTENT_TYPE="text/html;charset=gb2312";
    public void init()throws ServletException{}   
    
public void doGet(HttpServletRequest request,HttpServletResponse response)
    
throws ServletException,IOException{
        doPost(request,response);
    }
    
public void doPost(HttpServletRequest request,HttpServletResponse response)
    
throws ServletException,IOException{    
//    System.out.println("收到请求!");
        ServletContext sc=getServletContext();
        sc.log(request.getRemoteHost()
+":"+request.getRemoteAddr()+":"+request.getRemotePort()+":"+request.getRemoteUser()+" doPost():"+request.getRequestURL()+"?"+request.getQueryString());
        request.setCharacterEncoding(
"gb2312");
        response.setContentType(
"text/html;charset=gb2312");
        String action
=request.getParameter("action");
//      添加数据
        if(action.equals("add")){
            add_data(request,response);       
        }
//      删除数据
        else if(action.equals("del")){          
            del_data(request,response);
        }
//      执行SQL语句
        else if(action.equals("exe_sql")){   
            exe_sql(request,response);
        }
//      更新数据        
        else if(action.equals("update")){
            update_data(request,response);
        }
//      显示数据        
        else if(action.equals("showdata")){
            
try{
                
//show_data(request,response);
                show_smiple_data(request,response);            
            }
            
catch(SQLException e){
               e.printStackTrace();
            }
        }
//      计算地球上两点间距离        
        else if(action.equals("cal_distance")){
            calDistance(request,response);
        }          
        sc.log(
"处理完毕!");
    }
        
    
public void add_data(HttpServletRequest request,HttpServletResponse response)
    
throws ServletException,IOException{
        PrintWriter out
=response.getWriter();      
        String s_id
=request.getParameter("id");
        String name
=request.getParameter("name");
        String s_age
=request.getParameter("age");
        String phone
=request.getParameter("phone");
        String email
=request.getParameter("email");
        String s_workDate
=request.getParameter("workDate");
        String s_position_x
=request.getParameter("x_position");
        String s_position_y
=request.getParameter("y_position");       
        
try{
               
int id=Integer.parseInt(s_id);
               
try{
                  
int age=Integer.parseInt(s_age);
                  
try{
                     
float x_position=Float.parseFloat(s_position_x);
                     
float y_position=Float.parseFloat(s_position_y);
                     
try{
                        Date workDate
=Date.valueOf(s_workDate);
                          
if(Test_SQLServer.Conn_SQLServer()){
                            String add_sql
="insert into jsp_student values("+id+",'"+name+"',"+age+",'"+phone+"','"+email+"','"+workDate+"',"+x_position+","+y_position+")";
                            
boolean exe_result=Test_SQLServer.ExeSQL(add_sql);
                            
if(exe_result){
                               out.println(
"<html>");
                               out.println(
"<head><title>添加记录成功</title></head>");
                               out.println(
"<body>");                  
                               out.println(
"<script language=\"JavaScript\">");
                               out.println(
"window.alert(\"添加记录成功,恭喜你!\")"); 
                               String lo
="window.top.frames[\"right\"].location.reload()"
//                                刷新数据显示页面
                               out.println(lo);
                               out.println(
"</script>");
                               out.println(
"</body></html>");
                               System.out.println(
"添加记录成功");
                            }
                            
else{
                               System.out.println(
"添加记录失败");
                             out.println(
"<html>");
                               out.println(
"<head><title>添加记录失败</title></head>");
                               out.println(
"<body>");                  
                               out.println(
"<script language=\"JavaScript\">");
                               String m
="window.alert(\""+Test_SQLServer.msg+"\")";
                               out.println(m);
                               out.println(
"</script>");
                               out.println(
"</body></html>");
                         }
                         }
                       
else{
                          out.println(
"<html>");
                            out.println(
"<head><title>添加记录失败</title></head>");
                            out.println(
"<body>");                  
                            out.println(
"<script language=\"JavaScript\">");
                            out.println(
"window.alert(\"添加记录失败,无法连接数据库\")"); 
                            out.println(
"</script>");
                            out.println(
"</body></html>");
                            System.out.println(
"添加记录失败,无法连接数据库");
                       }
                   }
                   
catch(IllegalArgumentException ie){
                      out.println(
"<html>");
                        out.println(
"<head><title>添加记录失败</title></head>");
                        out.println(
"<body>");                  
                        out.println(
"<script language=\"JavaScript\">");
                        out.println(
"window.alert(\"添加记录失败,入职日期 字段请按2008-08-08 格式填写\")"); 
                        out.println(
"</script>");
                         out.println(
"</body></html>");  
                        ie.printStackTrace();
                        System.out.println(
"添加记录失败");
                      System.out.println(
"异常:"+ie.toString());
                  }
                }                 catch(NumberFormatException ne){                     out.println("<html>");                       out.println("<head><title>添加记录失败</title></head>");                       out.println("<body>");                                         out.println("<script language=\"JavaScript\">");                       out.println("window.alert(\"添加记录失败,坐标位置请填入经纬度的数字表示\")");                        out.println("</script>");                        out.println("</body></html>");                         ne.printStackTrace();                       System.out.println("添加记录失败");                     System.out.println("异常:"+ne.toString());                 }                }                catch(NumberFormatException ne){                   out.println("<html>");                   out.println("<head><title>添加记录失败</title></head>");                     out.println("<body>");                     out.println("<script language=\"JavaScript\">");                     out.println("window.alert(\"添加记录失败,原因:年龄 字段请填写数字\")");                      out.println("</script>");                                    out.println("</body></html>");                       ne.printStackTrace();                     System.out.println("添加记录失败");                   System.out.println("异常:"+ne.toString());                }             }             catch(NumberFormatException ne){                  out.println("<html>");                  out.println("<head><title>添加记录失败</title></head>");                  out.println("<body>");                  out.println("<script language=\"JavaScript\">");                  out.println("window.alert(\"添加记录失败,原因:编号 字段请填写数字\")");                     out.println("</script>");                                      out.println("</body></html>");                      ne.printStackTrace();                    System.out.println("添加记录失败");                  System.out.println("异常:"+ne.toString());            }            finally{                out.close();            }     }     public void del_data(HttpServletRequest request,HttpServletResponse response)     throws ServletException,IOException{            PrintWriter out=response.getWriter();            String s_id=request.getParameter("id");            try{              int id=Integer.parseInt(s_id);              if(Test_SQLServer.Conn_SQLServer()){                  String del_sql="delete from jsp_student where s_id="+id;                  if(Test_SQLServer.ExeSQL(del_sql)){                      out.println("<html>");                        out.println("<body>");                                          out.println("<script language=\"JavaScript\">");                        out.println("window.alert(\"删除记录成功!\")");  //                       刷新数据显示页面                        String lo="window.top.frames[\"right\"].location.reload()";                        out.println(lo);     //                       out.println("info_init()");                        out.println("</script>");                        out.println("</body></html>");                        System.out.println("删除记录成功!");                  }                  else{                      System.out.println("删除记录失败");                      out.println("<html>");                        out.println("<head><title>删除记录失败</title></head>");                        out.println("<body>");                                          out.println("<script language=\"JavaScript\">");                        String m="window.alert(\""+Test_SQLServer.msg+"\")";                        out.println(m);                        System.out.println(m);                        out.println("</script>");                        out.println("</body></html>");                  }              }              else              {                  out.println("<html>");                  out.println("<head><title>删除记录失败</title></head>");                  out.println("<body>");                                    out.println("<script language=\"JavaScript\">");                  out.println("window.alert(\"删除记录失败,无法连接数据库\")");                   out.println("</script>");                  out.println("</body></html>");                  System.out.println("删除记录失败,无法连接数据库");              }            }            catch(NumberFormatException ne){              out.println("<html>");              out.println("<head><title>删除记录失败</title></head>");              out.println("<body>");              out.println("<script language=\"JavaScript\">");              out.println("window.alert(\"删除记录失败,原因:编号 字段请填写数字\")");               out.println("</script>");                             out.println("</body></html>");                ne.printStackTrace();              System.out.println("删除记录失败");              System.out.println("异常:"+ne.toString());            }            out.close();     }     public void exe_sql(HttpServletRequest request,HttpServletResponse response){              }     public void update_data(HttpServletRequest request,HttpServletResponse response)     throws ServletException,IOException{         PrintWriter out=response.getWriter();               String s_x=request.getParameter("x");               String s_y=request.getParameter("y");         String s_update_num=request.getParameter("update_num");         //String s_num=request.getParameter("update_num");         try{             BigDecimal position_x=new BigDecimal(s_x);             BigDecimal position_y=new BigDecimal(s_y);             int num=Integer.parseInt(s_update_num);             //int num=Integer.parseInt(s_num);             String updateString="update jsp_student set s_positionX="+position_x+",s_positionY="+position_y+"where s_id="+num;               if(Test_SQLServer.Conn_SQLServer()){                 if(Test_SQLServer.ExeSQL(updateString)){                    out.println("<html>");                    out.println("<body>");                                          out.println("<script language=\"JavaScript\">");                        out.println("window.alert(\"更新坐标位置成功!\")");                         String lo="window.top.frames[\"right\"].location.reload()";                      out.println(lo);                          out.println("</script>");                        out.println("</body></html>");                        System.out.println("更新坐标位置成功!");                 }                 else{                      System.out.println("更新坐标位置失败");                      out.println("<html>");                        out.println("<head><title>更新坐标位置失败</title></head>");                        out.println("<body>");                                          out.println("<script language=\"JavaScript\">");                        String m="window.alert(\"更新坐标位置\"+\""+Test_SQLServer.msg+"\")";                        out.println(m);                        System.out.println(m);                        out.println("</script>");                        out.println("</body></html>");                  }              }              else              {                  out.println("<html>");                  out.println("<head><title>更新坐标位置失败</title></head>");                  out.println("<body>");                                    out.println("<script language=\"JavaScript\">");                  out.println("window.alert(\"更新坐标位置失败,无法连接数据库\")");                   out.println("</script>");                  out.println("</body></html>");                  System.out.println("更新坐标位置失败,无法连接数据库");              }         }         catch(NumberFormatException e){             e.printStackTrace();         }                        } //  显示数据,直接返回整个表格,包括相应的html标签     public void show_data(HttpServletRequest request,HttpServletResponse response)     throws ServletException,IOException,SQLException{         //System.out.println("coming show_data!");         PrintWriter out=response.getWriter();         if(Test_SQLServer.Conn_SQLServer()){             if(Test_SQLServer.ExeSQL("SELECT * FROM jsp_student")){                 Test_SQLServer.rs.first();                       out.write("<table  id=\"data\"  width=\"100%\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" class=\"chart_form\">");                   out.write("<tr>");                   out.write("<th >编号</th> <th>姓名</th> <th>年龄</th><th>电话</th><th>电邮</th><th>入职日期</th><th>位置(经纬度坐标)</th>");                   out.write("</tr>");                 do{                                           out.write("<tr valign=\"MIDDLE\">");                        out.write("<td><input type=\"checkbox\" align=\"right\" id="+Test_SQLServer.rs.getInt(1)+"> "+Test_SQLServer.rs.getInt(1)+"</td>");                        out.write("<td>"+Test_SQLServer.rs.getString(2)+"</td>");                        out.write("<td>"+Test_SQLServer.rs.getInt(3)+"</td>");                        out.write("<td>"+Test_SQLServer.rs.getString(4)+"</td>");                        out.write("<td><a href=\"mailto:\">"+Test_SQLServer.rs.getString(5)+"</a></td>");                        out.write("<td>"+Test_SQLServer.rs.getDate(6)+"</td>");                        out.write("<td>("+Test_SQLServer.rs.getBigDecimal(7)+","+Test_SQLServer.rs.getBigDecimal(8)+")</td>");                        out.write("</tr>");                                        }while(Test_SQLServer.rs.next());                 out.write("</table>");             }             else{                 out.write("<p>查询数据库失败,无法显示!</p>");             }          }          else          {              out.write("<p>连接数据库失败,无法显示!</p>");          }          out.close();     } //  显示数据,仅仅返回相应的数据,其他html标签在调用的时候由js生成     public void show_smiple_data(HttpServletRequest request,HttpServletResponse response)     throws ServletException,IOException,SQLException{         PrintWriter out=response.getWriter();         if(Test_SQLServer.Conn_SQLServer()){             if(Test_SQLServer.ExeSQL("SELECT * FROM jsp_student")){                 Test_SQLServer.rs.first();                          do{                                           out.write(String.valueOf(Test_SQLServer.rs.getInt(1))+",");                        out.write(Test_SQLServer.rs.getString(2)+",");                        out.write(String.valueOf(Test_SQLServer.rs.getInt(3))+",");                        out.write(Test_SQLServer.rs.getString(4)+",");                        out.write(Test_SQLServer.rs.getString(5)+",");                        out.write(Test_SQLServer.rs.getDate(6).toString()+",");                        out.write(Test_SQLServer.rs.getBigDecimal(7).toString()+","+Test_SQLServer.rs.getBigDecimal(8).toString()+";");                                    }while(Test_SQLServer.rs.next());             }             else{                 out.write("<p>查询数据库失败,无法显示!</p>");             }          }          else          {              out.write("<p>连接数据库失败,无法显示!</p>");          }          out.close();     }     //  计算地球上两点间距离     public void calDistance(HttpServletRequest request,HttpServletResponse response)     throws ServletException,IOException{         System.out.println("coming……");         PrintWriter out=response.getWriter();         String s_ar=request.getParameter("ar");         String s_ap=request.getParameter("ap");         String s_br=request.getParameter("br");         String s_bp=request.getParameter("bp");         //System.out.println("ar"+s_ar);         //System.out.println("ap"+s_ap);         //System.out.println("br"+s_br);         //System.out.println("bp"+s_bp);         if(s_ar.equals(null)||s_ap.equals(null)||s_br.equals(null)||s_bp.equals(null)){                        out.println("坐标参数传递失败!");             System.out.println("坐标参数传递失败!");         }         else{             try{                 double ar=Double.parseDouble(s_ar);                 double ap=Double.parseDouble(s_ap);                 double br=Double.parseDouble(s_br);                 double bp=Double.parseDouble(s_bp);                 double dis=cal.cal_distance(ar, ap, br, bp);                 out.write(String.valueOf(dis));                 out.close();             }             catch(NumberFormatException ne){                 ne.printStackTrace();             }         }     } }   具体功能主要分三大块: 1. 信息添加,删除等。最开始是采用jsp页面跳转的方式进行数据提交,由于需要刷新整个页面且台复杂,后来就改为用Ajax+Servlet在后台提交进行处理,这样一来就没有页面跳动,而且处理起来更方便了。 2. 显示记录信息。起先是用jsp页面在载入时直接从数据库读取数据显示,增加和删改数据时需要重新载入整个jsp页面,用Ajax后就直接用js生成整个表格了。 3. 地图功能。主要有:定位功能----在表格中点击记录中的姓名超链接后就可根据其经纬度坐标在地图上进行定位;更新坐标----选中某条记录后即可在地图上选取一点更新其坐标;量算功能----距离量算和面积量算(还有问题没解决),GIS中的常见功能。 来张图: 本来想加进更多的功能,比如地理解码:直接输入一个地名然后就可定位至该处(这在Google Map API中很容易实现,最近也稍微了解了一下,有时间也说说),缓冲区分析等稍微复杂一点的GIS功能,这些虽然好玩,但是有点难啃,得下狠功夫,作为学习了解也就罢了,有机会再说。 至此,这个小系统介绍的差不多了。可以看出,目前功能虽然粗陋,但已具备了一个小型系统的架子,可以在其基础上添加很多应用。 这玩意虽小,但作为初学者的我也费了不少劲,但说系统架构,最开始把所有的东西都放到jsp页面里,每个操作都是一个页面跳转,结果最多的时候有十多个页面(都是跳转的,没什么实际用途);后来就把数据库相关操作独立出来,通过专门的类来与数据库打交道;最后用Ajax+Servlet把记录和地图显示部分再分出来,直接用js控制它们。现在我用4个页面和两个java类就完成了这个小玩意,跟最初相比还真是有不小的进步;在这个过程中,对html,javascript,jsp,java,servlet,ajax这些都或多或少的有了一些了解,以前不会的现在稍微会一些,以前不怎么清楚的现在也都有了更深的理解,也算不小的进步吧。 值得一提的是,在这些日子,我自己翻英文文档,自己搜资料,自己找解决办法,一个人走到了这一步,也让我对自己更有信心了。 /*   文件:dataServlet.java   功能:处理Ajax请求   时间:2008年9月   作者:饶正锋,权利所有 */ package OperDB; import java.io.IOException; import java.io.PrintWriter; import java.math.BigDecimal; import java.sql.Date; import java.sql.SQLException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /*  *通过servlet来处理数据的提交,请求操作   */ public class dataServlet extends HttpServlet{ //  private static  final String CONTENT_TYPE="text/html;charset=gb2312";     public void init()throws ServletException{}        public void doGet(HttpServletRequest request,HttpServletResponse response)     throws ServletException,IOException{         doPost(request,response);     }     public void doPost(HttpServletRequest request,HttpServletResponse response)     throws ServletException,IOException{     //    System.out.println("收到请求!");         ServletContext sc=getServletContext();         sc.log(request.getRemoteHost()+":"+request.getRemoteAddr()+":"+request.getRemotePort()+":"+request.getRemoteUser()+" doPost():"+request.getRequestURL()+"?"+request.getQueryString());         request.setCharacterEncoding("gb2312");         response.setContentType("text/html;charset=gb2312");         String action=request.getParameter("action"); //      添加数据         if(action.equals("add")){             add_data(request,response);                } //      删除数据         else if(action.equals("del")){                       del_data(request,response);         } //      执行SQL语句         else if(action.equals("exe_sql")){                exe_sql(request,response);         } //      更新数据                 else if(action.equals("update")){             update_data(request,response);         } //      显示数据                 else if(action.equals("showdata")){             try{                 //show_data(request,response);                 show_smiple_data(request,response);                         }             catch(SQLException e){                e.printStackTrace();             }         } //      计算地球上两点间距离                 else if(action.equals("cal_distance")){             calDistance(request,response);         }                   sc.log("处理完毕!");     }              public void add_data(HttpServletRequest request,HttpServletResponse response)     throws ServletException,IOException{         PrintWriter out=response.getWriter();               String s_id=request.getParameter("id");         String name=request.getParameter("name");         String s_age=request.getParameter("age");         String phone=request.getParameter("phone");         String email=request.getParameter("email");         String s_workDate=request.getParameter("workDate");         String s_position_x=request.getParameter("x_position");         String s_position_y=request.getParameter("y_position");                try{                int id=Integer.parseInt(s_id);                try{                   int age=Integer.parseInt(s_age);                   try{                      float x_position=Float.parseFloat(s_position_x);                      float y_position=Float.parseFloat(s_position_y);                      try{                         Date workDate=Date.valueOf(s_workDate);                           if(Test_SQLServer.Conn_SQLServer()){                             String add_sql="insert into jsp_student values("+id+",'"+name+"',"+age+",'"+phone+"','"+email+"','"+workDate+"',"+x_position+","+y_position+")";                             boolean exe_result=Test_SQLServer.ExeSQL(add_sql);                             if(exe_result){                                out.println("<html>");                                out.println("<head><title>添加记录成功</title></head>");                                out.println("<body>");                                                  out.println("<script language=\"JavaScript\">");                                out.println("window.alert(\"添加记录成功,恭喜你!\")");                                 String lo="window.top.frames[\"right\"].location.reload()";  //                                刷新数据显示页面                                out.println(lo);                                out.println("</script>");                                out.println("</body></html>");                                System.out.println("添加记录成功");                             }                             else{                                System.out.println("添加记录失败");                              out.println("<html>");                                out.println("<head><title>添加记录失败</title></head>");                                out.println("<body>");                                                  out.println("<script language=\"JavaScript\">");                                String m="window.alert(\""+Test_SQLServer.msg+"\")";                                out.println(m);                                out.println("</script>");                                out.println("</body></html>");                          }                          }                        else{                           out.println("<html>");                             out.println("<head><title>添加记录失败</title></head>");                             out.println("<body>");                                               out.println("<script language=\"JavaScript\">");                             out.println("window.alert(\"添加记录失败,无法连接数据库\")");                              out.println("</script>");                             out.println("</body></html>");                             System.out.println("添加记录失败,无法连接数据库");                        }                    }                    catch(IllegalArgumentException ie){                       out.println("<html>");                         out.println("<head><title>添加记录失败</title></head>");                         out.println("<body>");                                           out.println("<script language=\"JavaScript\">");                         out.println("window.alert(\"添加记录失败,入职日期 字段请按2008-08-08 格式填写\")");                          out.println("</script>");                          out.println("</body></html>");                           ie.printStackTrace();                         System.out.println("添加记录失败");                       System.out.println("异常:"+ie.toString());                   }                 }                 catch(NumberFormatException ne){                     out.println("<html>");                       out.println("<head><title>添加记录失败</title></head>");                       out.println("<body>");                                         out.println("<script language=\"JavaScript\">");                       out.println("window.alert(\"添加记录失败,坐标位置请填入经纬度的数字表示\")");                        out.println("</script>");                        out.println("</body></html>");                         ne.printStackTrace();                       System.out.println("添加记录失败");                     System.out.println("异常:"+ne.toString());                 }                }                catch(NumberFormatException ne){                   out.println("<html>");                   out.println("<head><title>添加记录失败</title></head>");                     out.println("<body>");                     out.println("<script language=\"JavaScript\">");                     out.println("window.alert(\"添加记录失败,原因:年龄 字段请填写数字\")");                      out.println("</script>");                                    out.println("</body></html>");                       ne.printStackTrace();                     System.out.println("添加记录失败");                   System.out.println("异常:"+ne.toString());                }             }             catch(NumberFormatException ne){                  out.println("<html>");                  out.println("<head><title>添加记录失败</title></head>");                  out.println("<body>");                  out.println("<script language=\"JavaScript\">");                  out.println("window.alert(\"添加记录失败,原因:编号 字段请填写数字\")");                     out.println("</script>");                                      out.println("</body></html>");                      ne.printStackTrace();                    System.out.println("添加记录失败");                  System.out.println("异常:"+ne.toString());            }            finally{                out.close();            }     }     public void del_data(HttpServletRequest request,HttpServletResponse response)     throws ServletException,IOException{            PrintWriter out=response.getWriter();            String s_id=request.getParameter("id");            try{              int id=Integer.parseInt(s_id);              if(Test_SQLServer.Conn_SQLServer()){                  String del_sql="delete from jsp_student where s_id="+id;                  if(Test_SQLServer.ExeSQL(del_sql)){                      out.println("<html>");                        out.println("<body>");                                          out.println("<script language=\"JavaScript\">");                        out.println("window.alert(\"删除记录成功!\")");  //                       刷新数据显示页面                        String lo="window.top.frames[\"right\"].location.reload()";                        out.println(lo);     //                       out.println("info_init()");                        out.println("</script>");                        out.println("</body></html>");                        System.out.println("删除记录成功!");                  }                  else{                      System.out.println("删除记录失败");                      out.println("<html>");                        out.println("<head><title>删除记录失败</title></head>");                        out.println("<body>");                                          out.println("<script language=\"JavaScript\">");                        String m="window.alert(\""+Test_SQLServer.msg+"\")";                        out.println(m);                        System.out.println(m);                        out.println("</script>");                        out.println("</body></html>");                  }              }              else              {                  out.println("<html>");                  out.println("<head><title>删除记录失败</title></head>");                  out.println("<body>");                                    out.println("<script language=\"JavaScript\">");                  out.println("window.alert(\"删除记录失败,无法连接数据库\")");                   out.println("</script>");                  out.println("</body></html>");                  System.out.println("删除记录失败,无法连接数据库");              }            }            catch(NumberFormatException ne){              out.println("<html>");              out.println("<head><title>删除记录失败</title></head>");              out.println("<body>");              out.println("<script language=\"JavaScript\">");              out.println("window.alert(\"删除记录失败,原因:编号 字段请填写数字\")");               out.println("</script>");                             out.println("</body></html>");                ne.printStackTrace();              System.out.println("删除记录失败");              System.out.println("异常:"+ne.toString());            }            out.close();     }     public void exe_sql(HttpServletRequest request,HttpServletResponse response){              }     public void update_data(HttpServletRequest request,HttpServletResponse response)     throws ServletException,IOException{         PrintWriter out=response.getWriter();               String s_x=request.getParameter("x");               String s_y=request.getParameter("y");         String s_update_num=request.getParameter("update_num");         //String s_num=request.getParameter("update_num");         try{             BigDecimal position_x=new BigDecimal(s_x);             BigDecimal position_y=new BigDecimal(s_y);             int num=Integer.parseInt(s_update_num);             //int num=Integer.parseInt(s_num);             String updateString="update jsp_student set s_positionX="+position_x+",s_positionY="+position_y+"where s_id="+num;               if(Test_SQLServer.Conn_SQLServer()){                 if(Test_SQLServer.ExeSQL(updateString)){                    out.println("<html>");                    out.println("<body>");                                          out.println("<script language=\"JavaScript\">");                        out.println("window.alert(\"更新坐标位置成功!\")");                         String lo="window.top.frames[\"right\"].location.reload()";                      out.println(lo);                          out.println("</script>");                        out.println("</body></html>");                        System.out.println("更新坐标位置成功!");                 }                 else{                      System.out.println("更新坐标位置失败");                      out.println("<html>");                        out.println("<head><title>更新坐标位置失败</title></head>");                        out.println("<body>");                                          out.println("<script language=\"JavaScript\">");                        String m="window.alert(\"更新坐标位置\"+\""+Test_SQLServer.msg+"\")";                        out.println(m);                        System.out.println(m);                        out.println("</script>");                        out.println("</body></html>");                  }              }              else              {                  out.println("<html>");                  out.println("<head><title>更新坐标位置失败</title></head>");                  out.println("<body>");                                    out.println("<script language=\"JavaScript\">");                  out.println("window.alert(\"更新坐标位置失败,无法连接数据库\")");                   out.println("</script>");                  out.println("</body></html>");                  System.out.println("更新坐标位置失败,无法连接数据库");              }         }         catch(NumberFormatException e){             e.printStackTrace();         }                        } //  显示数据,直接返回整个表格,包括相应的html标签     public void show_data(HttpServletRequest request,HttpServletResponse response)     throws ServletException,IOException,SQLException{         //System.out.println("coming show_data!");         PrintWriter out=response.getWriter();         if(Test_SQLServer.Conn_SQLServer()){             if(Test_SQLServer.ExeSQL("SELECT * FROM jsp_student")){                 Test_SQLServer.rs.first();                       out.write("<table  id=\"data\"  width=\"100%\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" class=\"chart_form\">");                   out.write("<tr>");                   out.write("<th >编号</th> <th>姓名</th> <th>年龄</th><th>电话</th><th>电邮</th><th>入职日期</th><th>位置(经纬度坐标)</th>");                   out.write("</tr>");                 do{                                           out.write("<tr valign=\"MIDDLE\">");                        out.write("<td><input type=\"checkbox\" align=\"right\" id="+Test_SQLServer.rs.getInt(1)+"> "+Test_SQLServer.rs.getInt(1)+"</td>");                        out.write("<td>"+Test_SQLServer.rs.getString(2)+"</td>");                        out.write("<td>"+Test_SQLServer.rs.getInt(3)+"</td>");                        out.write("<td>"+Test_SQLServer.rs.getString(4)+"</td>");                        out.write("<td><a href=\"mailto:\">"+Test_SQLServer.rs.getString(5)+"</a></td>");                        out.write("<td>"+Test_SQLServer.rs.getDate(6)+"</td>");                        out.write("<td>("+Test_SQLServer.rs.getBigDecimal(7)+","+Test_SQLServer.rs.getBigDecimal(8)+")</td>");                        out.write("</tr>");                                        }while(Test_SQLServer.rs.next());                 out.write("</table>");             }             else{                 out.write("<p>查询数据库失败,无法显示!</p>");             }          }          else          {              out.write("<p>连接数据库失败,无法显示!</p>");          }          out.close();     } //  显示数据,仅仅返回相应的数据,其他html标签在调用的时候由js生成     public void show_smiple_data(HttpServletRequest request,HttpServletResponse response)     throws ServletException,IOException,SQLException{         PrintWriter out=response.getWriter();         if(Test_SQLServer.Conn_SQLServer()){             if(Test_SQLServer.ExeSQL("SELECT * FROM jsp_student")){                 Test_SQLServer.rs.first();                          do{                                           out.write(String.valueOf(Test_SQLServer.rs.getInt(1))+",");                        out.write(Test_SQLServer.rs.getString(2)+",");                        out.write(String.valueOf(Test_SQLServer.rs.getInt(3))+",");                        out.write(Test_SQLServer.rs.getString(4)+",");                        out.write(Test_SQLServer.rs.getString(5)+",");                        out.write(Test_SQLServer.rs.getDate(6).toString()+",");                        out.write(Test_SQLServer.rs.getBigDecimal(7).toString()+","+Test_SQLServer.rs.getBigDecimal(8).toString()+";");                                    }while(Test_SQLServer.rs.next());             }             else{                 out.write("<p>查询数据库失败,无法显示!</p>");             }          }          else          {              out.write("<p>连接数据库失败,无法显示!</p>");          }          out.close();     }     //  计算地球上两点间距离     public void calDistance(HttpServletRequest request,HttpServletResponse response)     throws ServletException,IOException{         System.out.println("coming……");         PrintWriter out=response.getWriter();         String s_ar=request.getParameter("ar");         String s_ap=request.getParameter("ap");         String s_br=request.getParameter("br");         String s_bp=request.getParameter("bp");         //System.out.println("ar"+s_ar);         //System.out.println("ap"+s_ap);         //System.out.println("br"+s_br);         //System.out.println("bp"+s_bp);         if(s_ar.equals(null)||s_ap.equals(null)||s_br.equals(null)||s_bp.equals(null)){                        out.println("坐标参数传递失败!");             System.out.println("坐标参数传递失败!");         }         else{             try{                 double ar=Double.parseDouble(s_ar);                 double ap=Double.parseDouble(s_ap);                 double br=Double.parseDouble(s_br);                 double bp=Double.parseDouble(s_bp);                 double dis=cal.cal_distance(ar, ap, br, bp);                 out.write(String.valueOf(dis));                 out.close();             }             catch(NumberFormatException ne){                 ne.printStackTrace();             }         }     } } 具体功能主要分三大块: 1. 信息添加,删除等。最开始是采用jsp页面跳转的方式进行数据提交,由于需要刷新整个页面且台复杂,后来就改为用Ajax+Servlet在后台提交进行处理,这样一来就没有页面跳动,而且处理起来更方便了。 2. 显示记录信息。起先是用jsp页面在载入时直接从数据库读取数据显示,增加和删改数据时需要重新载入整个jsp页面,用Ajax后就直接用js生成整个表格了。 3. 地图功能。主要有:定位功能----在表格中点击记录中的姓名超链接后就可根据其经纬度坐标在地图上进行定位;更新坐标----选中某条记录后即可在地图上选取一点更新其坐标;量算功能----距离量算和面积量算(还有问题没解决),GIS中的常见功能。 来张图: 本来想加进更多的功能,比如地理解码:直接输入一个地名然后就可定位至该处(这在Google Map API中很容易实现,最近也稍微了解了一下,有时间也说说),缓冲区分析等稍微复杂一点的GIS功能,这些虽然好玩,但是有点难啃,得下狠功夫,作为学习了解也就罢了,有机会再说。 至此,这个小系统介绍的差不多了。可以看出,目前功能虽然粗陋,但已具备了一个小型系统的架子,可以在其基础上添加很多应用。 这玩意虽小,但作为初学者的我也费了不少劲,但说系统架构,最开始把所有的东西都放到jsp页面里,每个操作都是一个页面跳转,结果最多的时候有十多个页面(都是跳转的,没什么实际用途);后来就把数据库相关操作独立出来,通过专门的类来与数据库打交道;最后用Ajax+Servlet把记录和地图显示部分再分出来,直接用js控制它们。现在我用4个页面和两个java类就完成了这个小玩意,跟最初相比还真是有不小的进步;在这个过程中,对html,javascript,jsp,java,servlet,ajax这些都或多或少的有了一些了解,以前不会的现在稍微会一些,以前不怎么清楚的现在也都有了更深的理解,也算不小的进步吧。 值得一提的是,在这些日子,我自己翻英文文档,自己搜资料,自己找解决办法,一个人走到了这一步,也让我对自己更有信心了。   /*   文件:dataServlet.java   功能:处理Ajax请求   时间:2008年9月   作者:饶正锋,权利所有 */ package OperDB; import java.io.IOException; import java.io.PrintWriter; import java.math.BigDecimal; import java.sql.Date; import java.sql.SQLException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /*  *通过servlet来处理数据的提交,请求操作   */ public class dataServlet extends HttpServlet{ //  private static  final String CONTENT_TYPE="text/html;charset=gb2312";     public void init()throws ServletException{}        public void doGet(HttpServletRequest request,HttpServletResponse response)     throws ServletException,IOException{         doPost(request,response);     }     public void doPost(HttpServletRequest request,HttpServletResponse response)     throws ServletException,IOException{     //    System.out.println("收到请求!");         ServletContext sc=getServletContext();         sc.log(request.getRemoteHost()+":"+request.getRemoteAddr()+":"+request.getRemotePort()+":"+request.getRemoteUser()+" doPost():"+request.getRequestURL()+"?"+request.getQueryString());         request.setCharacterEncoding("gb2312");         response.setContentType("text/html;charset=gb2312");         String action=request.getParameter("action"); //      添加数据         if(action.equals("add")){             add_data(request,response);                } //      删除数据         else if(action.equals("del")){                       del_data(request,response);         } //      执行SQL语句         else if(action.equals("exe_sql")){                exe_sql(request,response);         } //      更新数据                 else if(action.equals("update")){             update_data(request,response);         } //      显示数据                 else if(action.equals("showdata")){             try{                 //show_data(request,response);                 show_smiple_data(request,response);                         }             catch(SQLException e){                e.printStackTrace();             }         } //      计算地球上两点间距离                 else if(action.equals("cal_distance")){             calDistance(request,response);         }                   sc.log("处理完毕!");     }              public void add_data(HttpServletRequest request,HttpServletResponse response)     throws ServletException,IOException{         PrintWriter out=response.getWriter();               String s_id=request.getParameter("id");         String name=request.getParameter("name");         String s_age=request.getParameter("age");         String phone=request.getParameter("phone");         String email=request.getParameter("email");         String s_workDate=request.getParameter("workDate");         String s_position_x=request.getParameter("x_position");         String s_position_y=request.getParameter("y_position");                try{                int id=Integer.parseInt(s_id);                try{                   int age=Integer.parseInt(s_age);                   try{                      float x_position=Float.parseFloat(s_position_x);                      float y_position=Float.parseFloat(s_position_y);                      try{                         Date workDate=Date.valueOf(s_workDate);                           if(Test_SQLServer.Conn_SQLServer()){                             String add_sql="insert into jsp_student values("+id+",'"+name+"',"+age+",'"+phone+"','"+email+"','"+workDate+"',"+x_position+","+y_position+")";                             boolean exe_result=Test_SQLServer.ExeSQL(add_sql);                             if(exe_result){                                out.println("<html>");                                out.println("<head><title>添加记录成功</title></head>");                                out.println("<body>");                                                  out.println("<script language=\"JavaScript\">");                                out.println("window.alert(\"添加记录成功,恭喜你!\")");                                 String lo="window.top.frames[\"right\"].location.reload()";  //                                刷新数据显示页面                                out.println(lo);                                out.println("</script>");                                out.println("</body></html>");                                System.out.println("添加记录成功");                             }                             else{                                System.out.println("添加记录失败");                              out.println("<html>");                                out.println("<head><title>添加记录失败</title></head>");                                out.println("<body>");                                                  out.println("<script language=\"JavaScript\">");                                String m="window.alert(\""+Test_SQLServer.msg+"\")";                                out.println(m);                                out.println("</script>");                                out.println("</body></html>");                          }                          }                        else{                           out.println("<html>");                             out.println("<head><title>添加记录失败</title></head>");                             out.println("<body>");                                               out.println("<script language=\"JavaScript\">");                             out.println("window.alert(\"添加记录失败,无法连接数据库\")");                              out.println("</script>");                             out.println("</body></html>");                             System.out.println("添加记录失败,无法连接数据库");                        }                    }                    catch(IllegalArgumentException ie){                       out.println("<html>");                         out.println("<head><title>添加记录失败</title></head>");                         out.println("<body>");                                           out.println("<script language=\"JavaScript\">");                         out.println("window.alert(\"添加记录失败,入职日期 字段请按2008-08-08 格式填写\")");                          out.println("</script>");                          out.println("</body></html>");                           ie.printStackTrace();                         System.out.println("添加记录失败");                       System.out.println("异常:"+ie.toString());                   }                 }                 catch(NumberFormatException ne){                     out.println("<html>");                       out.println("<head><title>添加记录失败</title></head>");                       out.println("<body>");                                         out.println("<script language=\"JavaScript\">");                       out.println("window.alert(\"添加记录失败,坐标位置请填入经纬度的数字表示\")");                        out.println("</script>");                        out.println("</body></html>");                         ne.printStackTrace();                       System.out.println("添加记录失败");                     System.out.println("异常:"+ne.toString());                 }                }                catch(NumberFormatException ne){                   out.println("<html>");                   out.println("<head><title>添加记录失败</title></head>");                     out.println("<body>");                     out.println("<script language=\"JavaScript\">");                     out.println("window.alert(\"添加记录失败,原因:年龄 字段请填写数字\")");                      out.println("</script>");                                    out.println("</body></html>");                       ne.printStackTrace();                     System.out.println("添加记录失败");                   System.out.println("异常:"+ne.toString());                }             }             catch(NumberFormatException ne){                  out.println("<html>");                  out.println("<head><title>添加记录失败</title></head>");                  out.println("<body>");                  out.println("<script language=\"JavaScript\">");                  out.println("window.alert(\"添加记录失败,原因:编号 字段请填写数字\")");                     out.println("</script>");                                      out.println("</body></html>");                      ne.printStackTrace();                    System.out.println("添加记录失败");                  System.out.println("异常:"+ne.toString());            }            finally{                out.close();            }     }     public void del_data(HttpServletRequest request,HttpServletResponse response)     throws ServletException,IOException{            PrintWriter out=response.getWriter();            String s_id=request.getParameter("id");            try{              int id=Integer.parseInt(s_id);              if(Test_SQLServer.Conn_SQLServer()){                  String del_sql="delete from jsp_student where s_id="+id;                  if(Test_SQLServer.ExeSQL(del_sql)){                      out.println("<html>");                        out.println("<body>");                                          out.println("<script language=\"JavaScript\">");                        out.println("window.alert(\"删除记录成功!\")");  //                       刷新数据显示页面                        String lo="window.top.frames[\"right\"].location.reload()";                        out.println(lo);     //                       out.println("info_init()");                        out.println("</script>");                        out.println("</body></html>");                        System.out.println("删除记录成功!");                  }                  else{                      System.out.println("删除记录失败");                      out.println("<html>");                        out.println("<head><title>删除记录失败</title></head>");                        out.println("<body>");                                          out.println("<script language=\"JavaScript\">");                        String m="window.alert(\""+Test_SQLServer.msg+"\")";                        out.println(m);                        System.out.println(m);                        out.println("</script>");                        out.println("</body></html>");                  }              }              else              {                  out.println("<html>");                  out.println("<head><title>删除记录失败</title></head>");                  out.println("<body>");                                    out.println("<script language=\"JavaScript\">");                  out.println("window.alert(\"删除记录失败,无法连接数据库\")");                   out.println("</script>");                  out.println("</body></html>");                  System.out.println("删除记录失败,无法连接数据库");              }            }            catch(NumberFormatException ne){              out.println("<html>");              out.println("<head><title>删除记录失败</title></head>");              out.println("<body>");              out.println("<script language=\"JavaScript\">");              out.println("window.alert(\"删除记录失败,原因:编号 字段请填写数字\")");               out.println("</script>");                             out.println("</body></html>");                ne.printStackTrace();              System.out.println("删除记录失败");              System.out.println("异常:"+ne.toString());            }            out.close();     }     public void exe_sql(HttpServletRequest request,HttpServletResponse response){              }     public void update_data(HttpServletRequest request,HttpServletResponse response)     throws ServletException,IOException{         PrintWriter out=response.getWriter();               String s_x=request.getParameter("x");               String s_y=request.getParameter("y");         String s_update_num=request.getParameter("update_num");         //String s_num=request.getParameter("update_num");         try{             BigDecimal position_x=new BigDecimal(s_x);             BigDecimal position_y=new BigDecimal(s_y);             int num=Integer.parseInt(s_update_num);             //int num=Integer.parseInt(s_num);             String updateString="update jsp_student set s_positionX="+position_x+",s_positionY="+position_y+"where s_id="+num;               if(Test_SQLServer.Conn_SQLServer()){                 if(Test_SQLServer.ExeSQL(updateString)){                    out.println("<html>");                    out.println("<body>");                                          out.println("<script language=\"JavaScript\">");                        out.println("window.alert(\"更新坐标位置成功!\")");                         String lo="window.top.frames[\"right\"].location.reload()";                      out.println(lo);                          out.println("</script>");                        out.println("</body></html>");                        System.out.println("更新坐标位置成功!");                 }                 else{                      System.out.println("更新坐标位置失败");                      out.println("<html>");                        out.println("<head><title>更新坐标位置失败</title></head>");                        out.println("<body>");                                          out.println("<script language=\"JavaScript\">");                        String m="window.alert(\"更新坐标位置\"+\""+Test_SQLServer.msg+"\")";                        out.println(m);                        System.out.println(m);                        out.println("</script>");                        out.println("</body></html>");                  }              }              else              {                  out.println("<html>");                  out.println("<head><title>更新坐标位置失败</title></head>");                  out.println("<body>");                                    out.println("<script language=\"JavaScript\">");                  out.println("window.alert(\"更新坐标位置失败,无法连接数据库\")");                   out.println("</script>");                  out.println("</body></html>");                  System.out.println("更新坐标位置失败,无法连接数据库");              }         }         catch(NumberFormatException e){             e.printStackTrace();         }                        } //  显示数据,直接返回整个表格,包括相应的html标签     public void show_data(HttpServletRequest request,HttpServletResponse response)     throws ServletException,IOException,SQLException{         //System.out.println("coming show_data!");         PrintWriter out=response.getWriter();         if(Test_SQLServer.Conn_SQLServer()){             if(Test_SQLServer.ExeSQL("SELECT * FROM jsp_student")){                 Test_SQLServer.rs.first();                       out.write("<table  id=\"data\"  width=\"100%\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" class=\"chart_form\">");                   out.write("<tr>");                   out.write("<th >编号</th> <th>姓名</th> <th>年龄</th><th>电话</th><th>电邮</th><th>入职日期</th><th>位置(经纬度坐标)</th>");                   out.write("</tr>");                 do{                                           out.write("<tr valign=\"MIDDLE\">");                        out.write("<td><input type=\"checkbox\" align=\"right\" id="+Test_SQLServer.rs.getInt(1)+"> "+Test_SQLServer.rs.getInt(1)+"</td>");                        out.write("<td>"+Test_SQLServer.rs.getString(2)+"</td>");                        out.write("<td>"+Test_SQLServer.rs.getInt(3)+"</td>");                        out.write("<td>"+Test_SQLServer.rs.getString(4)+"</td>");                        out.write("<td><a href=\"mailto:\">"+Test_SQLServer.rs.getString(5)+"</a></td>");                        out.write("<td>"+Test_SQLServer.rs.getDate(6)+"</td>");                        out.write("<td>("+Test_SQLServer.rs.getBigDecimal(7)+","+Test_SQLServer.rs.getBigDecimal(8)+")</td>");                        out.write("</tr>");                                        }while(Test_SQLServer.rs.next());                 out.write("</table>");             }             else{                 out.write("<p>查询数据库失败,无法显示!</p>");             }          }          else          {              out.write("<p>连接数据库失败,无法显示!</p>");          }          out.close();     } //  显示数据,仅仅返回相应的数据,其他html标签在调用的时候由js生成     public void show_smiple_data(HttpServletRequest request,HttpServletResponse response)     throws ServletException,IOException,SQLException{         PrintWriter out=response.getWriter();         if(Test_SQLServer.Conn_SQLServer()){             if(Test_SQLServer.ExeSQL("SELECT * FROM jsp_student")){                 Test_SQLServer.rs.first();                          do{                                           out.write(String.valueOf(Test_SQLServer.rs.getInt(1))+",");                        out.write(Test_SQLServer.rs.getString(2)+",");                        out.write(String.valueOf(Test_SQLServer.rs.getInt(3))+",");                        out.write(Test_SQLServer.rs.getString(4)+",");                        out.write(Test_SQLServer.rs.getString(5)+",");                        out.write(Test_SQLServer.rs.getDate(6).toString()+",");                        out.write(Test_SQLServer.rs.getBigDecimal(7).toString()+","+Test_SQLServer.rs.getBigDecimal(8).toString()+";");                                    }while(Test_SQLServer.rs.next());             }             else{                 out.write("<p>查询数据库失败,无法显示!</p>");             }          }          else          {              out.write("<p>连接数据库失败,无法显示!</p>");          }          out.close();     }     //  计算地球上两点间距离     public void calDistance(HttpServletRequest request,HttpServletResponse response)     throws ServletException,IOException{         System.out.println("coming……");         PrintWriter out=response.getWriter();         String s_ar=request.getParameter("ar");         String s_ap=request.getParameter("ap");         String s_br=request.getParameter("br");         String s_bp=request.getParameter("bp");         //System.out.println("ar"+s_ar);         //System.out.println("ap"+s_ap);         //System.out.println("br"+s_br);         // 具体功能主要分三大块: Code /*   文件:dataServlet.java   功能:处理Ajax请求   时间:2008年9月   作者:饶正锋,权利所有 */ package OperDB; import java.io.IOException; import java.io.PrintWriter; import java.math.BigDecimal; import java.sql.Date; import java.sql.SQLException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /*  *通过servlet来处理数据的提交,请求操作   */ public class dataServlet extends HttpServlet{ //  private static  final String CONTENT_TYPE="text/html;charset=gb2312";     public void init()throws ServletException{}        public void doGet(HttpServletRequest request,HttpServletResponse response)     throws ServletException,IOException{         doPost(request,response);     }     public void doPost(HttpServletRequest request,HttpServletResponse response)     throws ServletException,IOException{     //    System.out.println("收到请求!");         ServletContext sc=getServletContext();         sc.log(request.getRemoteHost()+":"+request.getRemoteAddr()+":"+request.getRemotePort()+":"+request.getRemoteUser()+" doPost():"+request.getRequestURL()+"?"+request.getQueryString());         request.setCharacterEncoding("gb2312");         response.setContentType("text/html;charset=gb2312");         String action=request.getParameter("action"); //      添加数据         if(action.equals("add")){             add_data(request,response);                } //      删除数据         else if(action.equals("del")){                       del_data(request,response);         } //      执行SQL语句         else if(action.equals("exe_sql")){                exe_sql(request,response);         } //      更新数据                 else if(action.equals("update")){             update_data(request,response);         } //      显示数据                 else if(action.equals("showdata")){             try{                 //show_data(request,response);                 show_smiple_data(request,response);                         }             catch(SQLException e){                e.printStackTrace();             }         } //      计算地球上两点间距离                 else if(action.equals("cal_distance")){             calDistance(request,response);         }                   sc.log("处理完毕!");     }              public void add_data(HttpServletRequest request,HttpServletResponse response)     throws ServletException,IOException{         PrintWriter out=response.getWriter();               String s_id=request.getParameter("id");         String name=request.getParameter("name");         String s_age=request.getParameter("age");         String phone=request.getParameter("phone");         String email=request.getParameter("email");         String s_workDate=request.getParameter("workDate");         String s_position_x=request.getParameter("x_position");         String s_position_y=request.getParameter("y_position");                try{                int id=Integer.parseInt(s_id);                try{                   int age=Integer.parseInt(s_age);                   try{                      float x_position=Float.parseFloat(s_position_x);                      float y_position=Float.parseFloat(s_position_y);                      try{                         Date workDate=Date.valueOf(s_workDate);                           if(Test_SQLServer.Conn_SQLServer()){                             String add_sql="insert into jsp_student values("+id+",'"+name+"',"+age+",'"+phone+"','"+email+"','"+workDate+"',"+x_position+","+y_position+")";                             boolean exe_result=Test_SQLServer.ExeSQL(add_sql);                             if(exe_result){                                out.println("<html>");                                out.println("<head><title>添加记录成功</title></head>");                                out.println("<body>");                                                  out.println("<script language=\"JavaScript\">");                                out.println("window.alert(\"添加记录成功,恭喜你!\")");                                 String lo="window.top.frames[\"right\"].location.reload()";  //                                刷新数据显示页面                                out.println(lo);                                out.println("</script>");                                out.println("</body></html>");                                System.out.println("添加记录成功");                             }                             else{                                System.out.println("添加记录失败");                              out.println("<html>");                                out.println("<head><title>添加记录失败</title></head>");                                out.println("<body>");                                                  out.println("<script language=\"JavaScript\">");                                String m="window.alert(\""+Test_SQLServer.msg+"\")";                                out.println(m);                                out.println("</script>");                                out.println("</body></html>");                          }                          }                        else{                           out.println("<html>");                             out.println("<head><title>添加记录失败</title></head>");                             out.println("<body>");                                               out.println("<script language=\"JavaScript\">");                             out.println("window.alert(\"添加记录失败,无法连接数据库\")");                              out.println("</script>");                             out.println("</body></html>");                             System.out.println("添加记录失败,无法连接数据库");                        }                    }                    catch(IllegalArgumentException ie){                       out.println("<html>");                         out.println("<head><title>添加记录失败</title></head>");                         out.println("<body>");                                           out.println("<script language=\"JavaScript\">");                         out.println("window.alert(\"添加记录失败,入职日期 字段请按2008-08-08 格式填写\")");                          out.println("</script>");                          out.println("</body></html>");                           ie.printStackTrace();                         System.out.println("添加记录失败");                       System.out.println("异常:"+ie.toString());                   }                 }                 catch(NumberFormatException ne){                     out.println("<html>");                       out.println("<head><title>添加记录失败</title></head>");                       out.println("<body>");                                         out.println("<script language=\"JavaScript\">");                       out.println("window.alert(\"添加记录失败,坐标位置请填入经纬度的数字表示\")");                        out.println("</script>");                        out.println("</body></html>");                         ne.printStackTrace();                       System.out.println("添加记录失败");                     System.out.println("异常:"+ne.toString());                 }                }                catch(NumberFormatException ne){                   out.println("<html>");                   out.println("<head><title>添加记录失败</title></head>");                     out.println("<body>");                     out.println("<script language=\"JavaScript\">");                     out.println("window.alert(\"添加记录失败,原因:年龄 字段请填写数字\")");                      out.println("</script>");                                    out.println("</body></html>");                       ne.printStackTrace();                     System.out.println("添加记录失败");                   System.out.println("异常:"+ne.toString());                }             }             catch(NumberFormatException ne){                  out.println("<html>");                  out.println("<head><title>添加记录失败</title></head>");                  out.println("<body>");                  out.println("<script language=\"JavaScript\">");                  out.println("window.alert(\"添加记录失败,原因:编号 字段请填写数字\")");                     out.println("</script>");                                      out.println("</body></html>");                      ne.printStackTrace();                    System.out.println("添加记录失败");                  System.out.println("异常:"+ne.toString());            }            finally{                out.close();            }     }     public void del_data(HttpServletRequest request,HttpServletResponse response)     throws ServletException,IOException{            PrintWriter out=response.getWriter();            String s_id=request.getParameter("id");            try{              int id=Integer.parseInt(s_id);              if(Test_SQLServer.Conn_SQLServer()){                  String del_sql="delete from jsp_student where s_id="+id;                  if(Test_SQLServer.ExeSQL(del_sql)){                      out.println("<html>");                        out.println("<body>");                                          out.println("<script language=\"JavaScript\">");                        out.println("window.alert(\"删除记录成功!\")");  //                       刷新数据显示页面                        String lo="window.top.frames[\"right\"].location.reload()";                        out.println(lo);     //                       out.println("info_init()");                        out.println("</script>");                        out.println("</body></html>");                        System.out.println("删除记录成功!");                  }                  else{                      System.out.println("删除记录失败");                      out.println("<html>");                        out.println("<head><title>删除记录失败</title></head>");                        out.println("<body>");                                          out.println("<script language=\"JavaScript\">");                        String m="window.alert(\""+Test_SQLServer.msg+"\")";                        out.println(m);                        System.out.println(m);                        out.println("</script>");                        out.println("</body></html>");                  }              }              else              {                  out.println("<html>");                  out.println("<head><title>删除记录失败</title></head>");                  out.println("<body>");                                    out.println("<script language=\"JavaScript\">");                  out.println("window.alert(\"删除记录失败,无法连接数据库\")");                   out.println("</script>");                  out.println("</body></html>");                  System.out.println("删除记录失败,无法连接数据库");              }            }            catch(NumberFormatException ne){              out.println("<html>");              out.println("<head><title>删除记录失败</title></head>");              out.println("<body>");              out.println("<script language=\"JavaScript\">");              out.println("window.alert(\"删除记录失败,原因:编号 字段请填写数字\")");               out.println("</script>");                             out.println("</body></html>");                ne.printStackTrace();              System.out.println("删除记录失败");              System.out.println("异常:"+ne.toString());            }            out.close();     }     public void exe_sql(HttpServletRequest request,HttpServletResponse response){              }     public void update_data(HttpServletRequest request,HttpServletResponse response)     throws ServletException,IOException{         PrintWriter out=response.getWriter();               String s_x=request.getParameter("x");               String s_y=request.getParameter("y");         String s_update_num=request.getParameter("update_num");         //String s_num=request.getParameter("update_num");         try{             BigDecimal position_x=new BigDecimal(s_x);             BigDecimal position_y=new BigDecimal(s_y);             int num=Integer.parseInt(s_update_num);             //int num=Integer.parseInt(s_num);             String updateString="update jsp_student set s_positionX="+position_x+",s_positionY="+position_y+"where s_id="+num;               if(Test_SQLServer.Conn_SQLServer()){                 if(Test_SQLServer.ExeSQL(updateString)){                    out.println("<html>");                    out.println("<body>");                                          out.println("<script language=\"JavaScript\">");                        out.println("window.alert(\"更新坐标位置成功!\")");                         String lo="window.top.frames[\"right\"].location.reload()";                      out.println(lo);                          out.println("</script>");                        out.println("</body></html>");                        System.out.println("更新坐标位置成功!");                 }                 else{                      System.out.println("更新坐标位置失败");                      out.println("<html>");                        out.println("<head><title>更新坐标位置失败</title></head>");                        out.println("<body>");                                          out.println("<script language=\"JavaScript\">");                        String m="window.alert(\"更新坐标位置\"+\""+Test_SQLServer.msg+"\")";                        out.println(m);                        System.out.println(m);                        out.println("</script>");                        out.println("</body></html>");                  }              }              else              {                  out.println("<html>");                  out.println("<head><title>更新坐标位置失败</title></head>");                  out.println("<body>");                                    out.println("<script language=\"JavaScript\">");                  out.println("window.alert(\"更新坐标位置失败,无法连接数据库\")");                   out.println("</script>");                  out.println("</body></html>");                  System.out.println("更新坐标位置失败,无法连接数据库");              }         }         catch(NumberFormatException e){             e.printStackTrace();         }                        } //  显示数据,直接返回整个表格,包括相应的html标签     public void show_data(HttpServletRequest request,HttpServletResponse response)     throws ServletException,IOException,SQLException{         //System.out.println("coming show_data!");         PrintWriter out=response.getWriter();         if(Test_SQLServer.Conn_SQLServer()){             if(Test_SQLServer.ExeSQL("SELECT * FROM jsp_student")){                 Test_SQLServer.rs.first();                       out.write("<table  id=\"data\"  width=\"100%\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" class=\"chart_form\">");                   out.write("<tr>");                   out.write("<th >编号</th> <th>姓名</th> <th>年龄</th><th>电话</th><th>电邮</th><th>入职日期</th><th>位置(经纬度坐标)</th>");                   out.write("</tr>");                 do{                                           out.write("<tr valign=\"MIDDLE\">");                        out.write("<td><input type=\"checkbox\" align=\"right\" id="+Test_SQLServer.rs.getInt(1)+"> "+Test_SQLServer.rs.getInt(1)+"</td>");                        out.write("<td>"+Test_SQLServer.rs.getString(2)+"</td>");                        out.write("<td>"+Test_SQLServer.rs.getInt(3)+"</td>");                        out.write("<td>"+Test_SQLServer.rs.getString(4)+"</td>");                        out.write("<td><a href=\"mailto:\">"+Test_SQLServer.rs.getString(5)+"</a></td>");                        out.write("<td>"+Test_SQLServer.rs.getDate(6)+"</td>");                        out.write("<td>("+Test_SQLServer.rs.getBigDecimal(7)+","+Test_SQLServer.rs.getBigDecimal(8)+")</td>");                        out.write("</tr>");                                        }while(Test_SQLServer.rs.next());                 out.write("</table>");             }             else{                 out.write("<p>查询数据库失败,无法显示!</p>");             }          }          else          {              out.write("<p>连接数据库失败,无法显示!</p>");          }          out.close();     } //  显示数据,仅仅返回相应的数据,其他html标签在调用的时候由js生成     public void show_smiple_data(HttpServletRequest request,HttpServletResponse response)     throws ServletException,IOException,SQLException{         PrintWriter out=response.getWriter();         if(Test_SQLServer.Conn_SQLServer()){             if(Test_SQLServer.ExeSQL("SELECT * FROM jsp_student")){                 Test_SQLServer.rs.first();                          do{                                           out.write(String.valueOf(Test_SQLServer.rs.getInt(1))+",");                        out.write(Test_SQLServer.rs.getString(2)+",");                        out.write(String.valueOf(Test_SQLServer.rs.getInt(3))+",");                        out.write(Test_SQLServer.rs.getString(4)+",");                        out.write(Test_SQLServer.rs.getString(5)+",");                        out.write(Test_SQLServer.rs.getDate(6).toString()+",");                        out.write(Test_SQLServer.rs.getBigDecimal(7).toString()+","+Test_SQLServer.rs.getBigDecimal(8).toString()+";");                                    }while(Test_SQLServer.rs.next());             }             else{                 out.write("<p>查询数据库失败,无法显示!</p>");             }          }          else          {              out.write("<p>连接数据库失败,无法显示!</p>");          }          out.close();     }     //  计算地球上两点间距离     public void calDistance(HttpServletRequest request,HttpServletResponse response)     throws ServletException,IOException{         System.out.println("coming……");         PrintWriter out=response.getWriter();         String s_ar=request.getParameter("ar");         String s_ap=request.getParameter("ap");         String s_br=request.getParameter("br");         String s_bp=request.getParameter("bp");         //System.out.println("ar"+s_ar);         //System.out.println("ap"+s_ap);         //System.out.println("br"+s_br);         //System.out.println("bp"+s_bp);         if(s_ar.equals(null)||s_ap.equals(null)||s_br.equals(null)||s_bp.equals(null)){                        out.println("坐标参数传递失败!");             System.out.println("坐标参数传递失败!");         }         else{             try{                 double ar=Double.parseDouble(s_ar);                 double ap=Double.parseDouble(s_ap);                 double br=Double.parseDouble(s_br);                 double bp=Double.parseDouble(s_bp);                 double dis=cal.cal_distance(ar, ap, br, bp);                 out.write(String.valueOf(dis));                 out.close();             }             catch(NumberFormatException ne){                 ne.printStackTrace();             }         }     } } 具体功能主要分三大块:  1. 信息添加,删除等。最开始是采用jsp页面跳转的方式进行数据提交,由于需要刷新整个页面且台复杂,后来就改为用Ajax+Servlet在后台提交进行处理,这样一来就没有页面跳动,而且处理起来更方便了。 2. 显示记录信息。起先是用jsp页面在载入时直接从数据库读取数据显示,增加和删改数据时需要重新载入整个jsp页面,用Ajax后就直接用js生成整个表格了。  3. 地图功能。主要有:定位功能----在表格中点击记录中的姓名超链接后就可根据其经纬度坐标在地图上进行定位;更新坐标----选中某条记录后即可在地图上选取一点更新其坐标;量算功能----距离量算和面积量算(还有问题没解决),GIS中的常见功能。  来张图: 本来想加进更多的功能,比如地理解码:直接输入一个地名然后就可定位至该处(这在Google Map API中很容易实现,最近也稍微了解了一下,有时间也说说),缓冲区分析等稍微复杂一点的GIS功能,这些虽然好玩,但是有点难啃,得下狠功夫,作为学习了解也就罢了,有机会再说。 至此,这个小系统介绍的差不多了。可以看出,目前功能虽然粗陋,但已具备了一个小型系统的架子,可以在其基础上添加很多应用。 这玩意虽小,但作为初学者的我也费了不少劲,但说系统架构,最开始把所有的东西都放到jsp页面里,每个操作都是一个页面跳转,结果最多的时候有十多个页面(都是跳转的,没什么实际用途);后来就把数据库相关操作独立出来,通过专门的类来与数据库打交道;最后用Ajax+Servlet把记录和地图显示部分再分出来,直接用js控制它们。现在我用4个页面和两个java类就完成了这个小玩意,跟最初相比还真是有不小的进步;在这个过程中,对html,javascript,jsp,java,servlet,ajax这些都或多或少的有了一些了解,以前不会的现在稍微会一些,以前不怎么清楚的现在也都有了更深的理解,也算不小的进步吧。 值得一提的是,在这些日子,我自己翻英文文档,自己搜资料,自己找解决办法,一个人走到了这一步,也让我对自己更有信心了。    

Code

具体功能主要分三大块: 

1. 信息添加,删除等。最开始是采用jsp页面跳转的方式进行数据提交,由于需要刷新整个页面且台复杂,后来就改为用Ajax+Servlet在后台提交进行处理,这样一来就没有页面跳动,而且处理起来更方便了。

2. 显示记录信息。起先是用jsp页面在载入时直接从数据库读取数据显示,增加和删改数据时需要重新载入整个jsp页面,用Ajax后就直接用js生成整个表格了。 

3. 地图功能。主要有:定位功能----在表格中点击记录中的姓名超链接后就可根据其经纬度坐标在地图上进行定位;更新坐标----选中某条记录后即可在地图上选取一点更新其坐标;量算功能----距离量算和面积量算(还有问题没解决),GIS中的常见功能。 

来张图:

 

本来想加进更多的功能,比如地理解码:直接输入一个地名然后就可定位至该处(这在Google Map API中很容易实现,最近也稍微了解了一下,有时间也说说),缓冲区分析等稍微复杂一点的GIS功能,这些虽然好玩,但是有点难啃,得下狠功夫,作为学习了解也就罢了,有机会再说。 至此,这个小系统介绍的差不多了。可以看出,目前功能虽然粗陋,但已具备了一个小型系统的架子,可以在其基础上添加很多应用。 这玩意虽小,但作为初学者的我也费了不少劲,但说系统架构,最开始把所有的东西都放到jsp页面里,每个操作都是一个页面跳转,结果最多的时候有十多个页面(都是跳转的,没什么实际用途);后来就把数据库相关操作独立出来,通过专门的类来与数据库打交道;最后用Ajax+Servlet把记录和地图显示部分再分出来,直接用js控制它们。现在我用4个页面和两个java类就完成了这个小玩意,跟最初相比还真是有不小的进步;在这个过程中,对html,javascript,jsp,java,servlet,ajax这些都或多或少的有了一些了解,以前不会的现在稍微会一些,以前不怎么清楚的现在也都有了更深的理解,也算不小的进步吧。 值得一提的是,在这些日子,我自己翻英文文档,自己搜资料,自己找解决办法,一个人走到了这一步,也让我对自己更有信心了。



posted @ 2008-10-07 14:54    阅读(3839)  评论(6编辑  收藏  举报