views:

58

answers:

1

Hello All,

I ve created a sample REST web service which writes some data in a xml file. Now I have hard coded the path where the xml file is to be written. I want to know how to declare that local path of the file in web.xml file as servlet parameter and how to get the path from there and use it in codebe . Also I need to create the WAR file for the service which needs to deployed in tomcat. This war file should take that parameter from the web.xml file. I used eclipse IDE to develop the web service. Can anyone tell me how to do the above things ?

Here I have attached the servlet code present inside the web.xml file.

<servlet>
  <servlet-name>Jersey REST Service</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.sample.service</param-value>
  </init-param>
  <init-param>
    <param-name>filepath</param-name>
    <param-value>filepath value</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>Jersey REST Service</servlet-name>
  <url-pattern>/api/*</url-pattern>
</servlet-mapping>

com.sample.service is the package where I have my Rest web service class.

+1  A: 

Assuming you created this as a Dynamic Web project in Eclipse, just right-click on the

project name, > Export > WAR file

and fill in the details it asks for.

In your web.xml, you can define your filepath as below

<servlet>  
<servlet-name>MyServletName</servlet-name>  
<servlet-class>com.mycompany.MyServlet</servlet-class>  
<init-param>  
<param-name>filepath</param-name>  
<param-value>D:\hard-coded-path.xml</param-value>  
</init-param>  
</servlet> 

*Updated with correct answer as per comments *

You're getting the NullPointerException on getServletContext().getInitParameter("filepath") because the Context is not injected into the web service method.

And in your web service, use this code to get the path and write to it using the @Context annotation

 @GET
@Produces("text/plain")
public String doStuff(@Context ServletConfig sc) {



   String xmlpath = "Output filepath is: " + sc.getInitParameter("filepath");
    return xmlpath;
}

See here for usage and examples of @Context

JoseK
Hello JoseK. Thanks for the response. As you said, I have modified the web.xml and written the above code to get the path in the doGet method of HttpServlet class and created the WAR file. Can I know how to run this war file ? I am trying it in eclipse IDE ?
Senthil
You can run it from Eclipse directly. Right click on project > Run As > Run on Server. you have to configure the Tomcat server within Eclipse first via "Run configruations"
JoseK
Hello JoseK. Actually I am writing into the xml on server side ie. on the RESTful web service. I want to know how to access the filepath present in web.xml file within the REST web service class ie. inside PUT method of REST. But I cant able to access the ServletConfig object within the web service. Can you tell me how to do that ?
Senthil
I tried with both getServletConfig().getInitParameter("filepath") as well as getServletContext().getInitParameter("filepath") method to retrieve the filepath. I am getting the Null pointer exception in the GenericServlet class. Can I know whats the problem in it?
Senthil
Thank you JoseK for your response. Now I am not getting the exception. But still the filepath which is getting printed as null. I had given exactly the same name in web.xml as well as in getInitParameter("filepath") method.
Senthil
Hello JoseK. I tried changing the value of init-param and restarted the server as well and checked. Still I am getting the null as value. Do I need to make any other change other than this ?
Senthil
@JoseK. I ve attached web.xml portion above in the question part.
Senthil
@Senthil: da, this should work. do you get the other init-param com.sun.jersey.config.property.packages correctly? try one thing: change <servlet-name>Jersey REST Service</servlet-name> to JerseyRESTService, i.e. remove spaces though it might not make a difference I feel
JoseK
I am not getting both the parameters even if I remove the spaces from the servlet name.
Senthil
try as a plain servlet code or try as a war deployed on tomcat - i.e. outside of eclipse
JoseK
@JoseK. Can I know how to run the WAR outside the eclipse. I copied the WAR manually inside the WebApps directory of the tomcat6.6 server. Now Do I need to run the WAr in command line. If so what is the command to run. Can you list the steps to deploy the WAR and run it. Sorry to disturb you continuously. This is the first time I am trying this.
Senthil
put war in /webapps, start Tomcat from /bin/startup.bat or see more at http://tomcat.apache.org/tomcat-6.0-doc/manager-howto.html
JoseK
@Josek. I am able to get the filepath from web.xml. It is working for me if I get the param like this "sc.getInitParameter("filepath");". Thank You JoseK for your help.
Senthil
@Senthil: Glad it worked. I'll update the answer with that bit so it is complete.
JoseK