gpt4 book ai didi

java - 如何在JSP中访问web.xml初始化参数

转载 作者:太空宇宙 更新时间:2023-11-04 13:49:49 25 4
gpt4 key购买 nike

我已在 web.xml 中声明了我的 init 参数,并且在 init.jsp 中访问相同的参数时,我得到的输出为 null。我正在使用 Tomcat-8

我的web.xml是:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>SecondWebProject</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>InitJsp</servlet-name>
<jsp-file>/init.jsp</jsp-file>
<init-param>
<param-name>default_username</param-name>
<param-value>default user</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>InitJsp</servlet-name>
<url-pattern>/init.jsp</url-pattern>
</servlet-mapping>
</web-app>

我的 init.jsp 是:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Init jsp</title>
</head>
<body>
Default username is: <%= config.getInitParameter("default_username") %>
<br>
Default username is: <%=
getServletConfig().getInitParameter("default_username") %>
</body>
</html>

Eclipse中的文件结构如下: file

我找不到我哪里做错了。您这边的任何帮助都是有值(value)的..

最佳答案

请确保将 jsp 放在正确的位置/路径。
我测试了你的代码(使用tomcat7)并且运行良好,只有当我将jsp放在正确的路径(web.xml

中提到的路径时)

检查快照作为引用,如果将jsp直接放在WebContent/init.jsp下,它将运行正常,否则结果为null(检查快照中的url,结果为空) enter image description here

检查目录结构 WebContent/init.jsp Correct path enter image description here

关于java - 如何在JSP中访问web.xml初始化参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30420715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com