我要读取tomcat context.xml的参数 [英] I want to read the parameters of tomcat context.xml
问题描述
我想获取 context.xml 中的 companyName 值编写下面提到的代码后,我得到 null
值
请帮助我从 context.xml
获取值.你甚至可以告诉从 context.xml
注意:不要说在web.xml
Context.xml (Tomcat 7)
<上下文><!-- 监控资源的默认集合--><WatchedResource>WEB-INF/web.xml</WatchedResource><!-- 取消注释以禁用跨 Tomcat 重启的会话持久性 --><!--<管理器路径名=""/>--><!-- 取消注释以启用 Comet 连接跟踪(提供事件会话到期以及 webapp 生命周期) --><!--<Valve className="org.apache.catalina.valves.CometConnectionManagerValve"/>--><Parameter name="companyName" value="My Company, Incorporated"覆盖=假"/>上下文>
JSP(index.jsp)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><头><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>在此处插入标题</title>头部><身体><%ServletContext sc= getServletContext();String testNameValue = sc.getInitParameter("companyName");%><input type="text" value="<%=testNameValue%>"></html>
输出
在实施下面给出的解决方案后更新
出现异常
I want to get companyName value which is in context.xml
I am getting null
value after writing the code mentioned below
Please help me in getting the value from context.xml
. You can even tell other way of getting value from context.xml
NOTE : Don't say to write param in web.xml
Context.xml (Tomcat 7)
<?xml version='1.0' encoding='utf-8'?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- The contents of this file will be loaded for each web application -->
<Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->
<Parameter name="companyName" value="My Company, Incorporated"
override="false"/>
</Context>
JSP(index.jsp)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
ServletContext sc= getServletContext();
String testNameValue = sc.getInitParameter("companyName");
%>
<input type="text" value="<%=testNameValue%>">
</body>
</html>
Output
Updated after implementing the solution given below
Exception comes
You cannot load that way because context.xml
is JNDI resource. Please try the following approach:
Tomcat (context.xml)
<Parameter name="companyName" value="My Company, Incorporated" override="false"/>
Java Side
InitialContext context = new InitialContext();
Context xmlNode = (Context) context.lookup("java:comp/env");
String companyName = (String) xmlNode.lookup("companyName");
Spring Side HomeController.java
@Controller
@RequestMapping("/")
public class HomeController {
@Autowired
private ServletContext servletContext;
@RequestMapping(method=RequestMethod.GET)
public ModelAndView index(ModelAndView mav) throws Exception {
String companyName = servletContext.getInitParameter("companyName");
mav.setViewName("home/index");
mav.addObject("companyName", companyName);
return mav;
}
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
}
View Side index.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<body>
<c:out value="${companyName}"/>
</body>
</html>
The above example has proven successful on my end. My script can read from context.xml
file at runtime as shown.
这篇关于我要读取tomcat context.xml的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!