Page #470 (Key Terms)

 
[Page 1200 ( continued )]

Chapter Summary

  • A servlet is a special kind of program that runs from a Web server that supports servlets. Tomcat is a Web server that can run servlets. You start Tomcat using the startup command and stop Tomcat using the shutdown command from the Tomcat bin directory.

  • A servlet URL is specified by the hostname, port, and request string (e.g., http://localhost:8080/liangweb/ServletClass ). There are several ways to invoke a servlet: (1) by typing a servlet URL from a Web browser, (2) by placing a hyper reference link in an HTML page, and (3) by embedding a servlet URL in an HTML form. All the requests trigger the GET method except that you explicitly specify the POST method in the HTML form.

  • You develop a servlet by defining a class that extends the HttpServlet class, implements the doGet(HttpServletRequest, HttpServletResponse) method to respond to the GET method, and implements the doPost(HttpServletRequest, HttpServletResponse) method to respond to the POST method.

  • The request information passed from a client to the servlet is contained in an object of HttpServletRequest . You can use the methods getParameter , getParameter-Values , getRemoteAddr , getRemoteHost , getHeader , getQueryString , getCookies , and getSession to obtain the information from the request.


    [Page 1201]
  • The content sent back to the client is contained in an object of HttpServletResponse . To send content to the client, first set the type of the content (e.g., html/plain) using the setContentType(contentType) method, then output the content through an I/O stream on the HttpServletResponse object. You can obtain a character PrintWriter stream using the getWriter() method and obtain a binary OutputStream using the getOutputStream() method.

  • HttpServletResponse extends the ServletResponse interface to provide HTTP-specific functionality in sending a response. You can use the getWriter method to get an instance of PrintWriter to send a response to the client and the setContentType method to set the type of the content, such as html.

  • There are three ways to track a session. You can track a session by passing data from the servlet to the client as a hidden value in a dynamically generated HTML form by including a field such as <input type="hidden" name ="lastName" value="Smith"> . The next request will submit the data back to the servlet. The servlet retrieves this hidden value just like any other parameter value using the getParameter method.

  • You can track sessions using cookies. A cookie is created using the constructor new Cookie(String name, String value) . Cookies are sent from the server through the object of HttpServletResponse using the addCookie(aCookie) method to tell the browser to add a cookie with a given name and its associated value. If the browser already has a cookie with the key name, the value will be updated. The browser will then send the cookie with any request submitted to the same server. Cookies can have expiration dates set, after which they will not be sent to the server.

  • Java servlet API provides a session-tracking tool that enables tracking of a large set of data. A session can be obtained using the getSession() method through an HttpServletRequest object. The data can be stored as objects and are secure because they are kept on the server side using the setAttribute(String name, Object value) method.

  • Java servlets are not limited to sending text to a browser. They can return images in GIF, JPEG, or PNG format.

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net