Basic Structure of Servlet

Source: Internet
Author: User

The following code shows the basic structure of a simple Servlet. The Servlet processes GET requests. If you are not familiar with HTTP, it can be viewed as a request sent by the browser when a user enters a URL in the browser address bar, clicks a link on the Web page, and submits a form without a specified METHOD. Servlet can also easily process POST requests. A POST request is a request sent when a form with the specified METHOD = "POST" is submitted.

 
 
  1. Import java. io .*;
  2. Import javax. servlet .*;
  3. Import javax. servlet. http .*;
  4.  
  5. Public class SomeServlet extends HttpServlet {
  6. Public void doGet (HttpServletRequest request,
  7. HttpServletResponse response)
  8. Throws ServletException, IOException {
  9.  
  10. // Use "request" To Read request-related information, such as Cookies)
  11. // And form data
  12.  
  13. // Use "response" to specify the HTTP response status code and response Header
  14. // For example, specify the content type and set the Cookie)
  15.  
  16. PrintWriterOut=Response. GetWriter ();
  17. // Use "out" to send the response content to the browser
  18. }
  19. }

The basic structure of the Servlet. If a class is to be a Servlet, it should inherit from the HttpServlet and be sent through GET or POST based on the data, overwriting one or all of the doGet and doPost methods. The doGet and doPost methods both have two parameters: HttpServletRequest and HttpServletResponse. HttpServletRequest provides methods to access request information, such as form data and HTTP request headers. HttpServletResponse provides methods for specifying HTTP response status 200,404, etc.), response header Content-Type, Set-Cookie, and so on, most importantly, it provides a PrintWriter for sending data to the client. For a simple Servlet, most of its work is to generate a page sent to the client through the println statement.

Note that doGet and doPost throw two exceptions, so you must include them in the Declaration. In addition, you must import java. i/O packages use PrintWriter and other classes), javax. the servlet package must use HttpServlet and other classes) and javax. servlet. the HttpServletRequest class and HttpServletResponse class are used for the http package ).
Finally, the doGet and doPost methods are called by the service method. Sometimes you may need to directly overwrite the service method. The above is the basic Servlet Structure

  1. Introduction to functions of Servlet 2.4
  2. Introduction to multiple Servlet Interfaces
  3. Interface introduction-Servlet Context
  4. Servlet Registration Method
  5. Introduction to Servlet containers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.