Form help

How can I collect the data from a FORM element?
[68 byte] By [mtndeva] at [2007-9-26 1:38:26]
# 1
Use a jsp or servlet to process your form data - have a look at the following article http://www.javaworld.com/javaworld/jw-03-2000/jw-0331-ssj-forms.htmlRichard
Rijaos at 2007-6-29 2:26:23 > top of Java-index,Archived Forums,Portability & Platform Independence [Archive]...
# 2
hi richard,i need code in pure java can u help me
mtndeva at 2007-6-29 2:26:23 > top of Java-index,Archived Forums,Portability & Platform Independence [Archive]...
# 3
Give me more info - what are you trying to do?
Rijaos at 2007-6-29 2:26:23 > top of Java-index,Archived Forums,Portability & Platform Independence [Archive]...
# 4
just i want to get data from form using basic java
mtndeva at 2007-6-29 2:26:23 > top of Java-index,Archived Forums,Portability & Platform Independence [Archive]...
# 5
Where do you want the data?
jsalonen at 2007-6-29 2:26:23 > top of Java-index,Archived Forums,Portability & Platform Independence [Archive]...
# 6

Ok,

to read data from the following sample form using a jsp:

<html>

<head>

<title>test 1</title>

</head>

<body>

<form action=my.jsp method=post>

p1: <input type=text name=p1>

p2: <input type=text name=p2>

</form>

</body>

</html>

the jsp page (you can do the same thing with a servlet)

my.jsp

<html>

<head>

<title>test 2</title>

</head>

<body>

<%

String newp1 = request.getParameter("p1");

String newp2 = request.getParameter("p2");

out.println("p1 = " + newp1);

out.println("p2 = " + newp2);

%>

</body>

</html>

Hopefully that'll get you started, you should have a look at the tutorials on Sun's site too.

Richard

Rijaos at 2007-6-29 2:26:23 > top of Java-index,Archived Forums,Portability & Platform Independence [Archive]...
# 7
Hi,in jsp & servlet, you can get back all the field names of your form with request.getParameterNames();you can either find the value of a field in your form with request.getParameter(yourFieldName);hope this helps !Badr.
zbadr at 2007-6-29 2:26:23 > top of Java-index,Archived Forums,Portability & Platform Independence [Archive]...
# 8
Hi mtndeva,Can you please specify what exactely you need to do.Regards,Tirumalarao.Developer TechnicalSupport,SunMicroSystem,India.
rao_indts at 2007-6-29 2:26:23 > top of Java-index,Archived Forums,Portability & Platform Independence [Archive]...
# 9

Hi mtndeva,

I am not clear what you are expecting , any how iam sending related code.

<HTML>

<HEAD>

<SCRIPT>

function FormData (form) {

this.toString = FormData_toString;

this.toQueryString = FormData_toQueryString;

var fields = this.fields = new Object();

for (var e = 0; e < form.elements.length; e++) {

var field = form.elements[e];

if (field.name) {

if ((field.type.toLowerCase() == 'text'

|| field.type.toLowerCase() == 'textarea'

|| field.type.toLowerCase() == 'password'

|| field.type.toLowerCase() == 'file')

&& field.value)

fields[field.name] = field.value;

else if ((field.type.toLowerCase() == 'checkbox'

|| field.type.toLowerCase() == 'radio')

&& field.checked)

fields[field.name] = field.value;

else if (field.type.toLowerCase() == 'select-one'

&& field.selectedIndex != -1)

fields[field.name] =

field.options[field.selectedIndex].value;

else if (field.type.toLowerCase() == 'select-multiple'

&& field.selectedIndex != -1) {

fields[field.name] = new Array();

for (var i = 0; i < field.options.length; i++)

if (field.options.selected)

fields[field.name][fields[field.name].length] =

field.options.value;

}

}

}

}

function FormData_toString () {

var r = '';

for (var field in this.fields)

r += field + ': ' + this.fields[field] + '\n';

return r;

}

function urlEncode (string) {

string = string.replace(/ /g, '+');

return escape(string);

}

function FormData_toQueryString () {

var r = '';

for (var field in this.fields)

if (typeof this.fields[field] != 'string')

for (var i = 0; i < this.fields[field].length; i++)

r += urlEncode(field) + '=' + urlEncode(this.fields[field])

+ '&';

else

r += urlEncode(field) + '=' + urlEncode(this.fields[field]) + '&';

r = r.substring(0, r.length - 1);

return r;

}

</SCRIPT>

</HEAD>

<BODY>

<A HREF="javascript: var fd = new FormData(document.formName);

alert(fd);

void 0"

>

show form data

</A>

|

<A HREF="javascript: var fd = new FormData(document.formName);

alert(fd.toQueryString());

void 0"

>

show query string

</A>

<FORM NAME="formName">

<INPUT TYPE="text" NAME="aTextField" VALUE="JavaScript.FAQTs.com">


<TEXTAREA NAME="aTextArea" ROWS="2" COLS="20">

JavaScript.FAQTs.com

</TEXTAREA>


<INPUT TYPE="password" NAME="aPassword" VALUE="JavaScript">


<INPUT TYPE="file" NAME="aFileName">


<INPUT TYPE="radio" NAME="aRadioGroup" VALUE="radio1">

<INPUT TYPE="radio" NAME="aRadioGroup" VALUE="radio2">

<INPUT TYPE="radio" NAME="aRadioGroup" VALUE="radio3">


<INPUT TYPE="checkbox" NAME="aCheckBox" VALUE="checkbox1">


<SELECT NAME="select1">

<OPTION VALUE="Kibology">Kibology

<OPTION VALUE="Xibology">Xibology

<OPTION VALUE="Scriptology">Scriptology

</SELECT>


<SELECT NAME="select2" MULTIPLE>

<OPTION VALUE="Kibology">Kibology

<OPTION VALUE="Xibology">Xibology

<OPTION VALUE="Scriptology">Scriptology

</SELECT>


<INPUT TYPE="button" NAME="aButton" VALUE="button0">


<INPUT TYPE="submit" NAME="aSubmit" VALUE="submit0">

</FORM>

</BODY>

</HTML>

I hope this will help you out.

Still if you have any doubts please post the query with your exact requirements.

Regards,

TirumalaRao,

Developer TechnicalSupport,

Sun MicroSystem,India.

rao_indts at 2007-6-29 2:26:23 > top of Java-index,Archived Forums,Portability & Platform Independence [Archive]...
# 10
Hi raoindts, thankyou ... how did u got this code.. its ur owm code or u got from any other site.....
mtndeva at 2007-6-29 2:26:23 > top of Java-index,Archived Forums,Portability & Platform Independence [Archive]...