Servlet—————————————————————————————————————————————————
06尚硅谷_Servlet王振国 - 课堂笔记.pdf
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- servlet标签给tomcat配置servlet程序 -->
<servlet>
<!-- servlet-name标签 servlet程序起一个别名(一般是类名) -->
<servlet-name>hello</servlet-name>
<!-- servlet-class是servlet程序的全类名 -->
<servlet-class>com.example.demo2.hello</servlet-class>
</servlet>
<!-- servlet-mapping标签给servlet程序配置访问地址 -->
<servlet-mapping>
<!-- servlet-name标签的作用是告诉服务器,我当前配置的地址给哪个servlet程序使用 -->
<servlet-name>hello</servlet-name>
<!-- url-pattern标签配置访问地址
/ 斜杠在服务器解析的时候表示地址为http://ip:port/工程路径
/hello 表示地址http://ip:port/工程路径/hello
-->
<url-pattern>/hello</url-pattern>
</servlet-mapping>
运行结果
通过继承HttpServlet来实现servlet程序:
①
package com.example.demo2;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class HelloServlet2 extends HelloServlet{
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
System.out.println("doGet方法");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doPost方法");
}
}
②在web.xml中配置
<servlet>
<servlet-name>HelloServlet2</servlet-name>
<servlet-class>com.example.demo2.HelloServlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet2</servlet-name>
<url-pattern>/hello2</url-pattern>
</servlet-mapping>
③在网页中进行测试
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Servlet测试</title>
</head>
<body>
<form action="http://localhost:8080/demo2/hello2" method="get">
<input type="submit">
</form>
</body>
</html>
运行结果:
07尚硅谷_Servlet王振国 - 课堂笔记.pdf
每次请求都会封装新的request