上篇文章中介绍了MapXtreme Java Edition 4.8.2安装,这里介绍如何创建web gis应用。
(1)MyEclipse中tomcat配置
可以使用安装目录中已有的tomcat,也可以自己重新安装一个,这里使用新的tomcat。菜单-->Window-->Preferences-->MyEclipse-->Servers-->Tomcat
将安装目录下的\MapInfo\MapXtreme-4.8.2\apache-tomcat-5.5\webapps\mapxtreme482复制到Tomcat发布目录下,mapxtreme482应该是地图服务应用。启动tomcat,通过访问服务器地址http://127.0.0.1:8080/mapxtreme482/servlet/mapxtreme来查看是否部署成功。若显示为一空白页面则表明已部署成功。
(2)创建gis应用
1、配置User Library
菜单-->Window-->Preferences-->Java-->Build Path-->User Libraries
将安装目录\MapInfo\MapXtreme-4.8.2\lib下,common、client、server下的jar包都添加进去。
2、创建web项目
File-->new-->Web Project 这里创建MapXServlet项目
添加gis应用需要的jar包,项目-->Build Path-->Add Library-->User Library 将1中配置的jar包添加进去
3、编写servlet
- package com.mapinfo.myservlets;
- import java.awt.Color;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.ServletOutputStream;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import com.mapinfo.mapj.MapJ;
- import com.mapinfo.mapxtreme.client.MapXtremeImageRenderer;
- import com.mapinfo.util.DoubleRect;
- import com.mapinfo.xmlprot.mxtj.ImageRequestComposer;
- public class SimpleServletMap extends HttpServlet {
- private static String mimeType = "p_w_picpath/gif";
- private static String m_mapPath = "E:\\paper Material\\my.mdf";
- private static String m_mxtURL = "http://localhost:8086/mapxtreme482/servlet/mapxtreme";
- /**
- * Constructor of the object.
- */
- public SimpleServletMap() {
- super();
- }
- /**
- * Destruction of the servlet. <br>
- */
- public void destroy() {
- super.destroy(); // Just puts "destroy" string in log
- // Put your code here
- }
- /**
- * The doGet method of the servlet. <br>
- *
- * This method is called when a form has its tag value method equals to get.
- *
- * @param request the request send by the client to the server
- * @param response the response send by the server to the client
- * @throws ServletException if an error occurred
- * @throws IOException if an error occurred
- */
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType(mimeType);
- ServletOutputStream sos = response.getOutputStream();
- MapJ m_mapj = new MapJ();
- m_mapj.loadMapDefinition(m_mapPath);
- m_mapj.setDeviceBounds(new DoubleRect(0,0,800,600));
- try{
- ImageRequestComposer irc = ImageRequestComposer.create(m_mapj, ImageRequestComposer.MAX_COLORS_TRUECOLOR,Color.white,mimeType);
- MapXtremeImageRenderer renderer = new MapXtremeImageRenderer(m_mxtURL);
- renderer.render(irc);
- renderer.toStream(sos);
- }catch(Exception e){
- System.out.println("Error");
- //e.printStackTrace();
- }
- }
- /**
- * The doPost method of the servlet. <br>
- *
- * This method is called when a form has its tag value method equals to post.
- *
- * @param request the request send by the client to the server
- * @param response the response send by the server to the client
- * @throws ServletException if an error occurred
- * @throws IOException if an error occurred
- */
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html");
- PrintWriter out = response.getWriter();
- out
- .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
- out.println("<HTML>");
- out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
- out.println(" <BODY>");
- out.print(" This is ");
- out.print(this.getClass());
- out.println(", using the POST method");
- out.println(" </BODY>");
- out.println("</HTML>");
- out.flush();
- out.close();
- }
- /**
- * Initialization of the servlet. <br>
- *
- * @throws ServletException if an error occurs
- */
- public void init() throws ServletException {
- // Put your code here
- }
- }
MyEclipse默认情况,添加完servlet后会自动修改web.xml,如果没有修改web.xml记得自行修改。
4、发布
访问http://127.0.0.1:8086/MapXServlet/servlet/SimpleServletMap即可看到运行结果。此时只需启动tomcat,不需要启动MapXtreme Java Edition 4.8.2,已经将地图服务mapxtreme482复制到tomcat中。
可参考:(1)http://blog.csdn.net/tmri_vio/article/details/2683672,若只添加common和server中的jar包,会报错,需要在web应用\WEB-INF\classes下添加encoding-map.xml、layerxmlhandlers.xml、querybuilderxmlhandlers.xml(这三个文件在client文件夹下)。
(2)http://wenku.baidu.com/view/adb023fb770bf78a65295491.html
转载于:https://blog.51cto.com/chenniuniu/1130804