Do you know how to implement a servlet that can be accessed through a website?

Do you know how to implement a servlet that can be accessed through a website?

emsp; 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 the HTTP protocol, and it can handle all content related to the 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 and so on.

Servlet implementation

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

Case practice

1) Create a dynamic web project

2) New class


public class HelloWeb {


3) Implement the 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.


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 the request. 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. Specially used for request processing operations, business code can be written in this method.



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

public class HelloWeb extends HttpServlet {

	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, and this is done through a configuration file called web.xml.

<!--  servlet -->
  <servlet-name>helloweb</servlet-name><!--   -->
  <servlet-class></servlet-class><!-- servlet  -->
  <servlet-name>helloweb</servlet-name><!--   -->
  <url-pattern>/helloweb</url-pattern><!--   -->

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.

7) Start the project

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

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

Background results

At this point, our first Servlet has been implemented!


working principle

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