What are Servlets?
Java Servlets are programs that run on a Web or Application
server and act as a middle layer between a requests coming
from a Web browser or other HTTP client and databases or
applications on the HTTP server.
Servlets Tasks
1. Read the explicit data sent by the client.
2. Read the implicit HTTP request data sent by the browser.
3. Generate the results.
4. Send the explicit data (i.e., the document) to the client.
5. Send the implicit HTTP response data.
Servlet definition
Java Servlets are programs that run on a Web or Application
server and act as a middle layer between a requests coming
from a Web browser or other HTTP client and databases or
applications on the HTTP server.
Servlets typically extend HttpServlet and override doGet or doPost,
depending on whether the data is being sent by GET or by POST.
If you want a servlet to take the same action for both GET and POST
requests, simply have doGet call doPost, or vice versa.
doGet
Notice that there is no main() method in above program. When user
enters a URL on the address line of browser the browser sends the
request and the servlet’s doGet() method is called by the Container
(tomcat).
doGet method takes two arguments: HttpServletRequest and
HttpServletResponse
HttpServletRequest lets you get all of the incoming data; like form/
query data, request headers.
HttpServletResponse lets you send document content and response
headers back to the client.
doGet method throw two exceptions ServletException and IOException
you need to include them in method signature.
You can get a PrintWriter from the response object. Use the PrintWriter
object to send document content to the browser.