2011-05-03 120 views
0

通常我們在我們的HTML代碼中指定doGetdoPost,以便servlet將針對HTML代碼中的調用調用這些方法。Servlet doGet()doPost()方法

有沒有什麼辦法可以調用doPostdoGet Servlet doPost方法會被調用?

我知道有一種方法,在doGet我們可以調用doPost方法,但除此之外還有其他方法。

+0

相關:http://stackoverflow.com/questions/2349633/servlets-doget-and-dopost – BalusC 2011-05-03 23:33:26

回答

1

您可以創建一個通用方法,並將其放入doGet()或doPost()。

protected void processRequest(HttpServletRequest request,HttpServletResponse response) 
throws ServletException, IOException { 
} 

@Override 
protected void doGet(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException { 
    processRequest(request, response); 
} 

@Override 
protected void doPost(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException { 
    processRequest(request, response); 
} 
4

從另一個或更好的方向調用一個 - 從這兩個方法調用第三個方法是最好的方法。

您也可以重寫service()方法,但是那裏有更多的代碼可能不想放棄。

相關問題