Servlets doGet doPost AND?
Good day!
I am reading Head First Servlets and JSP and it says that 99% of servlets make use of the HttpServlet. What is the other 1%? And it says that in the real world, 99.9% of all servlets override either that doGet() or doPost() method. What is the other 0.1%? Or it isn't worth mentioning that's why the book didn't men开发者_开发百科tion it?
Thank you.
The 1% doesn't make use of abstract HttpServlet
class. So they use Servlet
interface without extending HttpServlet
.
Currently in the Java EE API, there's only one other Servlet
which does that: the FacesServlet
which is the core controller behind the Java EE provided MVC framework JavaServer Faces (JSF). JSF enables you to skip all the tedious process and the boilerplate code which is necessary to gather, convert and validate parameters, update the model values and invoke specific action methods.
But at the time of writing this book, the author likely didn't realize that. As of now, JSF certainly doesn't account for only 1%. In theory it's possible to implement Servlet
for other protocols than HTTP, like FTP. This is not provided by the standard Java EE API, but there are some 3rd party "FtpServlet" classes out. And I believe some Portlet APIs also use a non-HttpServlet
class (they just implement Servlet
and don't extend HttpServlet
).
As to the HTTP methods, next to HTTP GET
and POST
there are also HEAD
, PUT
, OPTIONS
, etc. But I think 0.1% is heavily underestimated. The HEAD
is definitely much more often used, think of servletcontainer's own DefaultServlet
(like as Tomcat has). The HEAD
plays an important role in browser cache requests. But when it comes to "homegrown" servlets, then it are indeed GET
and POST
which gets the lone attention.
See also:
- What is the difference between JSP, Servlets and JSF?
You can write FTP using servlets. That's probably the other 0.1%.
You can use the
protected void service(HttpServletRequest request, HttpServletResponse response)
which can handle both GET and POST requests. It's just an addendum. =]
精彩评论