Hi Talafous,
In the Servlet API, the HttpServletRequest object has getParameter() &
getAttribute() method, also has setAttribute() method but no setParameter()
method.
So it is said that it the two components is linked with the other, the
getParameter() method will work, otherwise, if the one component forward to
the other, the getAttribute() method will work.
>From your code, the two components just the linked relationship, not the
forward, so the getAttribute() method will return null.
If you want to use the getAttribute() in the Servlet, a primary controller
servlet is a must, all the request must go through the primary controller
then to the logic servlet. Thus, you can do something in the primary
controller, such as put the parameter into the request attribute scope.
for example,
private void putParameterToAttribute(HttpServletRequest request) {
Map<String, String[]> paraMap = request.getParameterMap();
Iterator<String> iter = paraMap.keySet().iterator();
boolean isGetMethod = request.getMethod().equalsIgnoreCase("get");
while (iter.hasNext()) {
String key = iter.next();
String[] values = paraMap.get(key);
if (isGetMethod && needTranslateGetMethod && values != null) {
for (int i = 0; i < values.length; i++) {
try {
values[i] = new String(values[i].getBytes("8859_1"),
"UTF-8");
} catch (UnsupportedEncodingException e) {
if (log.isDebugEnabled()) {
log.debug(e);
}
if (log.isInfoEnabled()) {
log.info("Unsupport encode!");
}
}
}
}
if (request.getAttribute(key) == null) {
if (values != null) {
if (values.length == 1) {
request.setAttribute(key, values[0]);
} else {
request.setAttribute(key, Arrays.asList(values));
}
}
}
}
}
Hope this help,
Best Wishes!
Nan Lei
2010/12/18 J. Talafous <[email protected]>
> 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: [email protected]
> For additional commands, e-mail: [email protected]
>
>