request parameters and add as session attributes - java

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.

Related

Servlet get GET and POST's parameters at the doPost method

My problem is when I'm trying to access a POST Variable with request.getParameter("name"), it works perfectly. But in some conditions, when a POST request arrives at my application, I also need to get GET Parameter from the Query String.
As far as I can see, with getParameter, you can only access current request's parameters, but, as in my condition, as I said, I also need to fetch GET Parameters inside doPost method.
Is there a way to fetch GET Parameters without parsing the Query String?
If you have parameters with the same name in the query string and in the posted form data, use getParameterValues().
Example:-
String fromQuery = request.getParameterValues("name")[0];
String fromForm = request.getParameterValues("name")[1];
The getParameter() method can return (if possible) both GET and POST parameters as it works transparently between GET and POST. You don't need to do any explicit work to get the GET parameters. you can use getParameter for both query parameters and POST parameters.
But should you do it? - It's considered a poor design practice especially if there is sensitive information to be sent.
Take a look at this answer:
HTTP POST with URL query parameters -- good idea or not?
I think you have a confusion here. You can retrieve all the request parameters (in both GET or POST or others) using the same getParameter(..) depending upon the type of request. If it's a GET request, you can retrieve all the GET parameters.
If it's a POST request, you can retrieve all the POST parameters. You get parameters using getParameter(...). And you make one request at a time. If you make a POST request in html or JSP file, you use doPost method receive all the parameters. At this point, there is nothing in GET request. Then after that, you make a GET request, you retrieve all the parameters in doGet method. At this moment, there is nothing in POST. Remember, HTTP requests are stateless.
To complete #Rei answer check out this code :
your form
<form action="?nom=nom1">
<input type="hidden" name="nom" value="nm2"/>
your doPost
System.out.println(request.getParameter("nom"));
String s = "";
for(String ss : request.getParameterValues("nom")) {
s += "|" + ss;
}
System.out.println(s);
System.out.println(request.getParameterMap().get("nom"));
what will be printed
nom1
|nom1|nm2
[Ljava.lang.String;#c7068db
ps : thanks to Julien for the code and testing

How do I retrieve the jsessionid from URL in JSP?

I know how to pass the jsessionid to the URL. I encode the url, which will look like this:
mysite.com;jsessionid=0123456789ABCDEF (http)
Does there exist a built-in method to retrieve the jsessionid from the URL, using Java? I found this method in the javadocs, isRequestedSessionIdFromURL, but it doesn't help me actually retrieve the value. Do I have to build my own retrieval method?
Thank you.
JSP has an implicit session object, similar the request object. It is an instance of java.servlet.http.HttpSession, which has the method getId().
So, you should be able to just do session.getId() in your JSP page.
Cookieless sessions are achieved in Java by appending a string of the format ;jsessionid=SESSION_IDENTIFIER to the end of a URL. To do this, all links emitted by your website need to be passed through either HttpServletRequest.encodeURL(), either directly or through mechanisms such as the JSTL tag. Failure to do this for even a single link can result in your users losing their session forever.
session.getId() should be able to get you the session id. WLS would be doing the actual parsing of the URl to retrieve the session id once it identifies that the session id is not stored in the cookie or in hidden form fields. The sequence usually is Cookie - URL - Hidden Form Fields.

Server side fix for receiving string containing '&'(ampersand)

We have already shipped a client (.NET WinForms) application which sends customer data to Java server. While most of the data sent by client are accepted at server side, some records are truncated because of the presence of & character in it, as client sends raw & and do not URL encode it, we have fixed it by using the below code:
string dataBefore="A & B";
string dataBefore = System.Web.HttpUtility.UrlEncode(dataBefore);
It is impossible for us to update all the client applications(which are already shipped) and we are thinking of a server side fix.
With the help of Fiddler, we have made sure the data has left client in full, but when server reads as below:
//in java
String dataReceied=request.getParameter("data");
it gets truncated if data contains &
Could someone help us suggesting a server side(java) fix for this? Is it possible to access the request stream in java(instead of request.getParameter())?
You can get access to the raw query string using HttpServletRequest.getQueryString() (javadoc), which:
returns a String containing the query string or null if the URL contains no query string. The value is not decoded by the container.
You can them perform manual decoding on that string, instead of using getParameter().
#Wesley's idea of using getParameterMap() may not be useful, because you don't know which order the parameters were supplied in.
I'd suggest implementing this logic as a servlet filter, to decouple the fixing of the broken parameters from your actual servlet logic. This would involve writing a custom subclass of HttpServletRequestWrapper which overrides getParameter() and manuyally decodes the query string. Your servlet would then be able to use the HttpServletrequest API as though everything was tickety boo.
It is cut off because & signifies a new URL parameter in a request like this:
google.com?query=java&page=2. Java converts all these parameters to a Map, so that's where it goes wrong.
Have you tried iterating through request.getParameterMap()? The remaining data is most likely in the name of the next parameter. If that does not work, check out the API of HTTPServletRequest to see if there is another way to get your data.
Good luck!
PS How angry are you guys at the intern that wrote & shipped that client? That sounds messed up!

cookie tutorial problem

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.

parameter of passing cookie

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.

Categories

Resources