In trying to get to know the HttpClient component a bit, I attempted to write a simple 
program using a Java Servlet and the HttpClient API - the end result being that the 
client make a post to the servlet and the servlet return a brief HTML response to the 
client.  The next step would be to transfer a file between the two, however, I'm 
running into some trouble with this first step.

The only real requirements I've placed on this step is to use a post method instead of 
a get (because future steps would not be able to use a get), and to pass a parameter 
in the request.  The parameter is where my problems start.  After checking for this 
parameter on the server side, I throw an exception if it is non-existent.  This is as 
far as the program has been running for me.  

I'm using HTTP Client 2.0 Alpha 2, and Commons Logging 1.0.2.  I've included a couple 
of example classes that represent the basics of my code, and the thrown exception, in 
case anyone might have an idea as to what I am doing wrong:

_________________________________________________________________________________________________

CLIENT CLASS -


import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;

import java.io.*;
import java.net.*;

public class HttpClientExmpl
{

    public HttpClientExmpl()
    {
        init();
    }

    public void init()
    {
        MultipartPostMethod postMethod = new MultipartPostMethod();
        
        postMethod.setPath("/servlet/HttpClientExmplServlet");
        postMethod.addParameter("param-name", "param-value");
        
        HttpClient client = new HttpClient();
        HostConfiguration hostConfig = new HostConfiguration();
        
        hostConfig.setHost("localhost", 8081, "http");
        client.setConnectionTimeout(30000);
        client.setHostConfiguration(hostConfig);

        try
        {
            client.executeMethod(postMethod);
        }
        catch(HttpException httpEx)
        {
            httpEx.printStackTrace();
        }
        catch(IOException ioEx)
        {
            ioEx.printStackTrace();
        }
        
        String responseString = postMethod.getResponseBodyAsString();
        
        postMethod.releaseConnection();
        
        System.out.println(responseString);
    }
    
    public static void main(String args[])
    {
        new HttpClientExmpl();
    }
}


_________________________________________________________________________________________________

SERVLET CLASS -

import javax.servlet.*;
import javax.servlet.http.*;

import java.io.*;

public class HttpClientExmplServlet extends HttpServlet
{
    
    public void doPost(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException
    {
        String paramValue = request.getParameter("param-name");
        
        if(paramValue == null)
            throw new ServletException(new NullPointerException("The getParameter call 
returned null."));

        response.setContentType("text/html");
        
        PrintWriter writer = response.getWriter();
        StringBuffer buf = new StringBuffer();
            
        buf.append("<HTML>\n");
        buf.append("<HEAD>\n<TITLE>HTTP Client Example 
Application</TITLE>\n</HEAD>\n");
        buf.append("<BODY>\n");
        buf.append("The servlet received the parameter sent within the post 
method.\n");
        buf.append("</BODY>\n");
        buf.append("</HTML>");

        writer.write(buf.toString());
        writer.flush();
        writer.close();
    }
}

_________________________________________________________________________________________________

RESULTING EXCEPTION -

2003-01-27 21:08:27 StandardWrapperValve[HttpClientExmplServlet]: Servlet.service() 
for servlet HttpClientExmplServlet threw exception
java.lang.NullPointerException: The getParameter call returned null.
        at HttpClientExmplServlet.doPost(HttpClientExmplServlet.java:14)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
        at 
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
        at 
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
        at 
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:223)
        at 
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:213)
        at 
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
        at 
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)
        at 
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
        at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
        at 
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:190)
        at 
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
        at 
org.apache.catalina.valves.CertificatesValve.invoke(CertificatesValve.java:246)
        at 
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
        at 
org.netbeans.modules.web.monitor.catalina.MonitorValve.invoke(MonitorValve.java:142)
        at 
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
        at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
        at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2347)
        at 
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
        at 
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
        at 
org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
        at 
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
        at 
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170)
        at 
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:468)
        at 
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
        at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
        at 
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
        at 
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
        at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
        at 
org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:1027)
        at 
org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1125)
        at java.lang.Thread.run(Thread.java:536)



Any suggestions would be greatly appreciated.

Thanks,

Daniel

p.s. I'm not even sure that MultipartPostMethod is the most suitable object for this.  
If not, I'd appreciate a point in the right direction.

Reply via email to