Following are the steps to delegate ExtJS Ajax or any synchronous calls to servlet.
1). Create a servlet class, with minimum having its doPost and doGet method. I have annotated the servlet with spring’s Controller annotation, which helps me later in doPost processing, but doesn’t impact much for processing at servlet level. You may opt for not to annotate with @Controller and use a simple HttpServlet. Below is full servlet code
import java.io.PrintWriter; import java.util.Enumeration; import java.util.List; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import net.sf.json.JSONArray; import com.ecms.model.Mainjob; import com.ecms.service.MainjobService; @Controller public class TestServlet extends HttpServlet { private MainjobService mainjobService; List<Mainjob> mainjobs = null; @Override public void doPost(HttpServletRequest req, HttpServletResponse res){ try{ System.out.println("inside doPost"); System.out.println("going to get req params doPost"); Enumeration<String> param = req.getParameterNames(); String temp; while (param.hasMoreElements()){ temp = param.nextElement(); System.out.println("req-->>"+temp+"--value-"+req.getParameter(temp)); } System.out.println("got params ererere"); // Json object form of data String data = "{total: 4," + "data: "+"[ {jobno: \"1\", eta: \"2\", portloading: \"Sin\", flightno: \"1243\"}," + " {jobno: \"2\", eta: \"2\", portloading: \"Sin\", flightno: \"14334\"}," + " {jobno: \"3\", eta: \"2\", portloading: \"Sin\", flightno: \"233\"}," + " {jobno: \"4\", eta: \"2\", portloading: \"Sin\", flightno: \"1233\"}" +"]}"; PrintWriter out = res.getWriter(); out.print(data); JSONArray jsonArray = JSONArray.fromObject(mainjobs); System.out.println("jsonArray"+jsonArray); System.out.println("mainjob size-->>"+mainjobs.size()); System.out.println("data--"+data); } catch(Exception ex){ ex.printStackTrace(); } } @Override public void doGet(HttpServletRequest req, HttpServletResponse res){ System.out.println("inside doGet"); } }
2) Declare this servlet class in application’s web.xml. Following is how I modify my web.xml.
<servlet> <servlet-name>test-servlet</servlet-name> <servlet-class>com.ecms.dao.test.TestServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>test-servlet</servlet-name> <url-pattern>*.test</url-pattern> </servlet-mapping>
3) Now update the JS file to point the call to the particular URL mentioned in the web.xml, if you use proxy within the JS file, than proxy’s api must now use any url that has suffix as “.test” as we have mapped test servlet to “/*.test” URL in application’s web.xml file (step2). Following is the code segment, where I integrate the call which forward to servlet.
var proxy = new Ext.data.HttpProxy({ api: { read : 'mainjob/viewMainjob.test', create : 'mainjob/createMainjob.action', } });
Note the difference between read and create, both URL has different suffix.
Above shows that the read operation would delegate task to test servlet, and create operation would look for “.action” mapping in web.xml, which can be mapped to another servlet.
Hope its useful for you, drop me your queries if any.
Thanks