com.mizar.faces.ajax.servlet
Class AjaxServlet

java.lang.Object
  extended by javax.servlet.GenericServlet
      extended by javax.servlet.http.HttpServlet
          extended by com.mizar.faces.ajax.servlet.AjaxServlet
All Implemented Interfaces:
java.io.Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig

public class AjaxServlet
extends javax.servlet.http.HttpServlet

The AjaxServlet is designed to handle Ajax Requests that do not require access to resources of the Faces application. A alternate phase listener AjaxPhaseListener class is available to handle requests that require access to the Faces scope.

From Javascript the function ajaxServletRequest(requestName,message,getResponse) may be used to send requests to this AjaxServlet. The third argument getResponse should be false if the handler does not return a message and true if the calling Javascript anticipates a response.


   if (getResponse){
     return client.responseText;
   }
 
 
Example Javascript:
 htmlStr = ajaxServletRequest('QuickPick',"srid=" + srid + "&sdox=" + sdoData.getPointX() + "&sdoy=" + sdoData.getPointY() + "&zoom=" + zoomLevel + "&bean=" + theBean,true);
 
Example Handler:
 public class QuickPickHandler extends AjaxServletHandler {
   GeometryBean geometryBean = null;

   public QuickPickHandler() {
   }

   public boolean processRequest(HttpServletRequest request, HttpServletResponse response) {
     boolean rtn = false;
     String returnStr = "<div align=\"center\" bgcolor=\"#ffffb2\"><b>No Information</b></div>";
     String pkay = request.getParameter("pkey");
     if ( StringUtils.isNotEmpty(pkay)){
       return false;
     }
     String sSrid = request.getParameter("srid");
     if ( StringUtils.isEmpty(sSrid)){
       return false;
     }
     Integer srid = Integer.valueOf(sSrid);
     String dist = request.getParameter("maxDistance");
     double maxDistance = 500.0;
     if (!StringUtils.isEmpty(dist)) {
       maxDistance = Double.valueOf(dist).doubleValue();
     }
     double[] mapXY = { Double.valueOf(request.getParameter("sdox")).doubleValue(), Double.valueOf(request.getParameter("sdoy")).doubleValue() };
     String beanClassName = request.getParameter("bean");
     Class beanClass = null;
     try {
       beanClass = Class.forName(beanClassName);
     } catch (ClassNotFoundException e) {
       _Logger.log(Level.SEVERE, "" + e.getLocalizedMessage());
     }
     if (beanClass != null) {
       geometryBean = findNearest(beanClass, mapXY, srid);
       if (geometryBean != null) {
         String pkey = geometryBean.getPrimaryKeyValue();
         returnStr = "<!--PKEY="+pkey+"-->"+FacesUtils.getInfoBox(geometryBean);
         rtn = true;
       } else {
         returnStr = "<div align=\"center\" bgcolor=\"#ffffb2\"><b>Nothing found</b></div>";
         rtn = true;
       }
     } else {
       returnStr = "<div align=\"center\" bgcolor=\"#ffffb2\"><b>System Error: " + beanClassName + " not registered</b></div>";
     }
     writeResponse(response, returnStr);
     return rtn;
   }
 }
 

The AjaxServlet must be registered in the applications WEB-INF/web.xml file as follows.

  <servlet>
      <servlet-name>AjaxServlet</servlet-name>
      <servlet-class>com.mizar.faces.ajax.servlet.AjaxServlet</servlet-class>
  </servlet>
  <servlet-mapping>
      <servlet-name>AjaxServlet</servlet-name>
      <url-pattern>/AjaxServlet</url-pattern>
  </servlet-mapping>
 

Handlers must be registed in the WEB-INF/ajax-config.xml file

 <?xml version="1.0"?>
 <ajax-servlet>
 <persistence-unit>applicationDS</persistence-unit>
   <listener>
     <ajax-request>MeasureLocation</ajax-request>
      <class-name>com.mizar.ajax.servlet.MeasureLocationHandler</class-name>
   </listener>
   <listener>
     <ajax-request>MeasureDistance</ajax-request>
      <class-name>com.mizar.ajax.servlet.MeasureDistanceHandler</class-name>
   </listener>
   <listener>
     <ajax-request>MeasureArea</ajax-request>
      <class-name>com.mizar.ajax.servlet.MeasureAreaHandler</class-name>
   </listener>
   <listener>
     <ajax-request>QuickPick</ajax-request>
      <class-name>com.mizar.ajax.servlet.QuickPickHandler</class-name>
   </listener>
 </ajax-servlet>
 

See Also:
SessionConstants.getEmf(), com.mizar.faces.ajax.AjaxPhaseListener, Serialized Form

Field Summary
protected  org.apache.commons.logging.Log _Logger
           
 
Constructor Summary
AjaxServlet()
           
AjaxServlet(java.lang.String fileName)
           
 
Method Summary
 void doGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)
           
 void doPost(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)
           
 java.util.HashMap<java.lang.String,AjaxHandler> getHandlers()
           
 void init(javax.servlet.ServletConfig config)
           
static boolean isMetric()
           
static void setMetric(boolean metric)
           
 
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doHead, doOptions, doPut, doTrace, getLastModified, service, service
 
Methods inherited from class javax.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_Logger

protected org.apache.commons.logging.Log _Logger
Constructor Detail

AjaxServlet

public AjaxServlet()

AjaxServlet

public AjaxServlet(java.lang.String fileName)
Method Detail

init

public void init(javax.servlet.ServletConfig config)
          throws javax.servlet.ServletException
Specified by:
init in interface javax.servlet.Servlet
Overrides:
init in class javax.servlet.GenericServlet
Throws:
javax.servlet.ServletException

doGet

public void doGet(javax.servlet.http.HttpServletRequest req,
                  javax.servlet.http.HttpServletResponse resp)
Overrides:
doGet in class javax.servlet.http.HttpServlet

doPost

public void doPost(javax.servlet.http.HttpServletRequest req,
                   javax.servlet.http.HttpServletResponse resp)
Overrides:
doPost in class javax.servlet.http.HttpServlet

getHandlers

public java.util.HashMap<java.lang.String,AjaxHandler> getHandlers()

setMetric

public static void setMetric(boolean metric)

isMetric

public static boolean isMetric()