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

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

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";
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) {		
		Object object = req.getAttribute(paramName);
		VersionReport versionReports = (VersionReport) object;
		PrintWriter out;
		try {
			out = resp.getWriter();
			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");
		} catch (IOException e) {

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("");

		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

				} 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.
					throw ex;

				} finally {

					// Closing the input stream will trigger connection release


		} catch (Exception e) {

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

View raw message