31 How Servlet is flexible to take both GET, POST Request | Adv Java Servlet Programming Tutorial

Опубликовано: 01 Февраль 2018
на канале: tech fort
697
56

#How to #make #Servlet #program #flexible to #take #both #GET and #POST #based #Request? #adv java #Advance #Java #Servlet #Programming #Tutorial

To make our servlet program as flexible program to take and process both GET and POST method based request we can use one of the following three approaches:

Approach 1: (Using service(-,-) method)
-----------
Ex: public class VoteServ extends HttpServlet
{
public void service(-,-) throws ServletException, IOException
{
----- //Request Processing Logic
-----
-----
}
}

Note - Both service (-, -) methods can receive & handle GET, POST methods based request given by client/browser, but keeping request processing logic in service(-,-) method is not industry standard.


Approach 2: Override both methods like doGet(-,-), doPost(-,-) methods & keep ----------- request processing logic in one method & call that method from other method.

public class WishServ extends HttpServlet
{
public void doGet(-,-)throws ServletException, IOException{
---
---
---//Request Processing Logic
}
public void doPost(-,-)thows ServletException, IOException{
doGet(-,-);
}
}
Note: Working with doXxx(-,-) methods is industry standard apporoach is to place request processing logic.

Approach 3: Keep Request processing logic in user defined method & call that
----------- user/programmer defined method from doGet(-,-) & doPost(-,-)

Note: Make sure that user defined method is having same prototype of doXxx(-,-) methods.

public class LoginServlet extends HttpServlet{

//user defined method
public void xyz(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException{ //Unchecked Exception
----
----// Request Processing Logic
----
}

public void doGet(-,-)throws ServletException, IOException{
xyz(req,res);
}
public void doPost(-,-)throws ServletException, IOException{
xyz(req, res);
}
}


Conlusion : Prefer approach 2 or approach 3 to develop the servlet program having the flexibility of modifing any request method (either GET or POST) based request processing logic.