This article mainly introduces the relevant information of myeclipse to create servlets in detail, which has certain reference value. Interested friends can refer to it
Now let's create a web application, which is called [myservlet] Ok, as shown below:
As you can see from the project window, as long as we create a web project, it will automatically create [WEB-INF] for us. directory, and create the [lib] directory and web.xml file under it. Let's take a look at the content of the web.xml file we just created:
The Web.xml file only sets the homepage. Why look at this first? I will talk about it later.
Of course we can create a Servlet according to the method in "Servlet Learning (1)", that is, create a class to inherit the Servlet implementation class GenericServelet, override the service() method, and add it in the web.xml file Configure this Servlet.
but! ! ! We do not use the method of inheriting the GenericServelet class, but inherit the HttpServlet class, a subclass of the GenericServelet class. What are the benefits of doing this? HttpServlet refers to a Servlet that can handle HTTP requests. It adds some HTTP protocol processing methods to the original Servlet interface, which is more powerful than the Servlet interface, so we only need to inherit the HttpServlet class. If you create a Servlet for a web project in MyEclipse, you can also see that MyEclipse inherits the Servlet you create from HttpServlet by default, as shown in the following figure:
Select [Servlet] , pop up, write the package name and the name of the Servlet I want to create: SecondServlet, as shown below:
You can see that MyEclipse automatically selects the parent class of the Servlet you want to create as HttpServlet.
When we select the HttpServlet class as the Servlet parent class to inherit, then we should know that the HttpServlet class has overridden the service() method. The code in this method will automatically determine the user's request method. If it is GET For requests, the doGet() method of HttpServlet is called; if it is a POST request, the doPost() method is called. Therefore, when we develop, we only need to override the doGet() method or doPost() method, and there is no need to override the service. ()method. For details, please see the Servlet API documentation for details about HttpServlet.
We check the doGet method and doPost method, and add other methods according to the actual situation. Then click [Next] and you will see another setting window:
【Generate/Map web.xml file】: Map the created Servlet to the web.xml file , this can save us a lot of things. When we choose to map the Servlet into the web.xml file, we can set what content is mapped to the web.xml file according to the following specifications.
【Servlet/JSP Class Name】: This is the content in the b472d9135dbff3dd7fcc77f5995c97d0 tag in the web.xml file.
[Servlet/JSP Name]: Set the content in the 700b5f17c4d842e4bd410f680f40946b tag in the web.xml file.
[Servlet/JSP Mapping URL]: Set the content of the 66e1775cbd9d5002635ae3285442ba88 tag under the 870ae7edaa11700bcea972d006efb06e tag in the web.xml file, that is, set the external access path of the Servlet.
[File Path of web.xml]: The path of the web.xml, generally does not need to be set.
After clicking Finish, let’s take a look at the web.xml file again. Yes, the mapping from the Servlet to the web.xml file that we just set has been added by MyEclipse in the web.xml file, which saves us trouble. A lot. As shown in the figure below:
Let’s take a look at the content in the Servlet:
Yes, it’s a bit messy, This is because MyEclipse generates Servlets based on templates when creating them. Here we can remove these unnecessary codes first. In the last part, we will explain how to modify the Servlet template in MyEclipse.
In this Servlet development that inherits HttpServlet, we only care about the doGet method and the doPost method. If you don’t know when the http request is GET and when it is POST, you can use the following "clever" method To handle GET requests or POST requests at the same time in one method:
Just mentioned that the service() method in HttpServlet has overridden the service() method of its parent class GenericServlet, then let’s take a look at the service() method in HttpServlet, click the cursor on HttpServlet, and then press the keyboard "F3" key, you will find:
The Servlet jar package source code is missing. Here we have two solutions:
1. Go to the sun official website or Download the Servlet source code from the oracle official website;
2. Go to the Apache-Tomcat official website to download the Tomcat source code. Here I choose this method:
Just choose the zip format (looks like tar.gz is for Linux).
After downloading and decompressing, the directory is followed by the word "src":
Then we can click "Attach Source" in MyEclipse, because it is Import the entire directory, so select "External Folder". After importing, we can see the source code of HttpServlet and its service() method. The code is long and will not be attached here, but the idea is to get the request object first. HttpServletRequest request method, and then use if judgment to call different methods for each request, such as doGet method or doPost method.
Next we can safely return to the Servlet we just created and write code in the doGet method. We will simply write a simple return to the client with some data:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { OutputStream out = response.getOutputStream(); out.write("Hello servlet again !".getBytes()); }
Then start the server and publish the written Servlet
Finally, we can view it in the browser:
Note After entering the host address and port (if any) in the address bar, if the web.xml is not set otherwise, it will be followed by the web project name and the external access path you set for the Servlet in the web.xml file.
The above is the detailed content of Graphical tutorial on creating servlet using Java myeclipse. For more information, please follow other related articles on the PHP Chinese website!