Introduction to Servlet-the first servlet program

servlet:

A servlet is actually a java class that implements a special interface, and this kind of tomcat server is known.

Features:

The servlet is created by the programmer and executed by the server.

use:

1. Create a Java class and inherit HttpServlet

2. Overwrite service method

3. Write logic code in the service method

4. Configure the servlet in the web.xml file under the WEB-INF folder under webRoot

to sum up:

Servlet is actually a Java class that the server can recognize. We need to put business rules into servlet

Then put the serrvlet into the tomcat server, and the server calls the corresponding serclet according to the request. Servlet can be called the entrance of background processing program

Servlet creation process:

1. Create a package (com.bjsxt.servlet)

2. Create a Java class that implements HttpServlet in the package

3. Override the service method and declare the request processing rules in the method

4. Configure the servlet in the web.xml file

<?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_2_5.xsd" id="WebApp_ID" version="2.5">
  <!-- 
  
  配置servlet 
   注意:
    一组servlet配置包括两个组成部分(servlet,servlet-mapping),两个组成部分的servlet-name值必须一致。
  
  -->>
  
  <servlet>
   <servlet-name>my</servlet-name>
   <servlet-class>com.bjsxt.servlet.MyServlet</servlet-class><!-- 书写servlet的全限定路径(包名+类名) -->
  </servlet>
  <servlet-mapping>
  <servlet-name>my</servlet-name> <!-- 与servlet-name保持一致 -->
  <url-pattern>/s</url-pattern><!-- 配置servlet的访问别名,可自定义 -->
  </servlet-mapping>
</web-app>

5. Copy all the content under webroot in the web project to the newly created folder in the webapps directory of the server.

6. Start the server localhost: 8080

Servlet running process analysis:

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44192389/article/details/103729294