Disclaimer

The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.

Thursday, February 5, 2009

Flex 3 Need web service or a servlet will do?

Flex 3 talks so heavily about the using web services to get data over internet that sometimes people who dont have real complex requirements are stumped by this. 

Creating SOAP web services is pretty tedious sepcially for beginners. So, a wise question to ask will be if really a web service is required to retrieve data over internet or a very simple thing like a servlet will do that job?

So some one will need a web service to consume in a Flex app when the business logic has to made available to the third parties too. Other scenarios are like a web service already exists for the functionality.

However, if the data has to consumed for some small apps and not required to be made available to third parties then a simple servlet can do a job which can return the desired XML output in the respone.

Just as an example you can write something like:


public class MyServlet extends HttpServlet {
   
    public MyServlet () {
    }
    
    public void doGet(HttpServletRequest req, HttpServletResponse resp) 
                throws ServletException, IOException {        
       try{
                      
            resp.setContentType("text/xml; charset=UTF-8");
            PrintWriter out = resp.getWriter();
            out.println("");  
            out.print("30");
            out.println("");  
            out.close();
        }catch(Exception e){
        System.out.println("DataException in startFetching ::" + e.getMessage());
          e.printStackTrace();;  
        } 
       
       return;
    }
}

And in your Flex App you would specifying the url that you will be use to deploy the servlet.


No comments: