wood burning stoves 2.0*
The moose likes Other JSE/JEE APIs and the fly likes JAX-RS/GetinitparameterNames() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "JAX-RS/GetinitparameterNames()" Watch "JAX-RS/GetinitparameterNames()" New topic
Author

JAX-RS/GetinitparameterNames()

Pascal Jak
Greenhorn

Joined: Jan 01, 2013
Posts: 2
Hi there

I am writing a small test program w/JAX-RS in order to familiarize.
The goal is just to display a number of init parameters as set in the web.xml file.

The problem is that I just cannot make a call to GetinitparameterNames(). Probably, this is because I just do not understant the "Context" annotation. Can someone enlight me ?

Thanks in adv.
P

My test class (cnt1, cnt2 displays as zero) :

package com.tcs;

import java.util.Enumeration;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Context;

@Path("/config")
public class Params {

private String str1, str2;
private int cnt1 = 0, cnt2 = 0;

public void init(@Context ServletConfig cfg) {
Enumeration e = cfg.getInitParameterNames();
while (e.hasMoreElements()) cnt1++;
}
@GET
@Produces(MediaType.TEXT_HTML)
public String displayParams(@Context ServletContext ctxt) {
str2 = ctxt.toString();
Enumeration e = ctxt.getInitParameterNames();
while (e.hasMoreElements()) cnt2++;
return "<html> " +
"<head><title>" + "GET Params" + "</title></head>" +
"<body>" +
"<h1>" + "Web App parms" + "</h1>" +
"<p> Cfg :" + str1 + "( Parms : " + cnt1 + ")</p>" +
"<p> Ctxt :" + str2 + "( Parms : " + cnt2 + ")</p>" +
"</body>" +
"</html> ";

} // displayParams
}


web.xml :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>RbacMgr</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>RbacMgr</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.tcs</param-value>
</init-param>
<init-param>
<param-name>com.thalesgroup.tcs.hostname</param-name>
<param-value>localhost</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>RbacMgr</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41178
    
  45
I'm not sure if it's supposed to work with all methods (like init). Have you tried using an instance variable like this:

@Context ServletConfig conf;

?



Ping & DNS - my free Android networking tools app
Pascal Jak
Greenhorn

Joined: Jan 01, 2013
Posts: 2
Ulf Dittmer wrote:I'm not sure if it's supposed to work with all methods (like init). Have you tried using an instance variable like this:

@Context ServletConfig conf;

?




Ulf

Not sure I understand you. If you mean to use ServletConfig at the GET stage, it does not work either.
Thanks anyway
P
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41178
    
  45
No, I don't mean as part of any method call - I mean an instance variable, like I wrote it.

Looking at the web.xml, it doesn't contain context parameters, so it's not surprising that cnt2 is 0.

As to using ServletConfig, it should be used to supply parameters to the JAX-RS engine, not to your code, so I would advise against using that in any case.
 
Don't get me started about those stupid light bulbs.
 
subject: JAX-RS/GetinitparameterNames()
 
Similar Threads
Activating page in another panel -- almost working but puzzled
getting error when deploying web app in weblogic 10.0
how to set domain namin on jboss as 4.0.5
[solved]Struts 2 and Vaadin
Using Vaadin and Struts 1 together