Meaningless Drivel is fun!*
The moose likes Servlets and the fly likes confused with ajax and http request Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "confused with ajax and http request" Watch "confused with ajax and http request" New topic
Author

confused with ajax and http request

zb cong
Ranch Hand

Joined: Jan 14, 2002
Posts: 416
in my web page:

xhttp = new XMLHttpRequest();
xhttp.open("POST", "media.do", true);
xhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhttp.send("data=value");
this is a struts project, in the action which map to "media.do":

System.out.println(request.getAttribute("data"));

it print null, when i use buffered reader of request to retrieve the http body, the "data=value" can be printed. so i wonder why the "request.getAttribute("data")" is null?

thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61628
    
  67

getAttribute() is not used to retrieve request parameters. That's the job of getParameter().


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
zb cong
Ranch Hand

Joined: Jan 14, 2002
Posts: 416
no, this is not "GET" action, i use AJAX to "POST" data, and i put the "data=value" in the http body, unfortunately, the value can not be retrieved.
as i know, request.getParameter() is used http GET action, and put the parameters in the url.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61628
    
  67

zb cong wrote:no, this is not "GET" action, i use AJAX to "POST" data, and i put the "data=value" in the http body, unfortunately, the value can not be retrieved.
as i know, request.getParameter() is used http GET action, and put the parameters in the url.

Nope, this is not correct. getParameter() is used to retrieve any parameters regardless of whether the HTTP method is a GET or a POST.

getAttribute() has nothing whatsoever to do with retrieving request information.
zb cong
Ranch Hand

Joined: Jan 14, 2002
Posts: 416
but the api say:

getAttribute
Object getAttribute(String name)Returns the value of the named attribute as an Object, or null if no attribute of the given name exists.
Attributes can be set two ways. The servlet container may set attributes to make available custom information about a request. For example, for requests made using HTTPS, the attribute javax.servlet.request.X509Certificate can be used to retrieve information on the certificate of the client. Attributes can also be set programatically using setAttribute(java.lang.String, java.lang.Object). This allows information to be embedded into a request before a RequestDispatcher call.

Attribute names should follow the same conventions as package names. This specification reserves names matching java.*, javax.*, and sun.*.


Parameters:
name - a String specifying the name of the attribute
Returns:
an Object containing the value of the attribute, or null if the attribute does not exist





and i have also tried getParameter method, it still return null.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61628
    
  67

Since you read the javadoc, what part of that says that getAttribute() should be used to fetch parameters when the HTTP is POST?

If getParameter() isn't working, then it's time to debug your request to make sure it's being correctly sent. Use a tool such as Fiddler, or the HttpFox plugin for Firefox, to see the actual HTTP request being sent and ensuring that it's all correct.
zb cong
Ranch Hand

Joined: Jan 14, 2002
Posts: 416
Bear Bibeault wrote:Since you read the javadoc, what part of that says that getAttribute() should be used to fetch parameters when the HTTP is POST?

If getParameter() isn't working, then it's time to debug your request to make sure it's being correctly sent. Use a tool such as Fiddler, or the HttpFox plugin for Firefox, to see the actual HTTP request being sent and ensuring that it's all correct.


you can refer to the ServletRequest page of java doc, the getAttribute method is describe there.
ok, let me try to trace the http protocol..
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61628
    
  67

zb cong wrote:you can refer to the ServletRequest page of java doc, the getAttribute method is describe there.

Yes, I know all about getAttribute() and it is not what you need to use!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: confused with ajax and http request