Java-maven建立javaweb项目,在main下建立三个文件夹,在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">
</web-app>
会发现wwbapp文件可以建立JSP文件,建立一个名为index的文件,
并在文件中添加如下代码块:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title> QQ:226104879 Email:acher24@126.com</title>
<meta name="keywords" content="关键字">
<meta name="description" content="简介">
</head>
<body>
<%
for(int i=1;i<10;i++){
out.print(String.format("<h1>hello %d</h1>",i));
}
%>
<h3><%=System.getProperty("java.home")%>
</h3>
<h3><%=System.getProperty("java.version")%>
</h3>
<h3><%=System.getProperty("os.name")%>
</h3>
<h3>${pageContext.servletContext.serverInfo}</h3>
<h3>${pageContext.request.contextPath}</h3>
<h3>${pageContext.request.method}</h3>
<h6>${header["User-Agent"]}</h6>
</body>
</html>
进行Tomcat配置
要进行包的导入,第一次运行的时候,需要配置包,可以去https://mvnrepository.com/中央仓库进行下载,我这里配置了三个包,并经行完整的配置。
<?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>org.example</groupId>
<artifactId>ms01</artifactId>
<version>1.0</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>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
</project>
第一次运行报错因为没有配置浏览器的路径,找到并把本地浏览器的路径进行输入
运行结果如图所示: