What servlet api is trying to solve?
edit Ok People! No need to bring this question to the abysses of SO. It might be a bit a rude question and/or rude phrasing/language for some SO elitits and/or Java/Sun/Oracle fan boys but I still believe that the question has it's place here and that is a legitimate question. Look at @Thilo answer, this is actually constructive and gave me almost all I needed to understand, have a better perspective about why the Servlet API is as it is.
The servlet API looks awkward, I don't understand it.
- What are the benefits of it ?
- What problems is it tryi开发者_开发知识库ng to solve ?
- What is it good at ?
- Doesn't the API forget good web practices like REST architecture ?
- Why should we want to use application, session, request scope instead of other solutions ?
- What does bring the API that don't a simple HTTP handler
Basically why is the Java platform the only one that has such an API where others use routing or URL to filesystem maps ?
- What are the benefits of it ?
It allows you to write (web) server code that runs in all kinds of application servers. It is a minimal base upon which more high-level frameworks can be built. I'd say it is the Java equivalent of the CGI protocol.
- What problems is it trying to solve ?
Define a standard interface to handle requests where the application server manages the communication with the user and the servlet implements to logic.
The main idea is that the programmer can concentrate on writing just the servlet, without having to worry about the whole HTTP stack and server (that is what the application server as a packaged solution takes care of).
It tries to handle more than just HTTP (such as requests by email), which is maybe a little overreaching (or, granted, awkward in regular use).
- What is it good at ?
It succeeded in becoming the standard. It has been adopted by all Java application server vendors. There are no relevant competing solutions (maybe some native protocols, but nothing cross-platform).
- Doesn't the API forget good web practices like REST architecture ?
The API is not concerned with that. It works at a lower level. TCP/IP is also not interested in good web practices.
- What does bring the API that don't a simple HTTP handler
The Servlet API is the Java way of doing a simple HTTP handler. There is really not much more to it. Unless by simple you mean that it does not need any lifecycle and configuration management.
How else would you write a "simple HTTP handler"? If your answer is "start with public static void main, bind a listening socket, and go from there", then yes, you don't need the servlet API. But if you want to re-use any kind of standard tools or libraries, you need some API they all agree on (to encapsulate requests and configuration data and so on).
If you tie yourself to a specific HTTP server, you could actually write code without the servlet API. I think Jetty, for example, has a native interface as an alternative to javax.servlet.
But seriously, there is nothing fundamentally wrong with the servlet API.
Servlet API allows adding custom functionality to web server. It does not forget REST API. REST API was introduced 10 years after Servlet API and is implemented on top of servlet API in java.
Application scope is needed to share data on application layers. The same is about session and request scopes.
精彩评论