title: Extending the servlet class to custom class for more customised response
body: <p>In my project, for inserting, updating and deleting the data I am requesting to servlet class and then it process and get back the response.</p>
  
  <p>It all happen via jQuery ajax.</p>
  
  <p>Right now it response only success or failure as follows</p>
  
  <pre><code>PrintWriter out =   response.getWriter();
  out.println("&lt;custom message&gt;");
  </code></pre>
  
  <p>now i want to make that message more meaningful</p>
  
  <p>By default i will accept 1 parameter called <code>"format"</code>, if the format is <code>null</code> then by default that particular servlet class will respond in <code>json</code> format else only 2 options are going to be there <code>json</code> and <code>xml</code>.</p>
  
  <p>then i need to set <code>response.setContentType("application/json");</code> and etc.</p>
  
  <p>So i will make one servlet class as follows</p>
  
  <pre><code>import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  
  public class myservlet extends HttpServlet {
  
      /**
       * 
       */
      private static final long serialVersionUID = 1L;
  
      public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,java.io.IOException
      {
          String format   =   req.getParameter("format");
  
          if(format == null)
          {
              format  =   "json";
          }
          else
          {
              if(format.equals("json"))
              {
                  resp.setContentType("application/json");
              }
              else if(format.equals("xml"))
              {
                  resp.setContentType("application/rss+xml");
              }
              else
              {
                  //error
              }
          }
      }
  
      public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,java.io.IOException
      {
          String format   =   req.getParameter("format");
  
          if(format == null)
          {
              format  =   "json";
          }
          else
          {
              if(format.equals("json"))
              {
                  resp.setContentType("application/json");
              }
              else if(format.equals("xml"))
              {
                  resp.setContentType("application/rss+xml");
              }
              else
              {
                  //error
              }
          }
      }
  
  }
  </code></pre>
  
  <p>and this above class is extended as follows </p>
  
  <p>import java.io.PrintWriter;</p>
  
  <pre><code>import javax.servlet.ServletException;
  
  public class abc extends myservlet
  {
      private static final long serialVersionUID = 1L;
  
      public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,java.io.IOException
      {
          PrintWriter out     =   resp.getWriter();
              out.println("{/"id/": /"file/"}");
          //response must be converted to either json or to xml
      }
  }
  </code></pre>
  
  <p>will it possible..??</p>
  
  <p>how can i convert response to either <code>xml</code> or <code>json</code> dynamically...??</p>