(十)JSP教程——config对象

config对象是脚本程序配置对象,表示当前JSP页面的配置信息。由于JSP页面通常无需配置,因此该对象在JSP页面中比较少见。

config对象可以读取一些初始化参数的值,而这些参数一般在web.xml配置文件中可以看到,并通过config对象的相应方法来读取参数。

首先创建一个config.jsp页面,页面的代码如下:

java 复制代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>

<head>

<title>config对象实例</title>

</head>

<body>

<!--输出当前Servlet的名称-->

<center>

<font color="red">

<%=config.getServletName()%>

</font>

</center>

<!--输出配置参数-->

姓名:<%=config.getInitParameter("name")%>

年龄:<%=config.getInitParameter("age")%>

学号:<%=config.getInitParameter("number")%>

</body>

</html>

然后在Web项目下找到web.xml配置文件,配置文件位置如下图所示

该配置文件内容初始化时显示如下图所示

然后加入需要的配置

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">

    <welcome-file-list>

        <welcome-file>index.jsp</welcome-file>

    </welcome-file-list>

    <servlet>

        <servlet-name>Information</servlet-name>

        <jsp-file>/config.jsp</jsp-file>

        <init-param>

            <param-name>name</param-name>

            <param-value>Jack</param-value>

        </init-param>

    </servlet>

    <servlet-mapping>

        <servlet-name>Information</servlet-name>

        <url-pattern>/config.jsp</url-pattern>

    </servlet-mapping>

</web-app>

该配置文件中配置了一个对应的参数,并对其进行赋值,最后将该Servlet映射到/index.jsp处。

启动该应用后,在浏览器地址栏中输入:http://localhost:8080/s001/config.jsp

可以看到在Web.xml中没有设置的话,该初始参数的值就为null。

相关推荐
趣知岛20 分钟前
初识Java
java·开发语言
步菲2 小时前
springboot canche 无法避免Null key错误, Null key returned for cache operation
java·开发语言·spring boot
毕设源码-朱学姐2 小时前
【开题答辩全过程】以 基于SpringBoot的中医理疗就诊系统为例,包含答辩的问题和答案
java·spring boot·后端
2201_757830876 小时前
全局异常处理器
java
上进小菜猪7 小时前
从人工目检到 AI 质检-YOLOv8 驱动的 PCB 缺陷检测系统【完整源码】
后端
知远同学7 小时前
Anaconda的安装使用(为python管理虚拟环境)
开发语言·python
小徐Chao努力7 小时前
【Langchain4j-Java AI开发】09-Agent智能体工作流
java·开发语言·人工智能
CoderCodingNo8 小时前
【GESP】C++五级真题(贪心和剪枝思想) luogu-B3930 [GESP202312 五级] 烹饪问题
开发语言·c++·剪枝
Coder_Boy_8 小时前
SpringAI与LangChain4j的智能应用-(理论篇3)
java·人工智能·spring boot·langchain
kylezhao20198 小时前
第1章:第一节 开发环境搭建(工控场景最优配置)
开发语言·c#