In Tomcat's first attempt, we built a basic environment for testing the servlet and JSP. We now turn to the specific development of Web applications.
Cook
The basic way a Web server works is to request-process-reply. The request and reply is on the network, based on the HTTP Protocol (Reference HTTP protocol). The request is a customer order, reply like a waiter serving food, and processing is in the kitchen, the chef according to the request menu, prepare the course of the dish.
The chef is usually an application on the server. The application can extract the information from the request and prepare a reply based on that information. Such applications can be written in many languages, such as C, C + +, Perl, Ruby, Python, Ruby, PHP, and so on. Because of the different design concepts and compiler features of different languages, the applications written in these languages have different characteristics (various chefs). For example, C and C + + language will have a relatively high operational efficiency, PHP Web application is extensive, ruby and Python development is convenient and so on.
(in the language dispute, the Web server's "Back Kitchen" is a battleground)
The servlet is a "chef" provided by the Java language. In Java, "all objects". A servlet is a special kind of Java object. The Java language utilizes the Servlet object to implement processing of HTTP requests.
Examples
The following is a simple servlet example, the source file is Testpage.java:
Package foo;
Import javax.servlet.*;
Import javax.servlet.http.*;
Import java.io.*;
public class Testpage extends HttpServlet {public
void doget (HttpServletRequest request,
HttpServletResponse Response)
throws IOException, servletexception
{
PrintWriter out = Response.getwriter ();
Out.println ("
The function of this servlet is to reply to the string "
This defines a Testpage class that inherits HttpServlet abstract classes (reference HttpServlet). We covered the Doget () method. The Doget () method handles the HTTP request of the GET method. If the HTTP request method is post, the Dopost () method should be overwritten. Each servlet should overwrite one of the doget () and Dopost (). The Doget () and Dopost () methods receive two parameter request and response, respectively, HttpServletRequest type and httpservletresponse type. These two parameters represent the request and reply of the HTTP communication.
HttpServletRequest and HttpServletResponse are two interfaces. Within the method, we can manipulate the request and response objects. For example, the Getwriter method of response is used to obtain writer, thus writing HTML text. These written text is passed back to the client as the body of the HTTP reply. For example, we can use the GetMethod () method of the request to learn the HTTP request method. Refer to the following official documentation:
More Ways to HttpServletRequest
More Ways to HttpServletResponse
In addition, the PrintWriter class comes from the Java.io package. Reference Java IO
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/