【问题标题】:Servlet doGet() doPost() methodServlet doGet() doPost() 方法
【发布时间】:2011-08-18 01:26:03
【问题描述】:

通常我们在 HTML 代码中指定 doGetdoPost,以便 servlet 将针对 HTML 代码中的调用调用这些方法。

有没有什么方法可以调用doPostdoGet Servlet doPost 方法?

我知道有一种方法可以在doGet 中调用doPost 方法,但除此之外还有其他方法。

【问题讨论】:

标签: servlets


【解决方案1】:

从另一个调用一个,或者更好 - 从两个调用第三种方法是最好的方法。

您也可以重写 service() 方法,但您可能不想丢失更多代码。

【讨论】:

    【解决方案2】:

    您可以创建一个通用方法并将其放入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);
    }
    

    【讨论】:

      猜你喜欢
      • 2012-02-27
      • 2011-07-09
      • 2011-01-21
      • 2014-06-02
      • 2014-05-23
      • 1970-01-01
      • 1970-01-01
      • 2015-04-01
      • 2011-02-01
      相关资源
      最近更新 更多