This article mainly introduces the relevant information on the basics of servlet in detail, which has certain reference value. Interested friends can refer to it
Servlet is a tool specifically used to develop dynamic web resources. technology, Sun provides a Servlet interface in its API (of course, we will not directly implement this interface, but it will be better to inherit its implementation class). Therefore, Servlet in the narrow sense refers to this interface, and in the broad sense, Servlet refers to this interface. Servlet refers to any class that implements this Servlet interface. Using Servlet to develop a dynamic web resource is actually developing a Java program to output data to the browser.
Servlet is actually a Java program that runs on the server. Servlet is one of the thirteen technologies of J2EE. Therefore, we cannot read the J2SE API documentation. If you are a beginner, it is best to read the specialized Servlet API documentation. Let’s take a look at the document introduction of Servlet:
From this introduction to the Servlet interface, we can see that if you want to implement the Servlet interface, you must implement all its methods. Servlet The program runs on the web server and is used to receive and respond to requests from clients. Of course, as I said before, if you write a class to implement the Servlet interface, you will have to override all the methods of the Servlet, but we can inherit its implementation class, such as the GenericServlet class or the HttpServlet class, so that we only need to override the ones we want to override. Just use the method.
The methods in Servlet include its life cycle methods and non-life cycle methods. In Servlet, life cycle methods include: init(ServletConfig config), destroy(), service(ServletRequest req, ServletResponse res), that is, Servlet initialization, response service to requests, and destruction of Servlet.
In addition to the Servlet life cycle methods, there are also non-life cycle methods, such as getServletConfig() and getServletInfo() methods, which can obtain some information about the Servlet.
The Servlet life cycle refers to the process from creation to responding to client requests and finally destruction of a Servlet instance. The specific process is as follows:
1. Server creates an instance of Servlet, that is, calls the init() method;
2. A client request (object) reaches the Server;
3. Server sends the request to Servlet;
4. Servlet generates a response (object) to the request;
5. Server activates the service() method of Servlet, passing the request object and The response object is used as a parameter;
6. The Service() method obtains the information of the request object, processes the request, accesses resources, and obtains the required information;
7. The Service() method uses the response object method, the response is sent back to the server and finally reaches the client. The Service method may also activate other methods to process requests, such as the doGet() or doPost() method;
8. For more client requests, the Server creates new request and response objects and still activates this Servlet. service method, pass these two objects to it as parameters, without calling the init() method. Generally, Servlets are only initialized once. When the Server no longer needs the Servlet (usually the Server is shut down), the Server calls the Servlet's destroy() method to destroy the Servlet.
As can be seen from the above, the three methods in the Servlet life cycle are called by the server, which can be said to be at a certain moment in the process from the beginning of the Servlet's existence to its destruction (if an event is triggered) The method that must be executed is called a life cycle method.
Therefore, the most important thing in Servlet is the service() method. If you want to transfer resources from the server back to the client or send data to the client, you will do it in the service() method. .
Judging from the service(ServletTequest req, ServletResponse res) method, not only the service() method is called and executed by the server, but also the ServletTequest request object and ServletResponse response object are also provided by the server. If we want to write a simple After the data is given to the client, you can operate the ServletResponse response object in the service() method:
As a starter, we don’t need to use the development tool IDE to write the Servlet, but write it manually first, which will help to understand the Servlet. underlying principles. Create my web application in the [webapps] directory of Tomcat. The directory where the web application is located is [myservlet]. We first create [WEB-INF] in the [myservlet] directory, and then create [classes] in [WEB-INF]. ] directory, [lib directory] and web.xml file, in the [classes] directory, create my Java program: FirstServlet.java
Because the Servlet we write is called by the server, the Servlet implementation class we create must be public. Let me start by saying that we do not need to implement all the methods of the Servlet, so we ask the client To transfer data, we only need to override the service() method. Then we only need to inherit the implementation class of Servlet, and then obtain the output stream of the corresponding object ServletResponse ServletOutputStream to output data to the client. At the same time, because the output is Byte stream, so you need to convert the characters into a byte array, and then manually write the custom package name and the Java package to be imported:
package fjdingsd.web; import java.io.*; import javax.servlet.*; public class FirstServlet extends GenericServlet{ public void service(ServletRequest req, ServletResponse res) throws ServletException, java.io.IOException { OutputStream out = res.getOutputStream(); out.write("Hello Servlet".getBytes()); } }
Use cmd to run this Java program Compile, but be aware that "javac" only imports the J2SE package by default, but does not include the J2EE package. So where do we go to find the J2EE Servlet package? In fact, because Tomcat supports Servlet, Tomcat's [lib] directory contains the Servlet JAR package:
So we should first perform this step in cmd to set the environment variable: set classpath = %classpath%; path/servlet-api.jar
Then you can compile the Java program just now: javac –d. Program name.java
("-d" represents the directory where the .class file is stored for the next command, and "." represents the current directory)
After the compilation is successful, you can see that there are bytecodes in the directory where the web application is located. The file and package names are:
Of course, there is already a Servlet program, but the browser cannot access it yet because the Servlet program has not yet set an external access path. So where do we configure the Servlet bytecode file we just created into a path that the browser can access?
The answer is in the web.xml file in this web application. Now we only take the simplest format. As mentioned in "Tomcat Detailed Learning Method (3)", change Tomcat's web.xml Copy the header and tail in the "template" into your own web. The 700b5f17c4d842e4bd410f680f40946b in 46309ed845064fdb06e746051efff9e0 and 870ae7edaa11700bcea972d006efb06e must be consistent, the b472d9135dbff3dd7fcc77f5995c97d0 in 46309ed845064fdb06e746051efff9e0 must have the complete package name and class name, and af2014856241039174d7db410be657ad in mapping> is the external access path we can set, and the path can be customized. The setting of this path does not need to be written, otherwise it will be the default value. Then the input address in the browser only needs to have the web application name, but even if it is set to the default value, the content in 66e1775cbd9d5002635ae3285442ba88 must also be There is a slash: "/".
At this time, open the Tomcat server and you can access the Servlet program you just wrote in the browser. The input format is: host name: port (80 does not Required)/web application name/external access path
If it is for Servlet access, then you only need to write the external access path. If you write the name of the Servlet, it is also Inaccessible, as shown below:
There is another problem. If the external access paths of multiple Servlets in the xml file are configured the same, they will also be inaccessible, as follows Figure:
Therefore, please configure different external access paths in custom web.xml for different Servlets.
The above is the detailed content of Detailed explanation of the basic knowledge of servlets in Java. For more information, please follow other related articles on the PHP Chinese website!