Example 2-3. The Hello servlet modified to return

Example 2-3. The Hello servlet modified to return quickly in response to HEAD requests import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Hello extends HttpServlet { public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { // Set the Content-Type header res.setContentType( text/html ); * Jason is proud to report that Sun added this feature in response to comments he made during beta testing. Example 2-3. The Hello servlet modified to return quickly in response to HEAD requests (continued) // Return early if this is a HEAD if (req.getMethod().equals ( HEAD )) return; // Proceed otherwise PrintWriter out = res.getWriter(); String name = req.getParameter ( name ); out.println( ); out.println( Hello, + name + ); out.println( ); out.println( Hello, + name); out.println( ); } } Notice that we set the Content-Typeheader, even if we are dealing with a HEAD request. Headers such as these are returned to the client. Some header values, such as Content-Length, may not be available until the response has already been calculated. If you want to be accurate in returning these header values, the effectiveness of this shortcut is limited. Make sure that you end the request handling with a returnstatement. Do not call System.exit(). If you do, you risk exiting the web server. Server-Side Includes All the servlets you’ve seen so far generate full HTML pages. If this were all that servlets could do, it would still be plenty. Servlets, however, can also be embedded inside HTML pages with something called server-side include (SSI) functionality. In many servers that support servlets, a page can be preprocessed by the server to include output from servlets at certain points inside the page. The tags used for a server-side include look similar to those used for applets:* If you see this text, it means that the web serverproviding this page does not support the SERVLET tag.
Quick Hint: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services

Comments are closed.