• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using getInitParameter( ) method

 
Davie Zhang
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I wrote a simple servlet and wanted to test out servlet init as below:
package com;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;


public class HelloWorldServlet extends HttpServlet
{
private String rate;

public void init() {
ServletConfig config = getServletConfig();
rate = config.getInitParameter("RATE1");
}

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException
{
response.setContentType("text/xml");
PrintWriter out = response.getWriter();

out.println("<html>");
out.println("<body>");
out.println("<h1>Hello World!</h1>");
out.println(" getRequestURI: " + request.getRequestURI());
out.println(" getServletPath: " + request.getServletPath());
out.println(" getPathInfo: " + request.getPathInfo());
out.println(" getConextPath: " + request.getContextPath());

out.println("</body>");
out.println("rate = ");
out.println(rate);

out.println("</html>");
}
}

--------------------------------------------------------
...used a web.xml file containing exactly the same info:

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Test Servlet</display-name>
<description>
This is a simple test servlet.
</description>
<servlet>
<servlet-name>Hello World</servlet-name>
<servlet-class>com.HelloWorldServlet</servlet-class>
<init-param>
<param-name>RATE1</param-name>
<param-value>0.65</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Hello World</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
--------------------------------------------------------------------
...ran in Tomcat 4.1, using this url: http://localhost:8080/jspbook/

I got back the servlet's init parameter value (displayed as 0.65):
- <html>
- <body>
<h1>Hello World!</h1>
getRequestURI: /jspbook/ getServletPath: getPathInfo: / getConextPath: /jspbook
</body>
first = 0.65
</html>

But if I used the invoker mechanism
with this url: http://localhost:8080/jspbook/servlet/com.HelloWorldServlet

I didn't get the init parameter value (displayed below as null)

- <html>
- <body>
<h1>Hello World!</h1>
getRequestURI: /jspbook/servlet/com.HelloWorldServlet getServletPath: /servlet/com.HelloWorldServlet getPathInfo: null getConextPath: /jspbook
</body>
first = null
</html>

Does anyone know why?? Or was there something I did wrong.

Thanks,

dz
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65516
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Point #1: Don't use the invoker, it's evil.

Point #2: When you use the invoker, there's no way it can match the servlet up against the definitions in web.xml (there can be multiple entries for the same servlet class -- which would it choose?). So, no init params.

Point #3: Don't use the invoker, it's evil.

Point #4: See points 1 and 3.
 
Davie Zhang
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for clarifying this up Bear!!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65516
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read this for more information.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!