(十)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。

相关推荐
customer081 分钟前
【开源免费】基于SpringBoot+Vue.JS医疗报销系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
B站计算机毕业设计超人5 分钟前
计算机毕业设计SpringBoot+Vue.jst房屋租赁系统(源码+LW文档+PPT+讲解)
vue.js·spring boot·后端·eclipse·intellij-idea·mybatis·课程设计
barcke10 分钟前
【深度解析】Java接入DeepSeek大模型:从零实现流式对话+多轮会话管理(完整项目实战) —— SpringBoot整合、API安全封装、性能优化全攻略
java·spring boot
zl97989922 分钟前
MybatisPlus-注解
java·spring·maven
thinkMoreAndDoMore29 分钟前
深度学习(3)-TensorFlow入门(常数张量和变量)
开发语言·人工智能·python
杰九32 分钟前
【环境配置】maven,mysql,node.js,vue的快速配置与上手
java·vue.js·spring boot·mysql·node.js·maven
wapicn9943 分钟前
‌挖数据平台对接DeepSeek推出一键云端部署功能:API接口驱动金融、汽车等行业智能化升级
java·人工智能·python·金融·汽车·php
蓝桉8021 小时前
图片爬取案例
开发语言·数据库·python
逸狼1 小时前
【JavaEE进阶】Spring DI
java·开发语言
m0_748248651 小时前
SpringBoot整合easy-es
spring boot·后端·elasticsearch