Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Integrating AJAX with ArcGIS Server - I

As I mentioned previously, one of the ways to implement Ajax in the ArcGIS Server Java application is by creating a Custom Servlet. We can then write Javascript code to submit client request to this custom Servlet using the XMLHttpRequest object. The servlet in return will generate the response which is again processed by the custom Javascript code on the client side.

Lets talk about how to create a Magnifier tool in ArcGIS Server MapViewer template by creating a custom servlet. First, we will pass the user click point (X/Y values) to our custom servlet. The custom Servlet will then use ArcObjects to generate the magnified image for that location. That sounds easy, but how can we get the server object properties (like server connection, map properties etc.) already stored in Java ADF in our custom Servlet? They are required for us to be able generate the map image using our custom ArcObjects code. The answer is the AGSWebContext object which hosts all ArcObjects for the server object.

Getting AGSWebContext in a Servlet is pretty simple. All we need to do is first get the WebSession attribute from session and then get AGSWebContext object from WebSession. Once we get AGSWebContext object, we need to
1. call activate method to hydrate the current properties of a pooled server object. (We are using pooled server object because we want to be able to quickly generate image and release the server object back. )
2. pass the AgsWebContext to another class names AgsMap which will generate the image.
3. call passivate method to release the server object back to the pool.

I will discuss the AgsMap class next time but for now here is the complete code for the custom Servlet:


package com.esri.arcgis.sample;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.esri.arcgis.webcontrols.ags.data.*;
import com.esri.arcgis.webcontrols.data.*;

public final class AgsServlet
extends HttpServlet {

private ServletConfig servletConfig = null;

public void destroy() {
servletConfig = null;
}

public ServletConfig getServletConfig() {

return (this.servletConfig);

}

public String getServletInfo() {

return (this.getClass().getName());

}

public void init(ServletConfig servletConfig)
throws ServletException {

this.servletConfig = servletConfig;
}

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws java.io.IOException, ServletException {
HttpSession session = request.getSession(true);
int x = Integer.parseInt(
(String)request.getParameter("x"));
int y = Integer.parseInt(
(String)request.getParameter("y"));
WebSession wsession = (WebSession)
session.getAttribute(WebSession.SESSION_ATTRIBUTE_NAME );
String url=null;
if(wsession!=null) {
AGSWebContext context = (AGSWebContext)
wsession.findWebContext(0);
context.activate();
url = AgsMap.getImageUrl(context, x, y);
context.passivate();
}
response.setContentType("text/html");
java.io.PrintWriter out=response.getWriter();
out.print(url);
out.flush();
}

public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws java.io.IOException, ServletException {
doGet(request, response);
}

}



This post first appeared on Geo Coding, please read the originial post: here

Share the post

Integrating AJAX with ArcGIS Server - I

×

Subscribe to Geo Coding

Get updates delivered right to your inbox!

Thank you for your subscription

×