Web pages are written in HTML. At the top of an HTML page, there should be a document type declaration, that describes exactly which variant of HTML is used (unfortunately, because of historical reasons, there are different variants / versions of HTML being used around the web).
A servlet is a software component that runs inside a servlet container. The servlet container receives HTTP requests from a browser and calls the servlets that are deployed in it to handle these requests. The servlet produces output to be sent back to whoever made the request (usually a web browser). The output is often in HTML.
So, the servlet needs to output the doctype because that’s just part of the HTML page that it produces.
0
solved Why do we use String docType in java? also tell me their advantages? [duplicate]