Do a browser pass the cookie name and cookie value saperately or both in one string ? Do it also pass domain or not??? I want to know what happens in the case of a java browser.
Cookies are sent as HTTP header, which is a pair of strings.
What kind of java browser are you talking about?
If you want to make a HTTP request in java, I'd suggest you use HTTPClient.
Related
I was given this as an in class problem and recievd 0 as the result, but I can't find the fault with it. The questions is Write the lines of code you would need to use in a servlet to retrieve a parameter
from an incoming request and add it to the session as an attribute:
My answer:
String param1= request.getParameter("param1");
HttpSession session= request.getSession();
String parameter1= (String)request.getAttribute("param1");
session.setAttribute("param1", parameter1);
Also, is the '(String)' necessary in the second line? or just good practice?
Thanks :)
You've made a very common mistake, confusing attributes with parameters. In your code you have:
String parameter1= (String)request.getAttribute("param1");
Attributes can be thought of as meta-data of the request. For example, if the request is made via SSL then you can get data about the request from the attributes. Take a look at the documentation for getAttribute for more details.
Parameters, on the other hand, can be used to get URL parameters. Your last question actually points you in the correct direction - getParameter() already returns a String so you don't need to cast it.
Properly written the line above should be:
String parameter1= request.getParameter("param1");
getParameter() - used to get url parameter from request in server side(java side).
Where as If you want to fetch any values from java side to jsp(view side)
you can use setAttribute() in Server side(Java side) and get the value using getAttribute() from jsp.
I am talking to a file upload service that accepts post data, not form data. By default, java's HttpURLConnection sets the Content-Type header to application/x-www-form-urlencoded. this is obviously wrong if i'm posting pure data.
I (the client) don't know the content type. I don't want the Content-Type header set at all. the service has a feature where it will guess at the content type (based on the file name, reading some data from the file, etc).
How do I unset a header? There's no remove header, and setting it to null doesn't change the value and setting it to the empty string results in the header being set with no value.
I haven't tested this approach but you can try this:
Extend HttpURLConnection and try by overriding its getContentHandler() and setContentHandler(...) methods. Most probably this should work as, you will take a look at code of getContentHandler().
Use Apache HttpClient instead of URLConnection
Use fluent Request to generate your request
use removeHeader()
What do you mean "i don't want the Content-Type header to set at all"?
The browser (or other http client) sends your post request to the server, so it has to inform the server which way it encoded the parameters.
If the Content-Type header is not set, on the server side you (= your server) won't be able to understand how to parse the received data.
If you didn't set Content-Type, the default value will be used.
You browser (or other http client) MUST do two things:
Send key/value pairs.
Inform the server how the key/value pairs were encoded.
So, it is impossible to completely get rid of this header.
I just accomplished this by setting the header to null.
connection.setRequestProperty(MY_HEADER, null);
Is there a way to use addRequestProperty if you have already connected to the URLConnection?
I need to do this cause I first have to connect to get the headerfields for the first Cookie, but later I have to send that first Cookie in order to get headerfields where a second set-cookie field is defined.
That won't work. The HTTP protocol conveys those properties as header items at the beginning of the request data. You'll need multiple requests.
http://www.hccp.org/java-net-cookie-how-to.html
According to this link I was trying to create cookie and send cookie to enter in a cookie site. But it is not working. Is there is any problem in that. I have some confusion on the method setRequestProperty of URLConnection. I don't understand what are they trying to send by passing "Cookie"? Is it only a string or name or value??
urlConn.setRequestProperty("Cookie", myCookie);
"Cookie" in this case is a way to tell the setRequestProperty method that the argument (which is really just a String) should be treated as a cookie.
setRequestProperty may be more useful for many kinds of properties, but addRequestProperty would be more useful for cookies, because you can have multiple cookies per request. The properties are specified in RFC 2068 -- read especially section 14.
I have a cookie that is formatted like partA:partB. The colon is not escaped in any fashion. I need to read this cookie in a JSP script, and request.getCookies() is only returning partA. I can't change the cookie because it is used in multiple applications, and fixing the cookie would break production code. Any ideas how I can read the full value of this cookie?
You should be able to read the Cookie header directly using the HttpServletRequest.