The remainder in the javax.servletand javax.servlet.httppackages are largely

The remainder in the javax.servletand javax.servlet.httppackages are largely support classes. For example, the ServletRequestand ServletResponseclasses in javax.servletprovide access to generic server requests and responses, while HttpServletRequestand HttpServletResponsein javax.servlet.httpprovide access to HTTP requests and responses. The javax.servlet.httppackage also contains an HttpSessionclass that provides built-in session tracking functionality and a Cookieclass that allows you to quickly set up and process HTTP cookies. Page Generation The most basic type of HTTP servlet generates a full HTML page. Such a servlet has access to the same information usually sent to a CGI script, plus a bit more. A servlet that generates an HTML page can be used for all the tasks where CGI is used currently, such as for processing HTML forms, producing reports from a database, taking orders, checking identities, and so forth. Writing Hello World Example 2-1 shows an HTTP servlet that generates a complete HTML page. To keep things as simple as possible, this servlet just says ‘’Hello World” every time it is accessed via a web browser.* Example 2-1. A servlet that prints “Hello World” import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType ( text/html ); PrintWriter out = res.getWriter(); * Fun trivia: the first instance of a documented “Hello World” program appeared in A Tutorial Introduction to the Language B, written by Brian Kernighan in 1973. For those too young to remember, B was a precursor to C. You can find more information on the B programming language and a link to the tutorial at http://cm.bell-labs.com/who/dmr/bintro.html. Example 2-1. A servlet that prints “Hello World” (continued) out.println(”“); out.println(”Hello World“); out.println(”“); out.println(”Hello World“); out.println(”“); } } This servlet extends the HttpServletclass and overrides the doGet()method inherited from it. Each time the web server receives a GET request for this servlet, the server invokes this doGet()method, passing it an HttpServletRequestobject and an HttpServletResponseobject. The HttpServletRequestrepresents the client’s request. This object gives a servlet access to information about the client, the parameters for this request, the HTTP headers passed along with the request, and so forth. Chapter 4 explains the full capabilities of the request object. For this example, we can completely ignore it. After all, this servlet is going to say “Hello World” no matter what the request!

Hint: If you are looking for very good and affordable webspace to host and run your java hosting application check Sandzak.com java web hosting provider

Comments are closed.