0

I am making a servlet for attendance. So in the doGet() method all the front end is displayed and if any error is generated ; i.e., something is left blank then the doPost() method should call the doGet() again for completing the blank spaces.

How can I call doGet() method from the same servlet's doPost()?

Ramanlfc
  • 8,283
  • 1
  • 18
  • 24
Udit Sareen
  • 53
  • 2
  • 7

2 Answers2

2

If I take your question literally (i.e. invoke doGet() from doPost()), you can just invoke the doGet() method... it's a standard method like any other.

Here's a tip: When the doPost() and doGet() methods share a common set of logic, it's a good practice to isolate that logic into a separate (private) method that is to be invoked by all relevant do***() methods. For example:

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // GET-based logic
  processCommonLogic();
  // Other GET-based logic
}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // POST-based logic
  processCommonLogic();
  // Other POST-based logic
}

private void processCommonLogic() /* throws ServletException and/or IOException if needed */ {
  // Common logic
}

You can use this pattern to create a processError() method that can be invoked wherever you need it.

However, if the scope your question goes beyond invoking doGet() from doPost(), I suggest you have a look at the references pointed by Alain O'Dea.

Jeff Morin
  • 1,010
  • 1
  • 13
  • 25
0

You can do that, it's a simple

  this.doGet(req, resp);
  return;

However, it's not a best practice. Generally better to implement view logic as a JSP, and dispatch to it from the post logic...

  this. getServletConfig().getRequestDispatcher("my_view.jsp")
    .forward(req,resp);;
  return;

Or use include(), or an MVC framework like Struts...

david
  • 997
  • 6
  • 15
  • Servlet invoking isn't taking place and also for some reason nothing is getting executed from the if statement even if I leave radio box unchecked doGet() rs.next(); String uid1=rs.getString("uid"); String name1=rs.getString("name"); out.println(""+uid1+""+name1 +""+ "YESNO"+""); doPost() `String att1 = request.getParameter("att1"); if(att1=="") { doGet(request, response); }` – Udit Sareen Nov 14 '15 at 14:01