如何从泽西岛的Web.xml获取配置参数?
我一直在尝试从我的jakarta jax-rs资源中的web.xml
文件中获取参数。我的web.xml如下:
<web-app>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>terrible.package.name</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/api/v1/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>max-customers-size</param-name>
<param-value>10</param-value>
</context-param>
</web-app>
在我的测试设置中,我正在使用Jetty运行我的Web应用程序:
public static void main(String[] args) throws Exception {
Server server = new Server(8080);
WebAppContext ctx = new WebAppContext("target/the-finished-servlet.war", "/");
server.setHandler(ctx);
server.start();
}
并且我使用jakarta定义了以下用户资源:
@Path("user")
public class UserResource {
ServletContext context;
@Context
public void setServletContext(ServletContext context) {
this.context = context;
System.out.println("setting to: " + context);
}
@GET
@Path("/list")
@Produces(MediaType.APPLICATION_JSON)
public Response getListOfAllUsers(final @Context HttpHeaders hdrs) throws SQLException {
System.out.println(context.getInitParameter("max-customers-size"));
return Response.ok("{}").build();
}
}
上下文始终是null
感觉。 setServletContext
方法被调用,并且参数为null
。我已经尝试在构造函数参数上使用@context
,我已经在上下文字段上尝试过它,并且在getListofallusers
方法中尝试了它。他们都没有工作。
我只想配置我的servlet(对于MySQL数据库路径和用户,在此示例中,我正在使用max-customers-size-size
进行测试,这并不重要,因为我的上下文是null
无论哪种方式)。是否有一种更简单的方法将自定义配置数据输入我的servlet?我希望我生成的war
文件的用户能够提供数据库端口之类的内容。 ServletContext为什么不起作用,如果有一种使我的Servlet可配置的更简单的方法,我该怎么做?
I have been trying to get parameters from my web.xml
file from within my Jakarta JAX-RS resource. My web.xml is as follows:
<web-app>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>terrible.package.name</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/api/v1/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>max-customers-size</param-name>
<param-value>10</param-value>
</context-param>
</web-app>
In my test setup, I am running my web app with Jetty:
public static void main(String[] args) throws Exception {
Server server = new Server(8080);
WebAppContext ctx = new WebAppContext("target/the-finished-servlet.war", "/");
server.setHandler(ctx);
server.start();
}
And I have the following user resource defined using jakarta:
@Path("user")
public class UserResource {
ServletContext context;
@Context
public void setServletContext(ServletContext context) {
this.context = context;
System.out.println("setting to: " + context);
}
@GET
@Path("/list")
@Produces(MediaType.APPLICATION_JSON)
public Response getListOfAllUsers(final @Context HttpHeaders hdrs) throws SQLException {
System.out.println(context.getInitParameter("max-customers-size"));
return Response.ok("{}").build();
}
}
The context is always null
, which makes absolutely no sense. The setServletContext
method is called, and the parameter is null
. I've tried using @Context
on a constructor parameter, I've tried it on my context field, and I've tried it in the getListOfAllUsers
method. None of them work.
I just want to configure my servlet (for MySQL database path and user, in this example I'm testing with max-customers-size
, which doesn't matter though, because my context is null
either way). Is there an easier way to get custom config data into my servlet? I want the user of my resulting war
file to be able to supply things like the database port. Why isn't ServletContext working, and if there is an easier way to make my servlet configurable, how do I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常,通过jndi配置了用于Servlet的数据库(数据库)(服务器JNDI上下文中存在定义,WebApp JNDI上下文包含对服务器级别位置的硬编码引用)
您的服务器级别通常具有定义<代码的XML > dataSource 作为
org.eclipse.jetty.plus.jndi.resource
在服务器级别上下文上。以及您的WebApp的
Web-Inf/web.xml
包含&lt; resource-ref&gt;
回到该服务器级别的位置。然后,您的代码在JNDI上使用
initialContext
访问此已定义的dataSource
。这具有额外的奖励,即数据源可以是任何JDBC兼容实现,甚至包括在JDBC DataSource前面的连接池实现(例如:C3P0)之类的复杂配置。
对于单元测试,您将像这样设置它...
jndi lookup失败嵌入式码头服务器
用于Standapp的标准WebApp部署码头容器,您会这样设置...
>
Typically a database (DataSource) for Servlet is configured via JNDI (the definition exists on the server JNDI context, and the webapp JNDI context contains a hardcoded reference to the server level location)
Your server level will typically have an XML that defines the
DataSource
as aorg.eclipse.jetty.plus.jndi.Resource
against the server level context.And your webapp's
WEB-INF/web.xml
contains the<resource-ref>
back to that server level location.Your code then uses the
InitialContext
on JNDI to access this already definedDataSource
.This has the added bonus that the datasource can be any JDBC compatible implementation, and even include complex configurations like a Connection pool implementation (eg: c3p0) in front of your JDBC DataSource.
For unit testing, you'll set it up like this ...
JNDI Lookup Failing For Embedded Jetty Server
For standard webapp deployment in a standalone Jetty container, you'll set it up like this ...
Declaring JNDI Resources