You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在JSP中获取page info?getServletInfo报错及Maven依赖问题

问题解决:JSP获取page info报错及正确实现方式

问题描述

我需要获取并打印JSP页面的page info(内容为Copyright 2023 by sean),按照教材指导在<body>中加入<%=this.getServletInfo()%>后,出现错误:Cannot resolve method 'getServletInfo' in 'JspClass'。使用IntelliJ Ultimate和Java Maven项目,怀疑缺少依赖;另外尝试${pageContext.request.servletPath}仅输出路径,无法获取page info。

当前JSP代码:

<%@ page info="Copyright 2023 by sean" contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>directive1.jsp</title>
    <h1>Directive Example1</h1>
</head>
<body>
<%=this.getServletInfo()%>
</body>
</html>

当前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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demojsp</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>demojsp</name>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.target>11</maven.compiler.target>
        <maven.compiler.source>11</maven.compiler.source>
        <junit.version>5.9.1</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>jakarta.servlet</groupId>
            <artifactId>jakarta.servlet-api</artifactId>
            <version>5.0.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>jakarta.servlet.jsp</groupId>
            <artifactId>jakarta.servlet.jsp-api</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>jakarta.servlet.jsp.jstl</groupId>
            <artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
            <version>2.0.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.2</version>
            </plugin>
        </plugins>
    </build>
</project>

解决方案

1. 错误原因

你使用的是Jakarta EE 9+版本(Servlet 5.0、JSP 3.1),该版本中JSP编译生成的类不再直接继承HttpServlet,因此this.getServletInfo()无法调用。你的pom依赖已经足够,不需要新增依赖,只需修改JSP代码即可。

2. 正确获取page info的两种方式

方式一:使用JSP内置对象page

将原代码中的<%=this.getServletInfo()%>替换为:

<%=page.getServletInfo()%>

page是JSP的内置对象,代表当前JSP实例,在Jakarta EE环境下支持getServletInfo()方法。

方式二:使用EL表达式(推荐)

更简洁的写法,符合现代JSP规范:

${page.servletInfo}

3. pom依赖优化(可选)

将jakarta.servlet.jsp-api的scope设为provided,因为Web容器(如Tomcat 10+)会自带该API,避免打包时冲突:

<dependency>
    <groupId>jakarta.servlet.jsp</groupId>
    <artifactId>jakarta.servlet.jsp-api</artifactId>
    <version>3.1.1</version>
    <scope>provided</scope>
</dependency>

修改后的完整JSP代码

<%@ page info="Copyright 2023 by sean" contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>directive1.jsp</title>
    <h1>Directive Example1</h1>
</head>
<body>
<%-- 方式1:使用page内置对象 --%>
<%=page.getServletInfo()%>

<%-- 方式2:使用EL表达式(推荐) --%>
${page.servletInfo}
</body>
</html>

内容的提问来源于stack exchange,提问作者beingicy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 02:02:11