Danh mục

Java Server Pages: A Code-Intensive Premium Reference- P3

Số trang: 10      Loại file: pdf      Dung lượng: 188.91 KB      Lượt xem: 4      Lượt tải: 0    
Thư viện của tui

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Java Server Pages: A Code-Intensive Premium Reference- P3:Before you begin reading Pure JSP Java Server Pages, you might want to take a look at its basicstructure. This should help you outline your reading plan if you choose not to read the text from cover tocover. This introduction gives you an overview of what each chapter covers.
Nội dung trích xuất từ tài liệu:
Java Server Pages: A Code-Intensive Premium Reference- P3//Process the HTTP Get requestpublic void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(text/html); PrintWriter out = response.getWriter(); out.println(); out.println(BasicServlet); out.println(); // Prints the REQUEST_METHOD sent by the client out.println(Your request method was + request.getMethod() + ); out.println(); out.close();}//Process the HTTP Post requestpublic void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(text/html); PrintWriter out = response.getWriter(); out.println(); out.println(BasicServlet); - 21 - out.println();// Prints the REQUEST_METHOD sent by the client out.println(Your request method was + request.getMethod() + ); out.println(); out.close(); }//Get Servlet information public String getServletInfo() { return BasicServlet Information; }}The HTML Required to Invoke the ServletThis servlet implements both the doGet() and the doPost() methods. Therefore there are two ways toinvoke this servlet.The first is to just reference the servlet by name in the URL. The following URL will execute the servlet onmy local server:http://localhost/servlet/BasicServletUsing this method defaults the request method to GET, which will invoke the servlets doGet() method.The second way to invoke the servlet is to create an HTML page that will send a request to the servletusing the POST method. This will invoke the servlets doPost() method. Listing 2.2 shows the HTMLlisting to complete this task.Listing 2.2: BasicServlet.html Displays the HTML Required to Invoke the Servlet Using the POSTMethodBasicServlet - 22 - press Submit Query to launch servlet BasicServlet When you invoke the servlet using either of these methods, the results will be similar to Figure 2.5. Theonly notable difference will be the request method returned.Figure 2.5: The BasicServlet HTML response page.Dissecting the BasicServletNow that you have the BasicServlet installed and running, lets take a closer look at each of its integralparts. We will be examining the location where the servlet fits into the framework, methods the servletimplements, and the objects being used by the servlet. - 23 -Where Does the BasicServlet Fit into the Servlet Framework?The first thing we are going to look at is where the BasicServlet fits into the servlet framework. Thisservlet extends the HttpServlet class. The HttpServlet class is an abstract class that simplifieswriting HTTP servlets. It extends the GenericServlet class and provides the functionality for handlingHTTP protocol-specific requests. The BasicServlet overrides four of its inherited methods. Figure 2.6shows where the BasicServlet fits into this hierarchy.Figure 2.6: The BasicServlet depicted in the framework.The Methods Overridden by the BasicServletThe following four methods are overridden by the BasicServlet: init() doGet() doPost() getServletInfo()Lets take a look at each of these methods in more detail.init()The BasicServlet defines a very simple implementation of the init() method. It takes theServletConfig object that is passed to it and passes it to its parents init() method, which stores theobject for later use. The parent that actually holds onto the ServletConfig object is theGenericServlet class. GenericServlet provides your servlet, through inheritance, with methods toaccess the ServletConfig object. The code that performs this action follows:super.init(config);This is a very important step. If you do not do this, you must hold a reference to the ServletConfigobject yourself.You will also notice this implementation of the init() method does not create any resources. This is whythe BasicServlet does not implement a destroy() method.doGet() and doPost()The BasicServlets doGet() and doPost() methods are identical. The only difference is the requeststhey service. The doGet() method handles GET requests, and the doPost() method handles POSTrequests.Both of these methods receive HttpServletRequest and HttpServletResponse objects. Theseobjects encapsulate the request/response paradigm. The HttpServletRequest contains informationsent from the client and the HttpServletResponse contains the information that will be sent back to theclient. The first executed line of these methods is listed as follows:response.setContentT ...

Tài liệu được xem nhiều: