I would like to know what is the best way to read a text input on a jsp page?
Could anyone tell what is the difference between the two java code separated by VS?
<input type=text id=myInput value="myInput">
<%
String data = request.getParameter("myinput");
//VS
request.setAttribute("myInput", data);
%>
I think you want the difference between a request
attributeandparameter.A request
parameteris always aString(i.e. they are always represented by aStringeven integers, booleans, floats etc like for eg: “1”, “1.1”, “true”) and in a certain URL like:http://google.com/search?q=question&cat=imagesqandcatare calledparametersorquery parametersand their value isquestionandimagesrespectively. This is an example ofGETrequest.POSTrequest parameters would be those which are submitted through an html<form>.Now request
attributesare objects as opposed toparameters. And their value can only be set by usingrequest.setAttribute("myInput", data);heredatacan be aString, instance or object of aPersonclass etc, in shortdatais an object.And one more difference is you don’t have a method
request.setParameter("myinput", data);there is no such method, so request parameters are only set when a html<form>is submitted or the URL contains parameters as explained above.Now with
parametersyou can get them as:even if value of
"myInput"may be anintorboolean.For an attribute you can get them as:
So now you know what is the different between the two codes, one reads the value from a request parameter (
request.getParameter()) and other from request attribute (request.getAttribute()).Let me know if this is not what you wanted.