1.创建maven项目
2.在src/main 目录下建立webapp/WEB-INF/web.xml文件
src/main/webapp/WEB-INF/web.xml
<?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">
<display-name>untitled</display-name>
<!-- 配置web项目的web.xml文件的首页 -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
3.编辑修改maven项目的pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.mingzi</groupId>
<artifactId>web99</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
</project>
4.idea配置tomcat部署
添加tomcat部署
5.在webapp目录下,建立index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
\${pageContext.request} = ${pageContext.request} |取得请求对象<br>
\${pageContext.session} = ${pageContext.session} |取得session对象<br>
\${pageContext.request.queryString} = ${pageContext.request.queryString} |取得请求的参
数字符串<br>
\${pageContext.request.requestURL} = ${pageContext.request.requestURL} |取得请求的URL
的服务器路径,但不包括请求之参数字符串<br>
\${pageContext.request.contextPath} = ${pageContext.request.contextPath} |服务的web
application的名称<br>
\${pageContext.request.method} = ${pageContext.request.method} |取得HTTP的方法(GET、
POST)<br>
\${pageContext.request.protocol} = ${pageContext.request.protocol} |取得使用的协议
(HTTP/1.1、HTTP/1.0)<br>
\${pageContext.request.remoteUser} = ${pageContext.request.remoteUser} |取得用户名称
<br>
\${pageContext.session['new']} = ${pageContext.session['new']} |判断session是否为新的,
所谓新的session,表示刚由server产生而client尚未使用<br>
\${pageContext.session.id} = ${pageContext.session.id} |取得session的ID<br>
\${header["User-Agent"]} = ${header["User-Agent"]}|用户浏览器的版本<br>
\${header["Host"]} = ${header["Host"]}|IP<br/>
\${pageContext.request.remoteAddr} = ${pageContext.request.remoteAddr} |取得用户的IP地
址<br>
\${pageContext.request.localAddr} = ${pageContext.request.localAddr} |取得服务器IP地址
<br>
\${pageContext.servletContext.serverInfo} =
${pageContext.servletContext.serverInfo}|取得主机端的服务信息<br>
\${pageContext.request.serverPort} = ${pageContext.request.serverPort}|端口信息<br>
\${pageContext.request.serverName} = ${pageContext.request.serverName}|服务器名称<br>
\${pageContext.request.remoteHost} = ${pageContext.request.remoteHost}|客户机名称<br>
</body>
</html>
debug模式启动服务器
后端接口项目,可以不建立webapp WEB-INF/ web.xml文件,可以直接编写servlet类
执行效果: