Community
Participate
Working Groups
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
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.
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.
Larry, this is already resolved, but in case you can provide some more insights.
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.