⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 naviservlet.java

📁 mapxtreme for java 的开发实例
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      {        // initialize client's map-state and create servlet session        createNewClientSession(req);      }      //------------------------------------------------------------------------      // obtain the client MAPJ object from servlet session      //------------------------------------------------------------------------      if ((clientSession = req.getSession(false)) == null)      {        throw new ServletException("CLIENT SESSION NOT OBTAINED.");      }      else if ((mapj=(MapJ)clientSession.getAttribute(NaviServlet.m_sessionNameForMapJ)) == null)      {        throw new ServletException("MAPJ NOT OBTAINED FROM SESSION.");      }      //------------------------------------------------------------------------      // handle ZOOM-IN / ZOOM-OUT / PAN request      //------------------------------------------------------------------------      if (httpParam_requestType != null)      {        //----------------------------------------------------------------------        // in all cases we apply a PAN operation        //----------------------------------------------------------------------        DoublePoint dp = mapj.transformScreenToNumeric        (          new DoublePoint          (            Double.parseDouble(httpParam_clickX),            Double.parseDouble(httpParam_clickY)          )        );        mapj.setCenter(dp);        //----------------------------------------------------------------------        // if we need to further ZOOM-IN        //----------------------------------------------------------------------        if (httpParam_requestType.equalsIgnoreCase(NaviServlet.m_requestZoomIn))        {          mapj.setZoom(mapj.getZoom()/2);        }        //----------------------------------------------------------------------        // if we need to further ZOOM-OUt        //----------------------------------------------------------------------        else if (httpParam_requestType.equalsIgnoreCase(NaviServlet.m_requestZoomOut))        {          mapj.setZoom(mapj.getZoom()*2);        }      }      //------------------------------------------------------------------------      // now that the map-state has been setup / modified according to the      // client request type, we need to formulate our response and send it      // back to the client.      //------------------------------------------------------------------------      // create a GIF image of the map in a web-accessable directory      String mapImageFilename = createNextMapImage(mapj);      System.out.println("");      System.out.println("mapImageFilename ["+mapImageFilename+"]");      // create IMAGE URL that client will use to get image      String protocol = req.getScheme();      String serverName = req.getServerName();      String serverPort = String.valueOf(req.getServerPort());      String contextPath = req.getContextPath();      System.out.println("");      System.out.println("protocol    ["+protocol+"]");      System.out.println("serverName  ["+serverName+"]");      System.out.println("serverPort  ["+serverPort+"]");      System.out.println("contextPath ["+contextPath+"]");      String imageURL =      protocol+"://"+serverName+":"+serverPort+contextPath+"/images/"+mapImageFilename+"?refresh="+Math.random();      System.out.println("");      System.out.println("imageURL    ["+imageURL+"]");      // create server URL that client will contact in subsequent request      System.out.println("URL before sessionID: ["+req.getRequestURL()+"]");      String thisServletURL = resp.encodeURL(req.getRequestURL().toString());      System.out.println("URL after sessionID: ["+thisServletURL+"]");      // create return HTML for return to client      System.out.println("createResponseHtml");      // maintain button selected state between requests      String panChecked = "Checked";      String zoomInChecked = "";      String zoomOutChecked = "";      if (httpParam_requestType != null) // can happen on init request      {        if (httpParam_requestType.equalsIgnoreCase(NaviServlet.m_requestZoomIn))        {          zoomInChecked = "checked";        }        else if (httpParam_requestType.equalsIgnoreCase(NaviServlet.m_requestZoomOut))        {          zoomOutChecked = "checked";        }        else        {          panChecked = "checked";        }      }      resp.setContentType("text/html");      PrintWriter toClient = resp.getWriter();      toClient.println("<form action\""+thisServletURL+"\"= method=\"post\">");      toClient.println("<center>");      toClient.println("<h2>NAVI</h2>");      toClient.println("<hr>");      toClient.println("<table border=1>");      toClient.println("<tr><td>");      toClient.println("<input type=\"image\" name=\"map\" src=\""+imageURL+"\" alt=\"map image\" border=1/>");      toClient.println("</td></tr>");      toClient.println("</table>");      toClient.println("</center>");      toClient.println("<br/><br/>");      toClient.println("<center>");      toClient.println("<input type=\"radio\" name=\"action\" value=\"pan\""  +panChecked+">Pan</input>");      toClient.println("<input type=\"radio\" name=\"action\" value=\"zin\""  +zoomInChecked+">ZoomIn</input>");      toClient.println("<input type=\"radio\" name=\"action\" value=\"zout\"" +zoomOutChecked+">ZoomOut</input>");      toClient.println("</center>");      toClient.println("</form>");      toClient.println("<center>");      toClient.println("<table border=1>");      toClient.println("<tr>");      toClient.println("<td><b>MDF:</b></td><td>"+NaviServlet.m_mdfFileToLoad+"</td>");      toClient.println("</tr>");      toClient.println("<tr>");      toClient.println("<td><b>ZOOM:</b></td><td>"+mapj.getZoom()+"</td>");      toClient.println("</tr>");      toClient.println("</table>");      toClient.println("</center>");    }    catch(Exception e)    {      System.out.println("MAJOR EXCEPTION - RESPONSE *NOT* SENT BACK TO CLIENT.");      System.out.println("EXCEPTION: ["+e.getMessage()+"]");      e.printStackTrace();    }  }  /**   * creates a new servlet session for the connecting client, and throws an   * exception if a session cannot be created.   *   * @param req an HttpServletRequest object that contains the request the   *        client has made of the servlet   * @throw ServletException if the map image could not be created.   */  private void createNewClientSession(HttpServletRequest req)  throws ServletException  {    // create a new session for the client and throw exception if NOT created    HttpSession session = null;    if ((session = req.getSession())==null)    {      throw new ServletException("CANNOT CREATE NEW CLIENT SESSION");    }    else    {      try      {        // once the session has been created, we must create an instance of the        // mapj object to hold our client's map-state.  Once initialized, we will        // store the MapJ object to session for subsequent use by the client.        MapJ mapj = new MapJ();        mapj.setDeviceBounds(new DoubleRect(0,0,320,200));        System.out.println("MapJ object created.");        mapj.loadMapDefinition(NaviServlet.m_mdfFileToLoad);        System.out.println("MDF file ["+NaviServlet.m_mdfFileToLoad+"] loaded.");        session.setAttribute(NaviServlet.m_sessionNameForMapJ, mapj);        System.out.println("MapJ object stored to session.");      }      catch(Exception e)      {        throw new ServletException(e.getMessage());      }    }  }  /**   * generates a new map image for the current client.   *   * @param mapj map-state of current client.   * @throw ServletException if problem occurs during image creation.   * @return the filename of the GIF file that was created.   */  synchronized private String createNextMapImage(MapJ mapj) throws ServletException  {    String returnValue = null;    try    {      System.out.println("attempting to generate map image GIF file...");      ImageRequestComposer irc =      ImageRequestComposer.create      (        mapj,        ImageRequestComposer.MAX_COLORS_TRUECOLOR,        Color.blue,        "image/gif"      );      MapXtremeImageRenderer mir =      new MapXtremeImageRenderer(NaviServlet.m_urlOfMapXtremeServlet);      mir.render(irc);      returnValue = "map"+(NaviServlet.sequenceNumber++)+".gif";      mir.toFile(NaviServlet.m_imageDirectory+"/"+returnValue);      System.out.println("GIF file ["+NaviServlet.m_imageDirectory+      "/"+returnValue+"] created.");    }    catch(Exception e)    {      throw new ServletException(e.getMessage());    }    return returnValue;  }}// eof

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -