emsp; Servlet is short for Server Applet, which stands for server-side applets. The main function of server-side program written in Java language is to browse and generate data interactively and generate dynamic Web content. Servlet mainly runs on the server side and is executed by the server. It is a class developed in accordance with the Servlet standard. Is a technology provided by SUN corporation for developing dynamic Web resources. (Translation: To implement Web development, you need to implement the Servlet standard.)

Servlets are essentially Java classes, but they are written in accordance with the Servlet specification. There is no main() method, and their creation, use, and destruction are managed by the Servlet container (such as Tomcat). Write your own class, don’t write main method, others automatically call)

Servlets are closely related to the HTTP protocol and can handle all content related to the HTTP protocol. This is one of the reasons servlets are so popular.

Servers that provide Servlet functionality are called Servlet containers, and there are many common containers, such as Tomcat, Jetty, WebLogic Server, WebSphere, JBoss, and so on.

Servlet implementation

  • Create a dynamic Web project
  • The new class
  • Implementing the Servlet specification
  • Override the service method
  • Configure web. XML
  • Publish the project
  • Start the project
  • Access and view the results

A case in field

1) Create dynamic Web projects

2) Create a class

package com.xxx.web;

public class HelloWeb {}Copy the code

3) Implement the Servlet specification

Implement Servlet specification, namely inherit HttpServlet class, and import the corresponding package, this class has completed the rules of communication, we only need to carry out business implementation.

package com.xxx.web;

import javax.servlet.http.HttpServlet;

public class HelloWeb extends HttpServlet {}Copy the code

4) Override the service method

The Servlet specification is just a way for our class to be able to receive the request. After receiving the request, we need to analyze the request, process the business logic, and calculate the result. Then we need to add code. 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("

Hello World!

"
); }}Copy the code

5) Configure web.xml

Once you’ve written all the code, you need to explain to the server that a particular request corresponds to a particular resource, in this case through a configuration file called web.xml.

<! -- Config servlet -->
<servlet>
  <servlet-name>helloweb</servlet-name><! -- Show the server -->
  <servlet-class>com.xxx.web.HelloWeb</servlet-class><! -- Resource path for servlet -->
</servlet>
<servlet-mapping>
  <servlet-name>helloweb</servlet-name><! -- Show the server -->
  <url-pattern>/helloweb</url-pattern><! -- External access path for browser -->
</servlet-mapping>
Copy the code

6) Release projects

At this point, everything that needs to be written and configured is done, and the project is complete, but you still need to publish the project to the server and run it if you want it to be accessible to the outside world.

7) Start the project

Seeing something like a long list of messages indicates a successful startup, and you can access the project

8) Access and view the results

After the project is properly published to the server, the user can access the resources in the project through a browser. Note That the URL format is correct and the Tomcat port is 8080. http://localhost:8080/hw/helloweb, the results page

The background results

Here we have our first Servlet implemented!

extension

The working principle of

Implementation process

The client sends a request according to the configuration of the web. XML file, finds the corresponding URl-pattern, reads the value in it, finds the corresponding servlet-name, finds the specified class, loads and executes the class, and returns the result. The Web server sends the result to the client