hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "J. Talafous" <jtalaf...@gmail.com>
Subject HttpServletRequest.getAttribute() returning null
Date Fri, 17 Dec 2010 20:35:39 GMT
Hi,

I am setting up a very simple HttpClient from which I want to send a
user object called VersionReport to a servlet running in Tomcat.  But
the getAttribute() method is returning null whatever I do.  I can get
PARAMETERS to work OK, but not ATTRIBUTES.  Any ideas?  Thanks in
advance.

-----
package com.c.gui.installor.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collections;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CollectorServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	public static final String paramName="report";
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) {		
		Object object = req.getAttribute(paramName);
		VersionReport versionReports = (VersionReport) object;
		PrintWriter out;
		try {
			out = resp.getWriter();
			out.append("<HTML>");
			out.append("<BODY>");
			if (versionReports == null) { // ALWAYS RETURNS NULL!
				out.append("parameter '"+paramName+"' not supplied");
				
				for (Object obj : Collections.list(req.getParameterNames())) {
					out.append("<br>  set param: "+obj);
				}
			} else {
				out.append("you sent the following: <br>");
				out.append(versionReports.getDnsName() + "<br>");
				out.append(versionReports.getInet().toString() + "<br>");
				out.append(versionReports.getVersion() + "<br>");
				out.append("have a nice day, goodbye");
			}
			out.append("</BODY>");
			out.append("</HTML>");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

----
package com.c.gui.installor.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

public class TestInstallor {

	public static void main(String[] args) {
		DefaultHttpClient client = new DefaultHttpClient();

		HttpGet httpGet = new HttpGet("http://127.0.0.1:8888/installor/collect");

		try {
			HttpContext context = new BasicHttpContext();
			InetAddress inetAddr = InetAddress.getLocalHost();
			VersionReport versionReport = new
VersionReport(inetAddr,"myhostname", "myversionnumber");
			context.setAttribute(CollectorServlet.paramName, versionReport);
			Object obj = context.getAttribute(CollectorServlet.paramName);
			HttpResponse resp = client.execute(httpGet, context);
			HttpEntity entity = resp.getEntity();
			if (entity != null) {
				InputStream instream = entity.getContent();
				try {

					BufferedReader reader = new BufferedReader(
							new InputStreamReader(instream));
					// do something useful with the response
					System.out.println(reader.readLine());

				} catch (IOException ex) {

					// In case of an IOException the connection will be released
					// back to the connection manager automatically
					throw ex;

				} catch (RuntimeException ex) {

					// In case of an unexpected exception you may want to abort
					// the HTTP request in order to shut down the underlying
					// connection and release it back to the connection manager.
					httpGet.abort();
					throw ex;

				} finally {

					// Closing the input stream will trigger connection release
					instream.close();

				}

				client.getConnectionManager().shutdown();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Mime
View raw message