• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

confused with ajax and http request

 
zb cong
Ranch Hand
Posts: 416
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getAttribute() is not used to retrieve request parameters. That's the job of getParameter().
 
zb cong
Ranch Hand
Posts: 416
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 416
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 416
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic