HttpServlet
- What is Servlet’s Job?
- Servlets are Java programs that run on Web or Application servers.
- It is acting as a middle layer between requests coming from Web browsers or other HTTP clients and databases or applications on the HTTP server.
- Figure:
- Jobs are:
- Read the explicit data sent by the client.
- Read the implicit HTTP request data sent by the browser.
- Generate the results.
- Send the explicit data (i.e., the document) to the client.
- Send the implicit HTTP response data.
- The HttpServlet class extends the GenericServlet class and implements Serializable interface.
- It provides http specific methods such as doGet, doPost, doHead, doTrace etc.
- public void service(ServletRequest req,ServletResponse res):dispatches the request to the protected service method by converting the request and response object into http type.
- protected void service(HttpServletRequest req, HttpServletResponse resp):receives the request from the service method, and dispatches the request to the doXXX() method depending on the incoming http request type.
- protected void doGet(HttpServletRequest req, HttpServletResponse resp):handles the GET request. It is invoked by the web container.
- protected void doPost(HttpServletRequest req, HttpServletResponse resp):handles the POST request. It is invoked by the web container.
- protected void doHead(HttpServletRequest req, HttpServletResponse resp):handles the HEAD request. It is invoked by the web container.
- protected void doOptions(HttpServletRequest req, HttpServletResponse resp):handles the OPTIONS request. It is invoked by the web container.
- protected void doPut(HttpServletRequest req, HttpServletResponse resp):handles the PUT request. It is invoked by the web container.
- protected void doTrace(HttpServletRequest req, HttpServletResponse resp):handles the TRACE request. It is invoked by the web container.
- protected void doDelete(HttpServletRequest req, HttpServletResponse resp):handles the DELETE request. It is invoked by the web container.
- protected long getLastModified(HttpServletRequest req):returns the time when HttpServletRequest was last modified since midnight January 1, 1970 GMT.
- Servlet methods has main two types of arguments:
- HttpServletRequest Vs. HttpServletResponse
- The HttpServletRequest lets you get at all of the incoming data;
- It has methods by which you can find out about information such as form (query) data, HTTP request headers, and the client’s hostname.
- The HttpServletResponse lets you specify outgoing information such as HTTP status codes (200, 404, etc.) and response headers (Content-Type, Set-Cookie, etc.).
- Most importantly, HttpServletResponse lets you obtain a PrintWriter that you use to send document content back to the client.
- For simple servlets, most of the effort is spent in println statements that generate the desired page.
- Generating plain text using Servlet
- Exmple:
- How it works?
- Generating HTML using Servlet
- Most servlets generate HTML, not plain text.
- To generate HTML, you add three steps:
- 1. Tell the browser that you’re sending it HTML.
- You accomplish the first step by setting the HTTP Content-Type response header to text/html.
- There is also a special setContentType method just for this purpose.
- Example:
- response.setContentType("text/html"); // for HTML output
- response.setContentType("application/vnd.ms-excel"); // for Excel spreadsheet output
- 2. Modify the println statements to build a legal Web page.
- 3. Check your HTML with a formal syntax validator.
- PrintWriter is an abstract class for writing to character streams.
- Print formatted representations of objects to a text-output stream.
- This class implements all of the print methods found in PrintStream.
- Example:
Tags:
HttpServlet