gpt4 book ai didi

java - JBoss 上下文参数

转载 作者:行者123 更新时间:2023-11-28 23:30:01 30 4
gpt4 key购买 nike

我有一个简单的 Servlet,它“提供”一个简单的 XML 文件;它将从文件中动态加载。在 tomcat 中我使用了上下文参数,在 JBoss 中我该怎么做?

代码:

resp.setContentType("text/xml");
// String path = System.getProperty("AnagraficaXML");
String path = getServletContext().getInitParameter("AnagraficaXML");
// InputStream is = ClassLoader.getSystemResourceAsStream(XML_FILE);
File file = new File(path);
FileReader fileReader = new FileReader(file);
BufferedReader br = new BufferedReader(fileReader);
PrintWriter out = resp.getWriter();
String line = br.readLine();
while (line != null) {
out.append(line);
line = br.readLine();
}
br.close();
fileReader.close();

Tomcat 配置(文件 server.xml)

<Context docBase="AnagraficaCedacri" path="/AnagraficaCedacri"
reloadable="true" source="org.eclipse.jst.jee.server:AnagraficaCedacri">
<Parameter name="AnagraficaXML" value="D:\Dvlpmnts\Cedacri\CedacriServerResources\Anagrafica.xml"
override="false" />
</Context>

我会在这里找到一个可以在 Tomcat/Jboss 和 WebSpe 中工作的“可移植”解决方案!

最佳答案

您需要创建一个 Java 网络应用程序 (WAR) 并将其放在部署文件夹中。

@WebServlet("/AnagraficaCedacri")
public class HelloWorldServlet extends HttpServlet {

protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

resp.setContentType("text/xml");
// String path = System.getProperty("AnagraficaXML");
String path = getServletContext().getInitParameter("AnagraficaXML");
// InputStream is = ClassLoader.getSystemResourceAsStream(XML_FILE);
File file = new File(path);
FileReader fileReader = new FileReader(file);
BufferedReader br = new BufferedReader(fileReader);
PrintWriter out = resp.getWriter();
String line = br.readLine();
while (line != null) {
out.append(line);
line = br.readLine();
}
br.close();
fileReader.close();
}
}

更多信息请访问:http://hudson.jboss.org/jenkins/job/JBoss-AS7-Docs/lastSuccessfulBuild/artifact/guides/developer-getting-started-guide/target/docbook/publish/en-US/html/helloworld.html

希望对您有所帮助。奥利弗

关于java - JBoss 上下文参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31741361/

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