Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlets

 
ankita balaji
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
Good Afternoon.Iam new to servlet world.I have problem in passing parameters in web.xml.My program is:
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>");
}
}

my web.xml content looks like this:
<?xml version="1.0" encoding="ISO-8859-1"?>

<!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>
After running my servlet through url:http:\\localhost:8080\servlet\HelloWorldServlet i got the output as
- <html>
- <body>
<h1>Hello World!</h1>
getRequestURI: /servlet/HelloWorldServlet getServletPath: /servlet/HelloWorldServlet getPathInfo: null getConextPath:
</body>
rate = null
</html>


can anyone please help me???Iam getting null value for my parameter.Is there anything wrong in the way iam running the program?Please suggest me.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Is the culprit. That call goes through the invoker servlet - the invoker servlet does not pay any attention to your specific web.xml entry.
See the JavaRanch FAQ on the invoker.
Bill
 
Ajosh Gopi
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ankit,

Try running your application after making a small change in the XML

<?xml version="1.0" encoding="ISO-8859-1"?>

<!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>/Hello</url-pattern> {This is the URl With Which u will be calling the servlet.}
</servlet-mapping>

you can call the servlet in this way ::

http:\\localhost:8080\{ContextName}\Hello


Hope this solves your Query
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic