📄 htmlembeddedmapservlet.java
字号:
/*
* HTMLEmbeddedMapServlet.java
*
* NOTE: Before you run this servlet, you must configure it.
* You can configure it by assigning explicit values to the
* member variables (m_mxtURL, m_fileToLoad)
* defined below; see "TODO" comments below. Or, instead of
* hard-coding these values into this source file, you can
* provide those five values by defining corresponding
* init parameters in your servlet administrator.
* COPYRIGHT 1999-2003, MapInfo Corporation
*
* Version 4.7.0
*
* Revision history:
*
* MapXtreme version 4.7: replaced (deprecated) Layer with FeatureLayer.
* Various modifications so that all strings displayed in the generated HTML
* page are in the client language.
*/
import com.mapinfo.devsupport.servlet.FieldReader;
import com.mapinfo.devsupport.servlet.MapToolkit;
import com.mapinfo.mapdefcontainer.JDBCMapDefContainer;
import com.mapinfo.mapj.MapJ;
import com.mapinfo.mapxtreme.client.MapXtremeImageRenderer;
import com.mapinfo.util.DoublePoint;
import com.mapinfo.util.DoubleRect;
import com.mapinfo.xmlprot.mxtj.ImageRequestComposer;
import java.awt.Color;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Hashtable;
import java.util.Random;
import java.io.*;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
/*
* This is an example of an HTTP Map Servlet that extends the HttpServlet
* class and contains MapJ objects
*/
public class HTMLEmbeddedMapServlet extends HttpServlet implements Serializable{
// Host Configuration
/**
*
*/
private static final long serialVersionUID = 1L;
// TODO: Specify the URL of the MapXtremeServlet that will
// service our mapping requests.
//
// Or, instead of specifying a literal string value here, you can
// specify this value using an init parameter called 'mapxtremeurl'.
private String m_mxtURL =
"http://localhost:8080/mapxtreme47/mapxtreme";
// TODO: If you want to initialize the map based on a file -- either
// a geoset (.gst file) or an XML-based map definition (.mdf file) --
// specify the name of the file, including the full path.
// Or you can specify this value using an init parameter called 'filetoload'.
// For example, on Windows, this string might look like "C:\\maps\\world.gst".
private String m_fileToLoad = null;
// TODO: If you plan to load a geoset (.gst file) instead of a map
// definition, then specify the "maps path" -- the directory path
// where map layers (.TAB files) are stored. Note that this is a
// directory on the server where MapXtremeServlet is running, which
// could be a different server than the server that is running
// this servlet. For example, if you are calling a MapXtremeServlet
// that is running on a Windows system, with map layers installed on
// drive C:, you might specify a path such as "C:\\data\\maps".
// If you will load a map definition instead of a geoset file,
// the m_mapPath variable is ignored.
// If you leave this variable set to null, and you load a .gst file,
// the program will assume that map layers are in the same directory
// as the .gst file that you are loading.
// Or you can specify this value using an init parameter called 'mappath'.
private String m_mapPath = null;
// TODO: If you want to load an XML-based map definition
// from a JDBC source, initialize the following two objects
// (in which case the m_fileToLoad variable will be ignored):
private JDBCMapDefContainer m_jdbcContainer = null;
private String m_mapName = null;
// TODO (Optional): You can re-configure the servlet so that it creates a
// GIF file on the web server, instead of its default behavior of streaming
// the image directly down to the browser. (For example, you might
// want to do performance testing, to compare the execution speed of
// streaming the image vs. creating and downloading GIF files.)
// If you want to create a server-side GIF instead of streaming the
// image directly to the browser, then change m_bGenerateImageFile to true.
// Or you can specify this value using an init parameter 'generateimagefile'.
// NOTE: If you set m_bGenerateImageFile to true, you will also need to
// set the m_mapImageURL and m_mapImagePath settings; see comments below.
private boolean m_bGenerateImageFile = false;
// TODO (optional): You can specify a path where the servlet can create and
// delete GIF image files, such as "C:\\JavaWebServer2.0\\public_html\\".
// **** This setting is only used if m_bGenerateImageFile is set to true. ****
// Or you can specify this value using an init parameter called 'mapimagepath'.
// NOTE: This string should end with the path separator for the OS
// on which the servlet will be deployed.
private String m_mapImagePath = null;
// TODO (optional): You can specify the beginning part of a URL to a GIF
// image on the web server. For example, "http://localhost/" or
// or "http://locahost:8080/" or "http://localhost/images/" .
// **** This setting is only used if m_bGenerateImageFile is set to true. ****
// Or you can specify this value using an init parameter called 'mapimageurl'.
private String m_mapImageURL = null;
// Define a constant for each type of image format we support
public static final int IMAGE_FORMAT_GIF = 0;
public static final int IMAGE_FORMAT_JPG = 1;
public static final int IMAGE_FORMAT_PNG = 2;
public static final int IMAGE_FORMAT_BMP = 3;
// Define constants to control various rendering options
public static final int NUM_OF_COLORS = 256;
public static final Color BACKGROUND_COLOR = Color.blue;
// Specify which type of map image to generate:
private int m_imageFormat = IMAGE_FORMAT_GIF;
private String m_imageExtension = ".gif";
// TODO (optional): Set the following booleans to true or false
// to control whether the corresponding items are included on
// the HTML page that the user sees. Or you can specify these
// values using init parameters named includescalebar,
// includelayercontrol, includetogglesize, and includezoomwidth.
private boolean m_bIncludeScalebar = true;
private boolean m_bIncludeLayerControl = true;
private boolean m_bIncludeToggleSize = true;
private boolean m_bIncludeZoomWidth = true;
// TODO (optional): You can set m_bDebug to true if you want error messages
// to include debugging information, or false otherwise. Or you can set
// this variable by setting a 'debug' init parameter to 'true'.
private boolean m_bDebug = false;
// A string used to report status information when m_bDebug is true.
private String m_debugMessage = "";
// A string to hold the URL used to invoke this servlet.
// You do not need to specify this explicitly; if you leave it
// set to null, we will determine it at runtime.
private String m_thisServletURL = null;
// Temporary image file handling
private static final String m_imagePrefix = "servmap";
private static int m_imageCounter = 0;
// Define the names used for the form fields
// Submit buttons on the Layer Settings page:
private static final String FF_LS_APPLY = "lsapply";
private static final String FF_LS_CANCEL = "lscancel";
// A string used to append parameters to an URL
// (such as the URL you click to bring up Layer Control)
private static final String FF_REQUEST_TYPE = "reqtype";
// Map Image:
private static final String FF_MAP_IMAGE = "mapImage";
private static final String FF_MAP_IMAGE_X = FF_MAP_IMAGE + ".x";
private static final String FF_MAP_IMAGE_Y = FF_MAP_IMAGE + ".y";
// Define identifiers that represent hidden form fields,
// which store the map's center and zoom so that the old
// center and zoom can be restored even after a session expires.
private static final String FF_OLD_X = "fx";
private static final String FF_OLD_Y = "fy";
private static final String FF_OLD_ZOOM = "fzoom";
private static final String FF_OLD_SIZE_LARGE = "flarge";
// Strings used with a Hashtable of user selections
private static final String HT_SUBMIT_BUTTON = "submitButton";
private static final String HT_SCREEN_LOCATION = "screenLocation";
private static final String HT_OLD_MAP_LOCATION = "oldmapcenter";
// Dimensions for a "small" (default sized) map image, in pixels
private static final int MAP_WIDTH_SMALL = 400;
private static final int MAP_HEIGHT_SMALL = 400;
// Dimensions for a "large" map image
private static final int MAP_WIDTH_LARGE = 550;
private static final int MAP_HEIGHT_LARGE = 550;
// Name for a session attribute that will store our MapJ object.
// Make sure this attribute name is unique (among attributes used
// by all the servlets in this context) or else this servlet may
// interfere with another servlet attempting to use the same name.
private static final String MAPJ_ATTR = "mapinfo.mapj.htmlmap";
/**
* This method initializes the servlet, and also initializes various
* member variables using initialization parameters, if those
* parameters have been provided. (You are not required to set up
* initialization parameters; instead, you can simply specify
* the desire, explicit values -- see the "TODO" comments above.
* However, you might want to provide the following settings
* through initialization parameters, so that you can change
* the values without recompiling.)
*
* @param config ServletConfig servlet initialization parameter
*
* @exception ServletException if detected when handling the request
*/
public void init(ServletConfig config)
throws ServletException
{
super.init(config);
// If the servlet set-up has provided initialization parameters,
// use those parameters to override the hard-coded values
// declared above.
String strParam = getInitParameter("mapxtremeurl");
if (strParam != null) {
m_mxtURL = strParam;
}
strParam = getInitParameter("filetoload");
if (strParam != null && strParam.length() > 0) {
m_fileToLoad = strParam;
}
strParam = getInitParameter("mappath");
if (strParam != null && strParam.length() > 0) {
m_mapPath = strParam;
}
strParam = getInitParameter("generateimagefile");
if (strParam != null) {
if (strParam.equalsIgnoreCase("true")) {
m_bGenerateImageFile = true;
} else if (strParam.equalsIgnoreCase("false")) {
m_bGenerateImageFile = false;
}
}
strParam = getInitParameter("mapimagepath");
if (strParam != null && strParam.length() > 0) {
m_mapImagePath = strParam;
}
strParam = getInitParameter("mapimageurl");
if (strParam != null && strParam.length() > 0) {
m_mapImageURL = strParam;
}
strParam = getInitParameter("imageformat");
if (strParam != null) {
if (strParam.equalsIgnoreCase("jpg") ||
strParam.equalsIgnoreCase("jpeg") ) {
m_imageFormat = IMAGE_FORMAT_JPG;
m_imageExtension = ".jpg";
} else if (strParam.equalsIgnoreCase("png") ) {
m_imageFormat = IMAGE_FORMAT_PNG;
m_imageExtension = ".png";
} else if (strParam.equalsIgnoreCase("bmp") ) {
m_imageFormat = IMAGE_FORMAT_BMP;
m_imageExtension = ".bmp";
} else {
m_imageFormat = IMAGE_FORMAT_GIF;
m_imageExtension = ".gif";
}
}
strParam = getInitParameter("debug");
if (strParam != null) {
if (strParam.equalsIgnoreCase("true")) {
m_bDebug = true;
} else if (strParam.equalsIgnoreCase("false")) {
m_bDebug = false;
}
}
// Process the four boolean settings that control whether we include
// various map elements (Scalebar, etc.) on the page.
strParam = getInitParameter("includelayercontrol");
if (strParam != null) {
if (strParam.equalsIgnoreCase("true")) {
m_bIncludeLayerControl = true;
} else if (strParam.equalsIgnoreCase("false")) {
m_bIncludeLayerControl = false;
}
}
strParam = getInitParameter("includescalebar");
if (strParam != null) {
if (strParam.equalsIgnoreCase("true")) {
m_bIncludeScalebar = true;
} else if (strParam.equalsIgnoreCase("false")) {
m_bIncludeScalebar = false;
}
}
strParam = getInitParameter("includezoomwidth");
if (strParam != null) {
if (strParam.equalsIgnoreCase("true")) {
m_bIncludeZoomWidth = true;
} else if (strParam.equalsIgnoreCase("false")) {
m_bIncludeZoomWidth = false;
}
}
strParam = getInitParameter("includetogglesize");
if (strParam != null) {
if (strParam.equalsIgnoreCase("true")) {
m_bIncludeToggleSize = true;
} else if (strParam.equalsIgnoreCase("false")) {
m_bIncludeToggleSize = false;
}
}
// If either the imagePath setting or the imageURL setting
// is null, we will not be able to generate image files that
// can be located by the user's browser. In this case,
// stream the image down to the client directly.
if (m_mapImagePath == null || m_mapImageURL == null) {
m_bGenerateImageFile = false;
}
}
/**
* service -- handle both GET and POST requests.
*
* @param req HttpServletRequest that encapsulates the request to
* the servlet
* @param res HttpServletResponse that encapsulates the response
* from the servlet
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -