Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Servlet.service() method return void and not an instance of ServletResponse?

Why does the service() method in the Servlet interface not return an instance of ServletResponse but rather work on the ServletResponse object provided by the container?

In simple words why is the service method of the Servlet interface like:

public void service(ServletRequest request, ServletResponse response);

and NOT like:

public ServletResponse service(ServletRequest request);
like image 758
Satadru Biswas Avatar asked Dec 21 '22 14:12

Satadru Biswas


2 Answers

If the response object is provided by the servlet container, it can control how things like buffering are handled. For example, suppose you created your own ServletResponse - how would the container manage the ability to stream the response if it's over a certain length, instead of buffering the data?

like image 64
Jon Skeet Avatar answered Apr 26 '23 23:04

Jon Skeet


It uses a Response the container builds partially for it. It doesn't build the response out of whole cloth. It'd have to be an argument in any event.

like image 37
Joseph Ottinger Avatar answered Apr 27 '23 00:04

Joseph Ottinger