Will you implement it through the Servlet accessed by the website?

Servlet is the abbreviation of Server Applet, which means server applet. The main function of the server-side program written in Java language is to interactively browse and generate data and generate dynamic Web content. Servlet mainly runs on the server-side and is called and executed by the server. It is a class developed in accordance with the Servlet standard. It is a technology provided by SUN for developing dynamic Web resources. (The implication: To achieve web development, you need to implement the Servlet standard)

  Servlet is essentially a Java class, but it must be written in accordance with the Servlet specification. There is no main() method. Its creation, use, and destruction are managed by the Servlet container (such as Tomcat). (The implication: write your own class, no need to write the main method, others will automatically call it)

  Servlet is closely related to HTTP protocol, and it can handle all content related to HTTP protocol. This is one of the reasons why Servlet is widely used.

  The server that provides Servlet function is called Servlet container. There are many common containers, such as Tomcat, Jetty, WebLogic Server, WebSphere, JBoss, etc.

Servlet implementation

  • Create a dynamic web project
  • New class
  • Implement Servlet specification
  • Override the service method
  • Configure web.xml
  • Publish project
  • Startup project
  • Visit and view results

Case practice

1) Create a dynamic web project

Will you implement it through the Servlet accessed by the website?

2) New class

Will you implement it through the Servlet accessed by the website?

package com.xxx.web;

public class HelloWeb {

}

3) Implement Servlet specification

To implement the Servlet specification, that is, to inherit the HttpServlet class and import the corresponding package. The communication rules have been completed in this class, and we only need to implement the business.

package com.xxx.web;

import javax.servlet.http.HttpServlet;

public class HelloWeb extends HttpServlet {

}

4) Rewrite the service method

Satisfying the Servlet specification only allows our class to meet the requirements of receiving requests. After receiving the request, it needs to analyze the request and perform business logic processing. To calculate the result, you need to add code. There is a method called service in the specification. Specifically used for request processing operations, business code can be written in this method.

package com.xxx.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWeb extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("I received a request");
        resp.getWriter().write("<h1>Hello World!</h1>");
    }

}

5) Configure web.xml

After finishing all the code writing, you also need to explain to the server that a specific request corresponds to a specific resource, which is done through a configuration file called web.xml.

<!-- 配置servlet -->
<servlet>
  <servlet-name>helloweb</servlet-name><!-- 给服务器看的 -->
  <servlet-class>com.xxx.web.HelloWeb</servlet-class><!-- servlet对应的资源路径 -->
</servlet>
<servlet-mapping>
  <servlet-name>helloweb</servlet-name><!-- 给服务器看的 -->
  <url-pattern>/helloweb</url-pattern><!-- 给浏览器看的,对外访问路径 -->
</servlet-mapping>

Will you implement it through the Servlet accessed by the website?

6) Publish the project

At this point, the areas that need to be written and configured have been completed, and the project is complete, but if you need to be able to access it from the outside world, you also need to publish the project to the server and run the server.

Will you implement it through the Servlet accessed by the website?

Will you implement it through the Servlet accessed by the website?

Will you implement it through the Servlet accessed by the website?

7) Start the project

Will you implement it through the Servlet accessed by the website?

Seeing a long list of messages like this indicates that the startup is successful, and then you can access the project

Will you implement it through the Servlet accessed by the website?

8) Visit and view the results

After the project is correctly published on the server, users can access the resources in the project through a browser. Note that the format of the url is correct, and the port of tomcat is 8080. < http://localhost:8080/hw/helloweb>, page result

Will you implement it through the Servlet accessed by the website?

Background results

Will you implement it through the Servlet accessed by the website?

At this point, our first Servlet is implemented!

Expand

working principle

Will you implement it through the Servlet accessed by the website?

Implementation process

The client sends a request according to the configuration of the web.xml file, finds the corresponding url-pattern, reads the value, finds the corresponding servlet-name, finds the specified class, loads and executes the class, and returns the result to the Web server to respond to the result Client

Will you implement it through the Servlet accessed by the website?

Guess you like

Origin blog.51cto.com/15047271/2572124