What is a servlet? The power node servlet tutorial takes you to a quick introduction to actual combat

Keywords: Java Back-end Programmer servlet

A component specification (server-side Java application) developed by sun company to expand the functions of web server. It is independent of platform and protocol, and can generate dynamic web pages. It acts as an intermediate layer between client requests (web browsers or other HTTP clients) and server responses (databases or applications on HTTP servers)

1. Used to extend web server functions (generate dynamic web pages)

The previous CGI was not efficient, so servlet was used to extend and replace CGI

The client sends a request to the web server, which starts and invokes the servlet. The servlet container is responsible for parsing data packets, including some processing related to network communication. The parsed data is then handed over to the servlet for processing.

Note: you can send requests directly to the servlet container without using the server. Because the servlet container also has a communication module, you can directly use the servlet container as a web server.

2. Component specification

(1) Component: a software module that conforms to certain specifications and realizes some functions and can be deployed separately. Components must be deployed into containers to run.

(2) Container: it is also a program that conforms to certain specifications and provides the running environment of components.

Note: a single component and a single container are meaningless. It can only run together

Tomcat is a servlet container and a web server

3. How the servlet container works:

The browser box web server sends the request web server receives the data to send the servlet container, then instantiation servlet calls the init method initialization, then calls the service method to use doget or dopost processing.

The servlet returns the processed structure to the web server and then sends it to the client

The advantage of servlets is that they create only one instance, initialize only once, but can be called multiple times. Different GCI s create multiple instances, and servlets have thread safety problems. servlet is quite efficient

servlet creation

public class servletest extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public servletest() {
        super();
    }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         response.setContentType("text/html");
                  PrintWriter out = response.getWriter();
                  out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
                  out.println("<HTML>");
                  out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
                  out.println("  <BODY>");
                  out.print("    This is ");
                 out.print(this.getClass());
                  out.println(", using the GET method");
                  out.println("  </BODY>");
                  out.println("</HTML>");
                  out.flush();
                  out.close();
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          response.setContentType("text/html");
                   PrintWriter out = response.getWriter();
                   out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
                   out.println("<HTML>");
                   out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
                   out.println("  <BODY>");
                  out.print("    This is ");
                   out.print(this.getClass());
                   out.println(", using the POST method");
                   out.println("  </BODY>");
                   out.println("</HTML>");
                  out.flush();
                   out.close();
    }
 
}

Configuration of web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>zz.servletest</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/test/hello</url-pattern>
  </servlet-mapping>
</web-app>

Start Tomcat input:

localhost8080/zzservlet/test/hello

be careful:

In web.xml, a servlet is a thread of a servlet instance. There can be multiple servlet tags, and each servlet represents a request processing

servletname is the name of the servlet. It can be any value, but it must correspond to that in servlet mapping

Servlet class is the corresponding Java class, that is, the Java directory under the root directory, that is, the project name is not required, the project name is the root directory, and class is the file name, that is, the name after package

URL pattern is the access path. The input access address is web address + port number + project name + access path

The difference between servlet and jsp is that servlet needs out.print one by one, while jsp does not need to output sentence by sentence. There is no difference between others

How do servlets get started quickly?

The servlet tutorial of the power node can be said to be the most detailed about servlet development. Servlets are the basis of all Java Web development. Through the study of servlets, we can observe the whole process of interaction between the most elementary browser and server. In the video, we will develop all relevant knowledge points for servlets, such as get and post processing, response mode, forwarding and redirection, Make the most in-depth explanation of relevant technologies such as context.

Finally, a comprehensive case of login operation and displaying student information list will be combined to make the most complete summary of servlet development.

Servlet online viewing:

https://www.bilibili.com/vide...

Servlet learning materials Download:

http://www.bjpowernode.com/?s...

Posted by jkraft10 on Wed, 17 Nov 2021 23:23:57 -0800