• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JAX-RS/GetinitparameterNames()

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic