8

【笔记】Servlet快速入门

 1 year ago
source link: https://feiju12138.github.io/2022/09/29/Servlet%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Servlet快速入门

  • 创建Web项目,引入Servlet依赖

pom.xml

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>

创建一个类

  • 创建一个类,实现servlet接口,当接收到请求时,会自动执行service()方法

@WebServlet(""):定义访问路径

package com;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;

@WebServlet("/test")
public class MyServlet implements Servlet {

public void init(ServletConfig servletConfig) throws ServletException {

}

public ServletConfig getServletConfig() {
return null;
}

public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("test");
}

public String getServletInfo() {
return null;
}

public void destroy() {

}
}

哔哩哔哩——黑马程序员


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK