The study of the servlet (i.)

Source: Internet
Author: User

First Knowledge servlet

The servlet is a technology dedicated to developing dynamic Web resources, and Sun provides a servlet interface in its API (of course, we do not directly implement this interface, but rather to inherit its implementation class would be better), so the narrow-sense servlet refers to this interface, A generalized servlet refers to any class that implements the Servlet interface. Developing a Dynamic Web resource using a servlet is actually developing a Java program that outputs data to the browser.

Servlet is actually a Java program running on the server, servlet is one of the 13 gate technology, so we can not go to see J2SE API documentation, if it is a beginner, it is best to see the API documentation of the servlet. Let's look at the documentation for the servlet:

From this introduction to the Servlet interface, it can be seen that if you want to implement a servlet interface, it is necessary to implement all of its methods, the Servlet program is run on the Web server, to receive and respond to requests from the client. Of course, as I said before, if you write a class to implement a servlet interface, you need to replicate all of the servlet's methods, but we can inherit its implementation class, such as the Genericservlet class or the HttpServlet class, This only requires a copy of the method we want to replicate.

The methods in the servlet include their life cycle methods and non-life cycle methods. In the servlet, the life cycle methods include: init (servletconfig config), destroy (), service (servlettequest req, servletresponse Res), That is, the servlet initializes, responds to the request, and the servlet destroys.

In addition to the servlet lifecycle approach, there are non-life-cycle methods, such as the Getservletconfig () and Getservletinfo () methods, which can be used to obtain some information about the servlet.

The servlet lifecycle is the process of creating a servlet instance from creation to responding to a client request and finally destroying it. The process is as follows:

1, the server creates an instance of the servlet, which is called the Init () method;

2, a client request (object) arrives at the server;

3, the server sends the request to the servlet;

4, the servlet generates a response (object) to the request;

5, the server activates the servlet's service () method, passing the request object and the response object as parameters;

6, the Service () method obtains the request object information, processes the request, accesses the resource, obtains the necessary information;

7, the Service () method uses the method of the response object to pass the response back to the server, eventually reaching the client. The service method may also activate other methods to process the request, such as the doget () or Dopost () method;

8, for more client requests, the server creates a new request and response object, still activates the servlet's service method, passes the two objects as parameters to it, and no longer calls the Init () method. The generic servlet is initialized only once. The server calls the servlet's Destroy () method to destroy the servlet when the server is not in need of a servlet (typically server shutdown).

As can be seen from the above, the three methods of the servlet life cycle are called by the server, and it can be said that the method that is bound to be executed at some point in the process of the servlet's existence start to destroy (if there is an event trigger) is called the life cycle method .

Therefore, the most important thing in the servlet is the service () method, which will be done in the service () method if you want to pass the resource back to the client from the server side or send the data to the client.

Judging from the service (servlettequest req, Servletresponse res) method, not only the service () method is executed by the server call, Even servlettequest request objects and Servletresponse response objects are also provided by the server, and if we are going to write a simple data to the client, we can manipulate the Servletresponse response object in the service () method:

As a starting point, we don't need to develop a tool IDE to write a servlet, but to write it manually, which helps to understand the underlying principles of the servlet. To build my web App in Tomcat's "WebApps" directory, where the Web application is "Myservlet", and based on the organization of the Web application described in the Tomcat detailed learning Method (iii), we first "Web-inf" in the "Myservlet" directory, and then create the "classes" directory in "Web-inf", "Lib Directory" and web. xml file, in the "Classes" directory, Build my Java program: Firstservlet.java

Because the servlet that you write is called by the server, the Servlet implementation class that we create must be public , and we start by saying that we don't have to implement all of the servlet's methods. So we only need the Replication service () method to transmit data to the client, then we only need to inherit the Servlet implementation class, and then obtain the corresponding object Servletresponse output stream Servletoutputstream can output data to the client, Also, because the output is a byte stream, you need to convert the character to an array of bytes, and then manually write the custom package name and the Java package to import:

 Package Fjdingsd.web; import java.io.*; import javax.servlet.*;  Public class extends genericservlet{     publicvoidthrows  servletexception, Java.io.IOException     {          = res.getoutputstream ();          Out.write ("Hello Servlet". GetBytes ());}     }

Use CMD to compile the Java program, but note that the "Javac" is just the default import of J2SE packages, and no Java EE packages. So where do we go to find the servlet bundle of the Java EE? In fact, because Tomcat supports Servlets, Tomcat's "Lib" directory contains the servlet's jar package :

So we should start this step in CMD to set the environment variable: set classpath =%classpath%; Path/servlet-api.jar

You can then compile the Java program: Javac–d. Program name. java

("-D" means the following command holds the directory for the. class file, ".") Represents the current directory)

After the compilation is successful, you can see that the Web application is in the same directory as the bytecode file and the package name:

Of course, the servlet program is now available, but the browser is not yet accessible because the servlet program has not set the external access path. So where do we configure the servlet bytecode file we just created as a path that the browser can access?

The answer is that in the Web. xml file, now we only take the most concise format, as described in the Tomcat detailed learning Method (iii), the "template" for Tomcat's web. Copy the header and tail into your own web. XML document, and copy some, see:

There are more important points of knowledge here.,<servlet> and <servlet-mapping> <servlet-name> must be consistent,<servlet> < Servlet-class> must have the full package name and class name, and <url-pattern> in <servlet-mapping> is the external access path we can set, and we can customize the path. The setting of this path can not be written, then the default value, then the browser input address as long as the Web application name can be, but even if set as the default, in <url-pattern>, the content must have a slash: "/".

At this point, the Tomcat server is turned on, you can access the browser just written servlet program, entered in the format: hostname: port (80 not required)/web application name/external access path

If you are accessing the servlet, you only need to write to the external access path, if you write the name of the servlet is also inaccessible, such as:

Another problem is that if the external access paths of multiple Servlets are configured in the XML file, they are also inaccessible, such as:

Therefore, different servlets should configure different external access paths in the custom Web. Xml.

The study of the servlet (i.)

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.