A Servlet is to complete the interaction between the browser and the server, the browser sends the request, the server responds, since both can communicate information, then they must have a specification, this specification is the HTTP protocol, with this agreement, no matter what’s your browser, the information server to all know.

Here we take Tomcat as an example to explain the server, if you are not familiar with Tomcat, you can learn about The introduction to Tomcat

Now that you know that the direct interaction between the browser and the server is agreed upon over HTTP, and you know about the Tomcat server, let’s write our first servlet class. First you need to create a Web project, create a new class in your Web project, Let the class inherit the HttpServlet, override the service method, and find the web. XML file to configure the servlet. Then copy the class file and the web. XML file to the webapps on the server. Next I’ll demonstrate the code and the results.

The first step is to write a class under your Web project. I wrote this class into the com.zhiying package, using MyServlet as an example and inheriting HttpServlet

package com.zhiying;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MyServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("this is my first servlet"); // This is to print a sentence in the browser}}Copy the code

Once you’ve written the servlet class, it’s time to configure the web.xml file


      
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <servlet>
        <! --servlet-name is a name given to the servlet class, in this case my-->
        <servlet-name>my</servlet-name>
        <! -- servlet-class is the path of your class, is the package name plus the class name -->
        <servlet-class>com.zhiying.MyServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <! -- The servlet-name must be the same as the one above to form a group -->
        <servlet-name>my</servlet-name>
        <! -- where url-pattern is a mapping of the path above, it is used for browser access -->
        <url-pattern>/my</url-pattern>
    </servlet-mapping>
</web-app>
Copy the code

Once you’ve configured this, you’re ready to run. You need to run Tomcat in the compiler, because there’s no main function in this class, and Tomcat can call the servlet class directly. In this case, if your compiler doesn’t have Tomcat integrated, you need to integrate Tomcat into the compiler first. Now you can run it. After you run it, you copy the.class and web.xml files you generated into Tomcat webapps. You create a new folder in webapps, in this case project, Both files are in the web-INF directory, so just copy the web-INF directory, and you’ll see that there is an index.jsp. This will also be copied to the Project in webapps, and it will not affect your execution. When you are done, you can demonstrate. And an explanation of index.jsp.

Start the server, enter the url localhost: 8080 / project/my if you see the information below, then prove that you are successful, is under the project project/my visit here, The /my path here will map com.zhiying.MyServlet to your web.xml configuration. The reason why the package name plus the class name is not used is that the package name plus the class name can create objects through reflection, which is not safe to display in the browser.

To explain the index.jsp, you enter the url localhost:8080/project to access the project project. You will see the following screenshot, which is the default access to the index.jsp and actually executes its corresponding servlet file

At this point, you have successfully written a servlet