title: How can i implement doPost() and doGet() request in the same servlet using ajax?
body: <p>I am doing a small project on jsp servlet.I want an example to implement doGet() and doPost()  methods using same servlet and sending and posting via an ajax call in forms in a jsp page.</p>
  
  <p>Also specify why it is not recommendable to use doGet() and doPost() in the same servlet.How to implement the same using two different servlets but in the work in the same jsp page.</p>
  
  <p>Thanks in advance any help would be benificial.</p>
  
  <p>MY jsp code:-</p>
  
  <pre><code> &lt;form action="/mamababu.do" method="POST"&gt;
      &lt;select name="command_no"&gt;
            &lt;c:forEach var="items" items="${scriptItems}"&gt;
  
              &lt;option value="${items.command}" name="command"&gt;${items.command}&lt;/option&gt;
  
            &lt;/c:forEach&gt;
         &lt;/select&gt;
         &lt;input type="submit" value="submit"&gt;&lt;/input&gt;
  &lt;/form&gt; 
  </code></pre>
  
  <p>My servlet class:-</p>
  
  <pre><code>package com.project.mamabhagne;
  
  import java.io.IOException;
  import java.io.PrintWriter;
  import java.sql.SQLException;
  import java.util.List;
  
  import javax.servlet.RequestDispatcher;
  import javax.servlet.ServletException;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  import com.google.gson.Gson;
  
  
  @WebServlet("/mamababu.do")
  public class mamababu extends HttpServlet {
  
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          // TODO Auto-generated method stub
          //get the data from database ie the model class
          try {
              List&lt;Script&gt; scriptitems=modelDBUtil.getScriptList();
             // String json = new Gson().toJson(scriptitems);
  
              request.setAttribute("scriptItems", scriptitems);
          } catch (ClassNotFoundException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (SQLException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
          /*String itemsfood[]={"biriyani","rice"};
          request.setAttribute("itemsfood",itemsfood)*/;
  
  
  
          //redirect to a different page
          RequestDispatcher dispatcher =request.getRequestDispatcher("scriptviewer.jsp");
  
          dispatcher.forward(request, response); 
      }
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          //TODO Auto-generated method stub
          System.out.println("hi");
           response.setContentType("text/html");
                PrintWriter out = response.getWriter();
                out.println("&lt;HTML&gt;&lt;HEAD&gt;&lt;TITLE&gt;Hello World!&lt;/TITLE&gt;"+
                "&lt;/HEAD&gt;&lt;BODY&gt;Hello World!&lt;/BODY&gt;&lt;/HTML&gt;");
                out.close();
                RequestDispatcher dispatcher =request.getRequestDispatcher("scriptviewer.jsp");
              dispatcher.forward(request, response); 
      }
  
  
  }
  </code></pre>
  
  <p>I am getting this error while posting form data:-</p>
  
  <pre><code>HTTP Status 404 - /mamababu.do
  
  
  type Status report
  
  message /mamababu.do
  
  description The requested resource is not available.
  
  
  Apache Tomcat/8.0.52
  </code></pre>