Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 339836 - Can't access web.xml init-param values when running servlet.
Summary: Can't access web.xml init-param values when running servlet.
Status: RESOLVED INVALID
Alias: None
Product: WTP ServerTools
Classification: WebTools
Component: jst.server (show other bugs)
Version: unspecified   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Larry Isaacs CLA
QA Contact: Angel Vera CLA
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-03-14 00:12 EDT by Chris Back CLA
Modified: 2011-03-16 09:14 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Chris Back CLA 2011-03-14 00:12:23 EDT
Build Identifier: 20110218-0911

I am moving a large web application project so it runs within Eclipse to make development more streamlined. Unfortunately, when I run the application through Eclipse (I added the necessary server and associated it with the project) I don't seem to have access to any of the init-params I have set up in the web.xml.

The servlet runs, I can output data to the response, etc. The web.xml is obviously being used, the servlet is loaded and mapped to the correct url.

Here is the web.xml from my trivial test case:

<?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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Sample</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  <servlet>
    <description>Test servlet.</description>
    <display-name>TestServlet</display-name>
    <servlet-name>TestServlet</servlet-name>
    <servlet-class>com.owneriq.Test</servlet-class>
    <init-param>
      <description>Sample init param.</description>
      <param-name>sample-param</param-name>
      <param-value>foo</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>TestServlet</servlet-name>
    <url-pattern>/test</url-pattern>
  </servlet-mapping>
</web-app>

And here is the servlet code (com/owneriq/TestServlet.java):

package com.owneriq;
import java.io.IOException;

public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private String sampleParam;

	public TestServlet() {
		super();
	}

	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);

		sampleParam = config.getServletContext().getInitParameter(
				"sample-param");
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/plain");
		ServletOutputStream output = response.getOutputStream();
		output.println("working");
		output.println("sampleParam:" + sample-param);
		output.close();

	}
}


In the output sampleParam is always null.
This is probably something extremely trivial, but I have been beating my head against the wall trying different versions of Eclipse (3.4 and 3.6) and tomcat.

Thanks,

Chris

Reproducible: Always
Comment 1 Chris Back CLA 2011-03-14 00:45:26 EDT
I made a mistake in posting the java code... the line

        output.println("sampleParam:" + sample-param);

should read

        output.println("sampleParam:" + sampleParam);

The "bug" still exists, I just made the mistake when copying sample code to the bug report.
Comment 2 Chris Back CLA 2011-03-14 16:31:34 EDT
Sort of resolved. If I put the parameter in the context.xml inside the META-INF directory things work as expected. It seems I should close this ticket and instead pursue the difference between the places in the servlet spec.
Comment 3 Angel Vera CLA 2011-03-15 10:45:00 EDT
Larry, this is already resolved, but in case you can provide some more insights.
Comment 4 Larry Isaacs CLA 2011-03-16 09:14:20 EDT
I believe you want ServletConfig.getInitParameter() or Servlet.getInitParameter() to read the init-parameter local to a servlet.  Calling ServletConfig.getServletContext().getInitParameter() gets "context wide" init parameters, i.e. <context-param> parameters.  You would think they would call it getContextParameter(), but they didn't.  Feel free to post a question on the Tomcat Users mailing list or the Web Tools newsgroup before reporting such "mysteries" as a bug. :)

Changing the resolution to INVALID since nothing in WTP needed fixing.