Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JAX-RS/GetinitparameterNames()

 
Pascal Jak
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;

?


 
Pascal Jak
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic