jsp radio button help

[nobr]hello,

I have implemented a drop down list in jsp, BUT however instead of

a drop down list i was wanting the results to be displayed in a

radio box format where the user would then choose the result he/she

would like the system to perform.below is the code for the

dop down list it works fine BUT::::::

**********************some code above*******************************

<form action ="searchAmazon.jsp" method="get">

<select name="authorname">

<%

for(int i=1;i<=v.size();i++){

%>

<option value="<%=v.elementAt(i-1) %>"><%= v.elementAt(i-1) %> </option>

<%

}

%>

</select>

Please select the Author name you would like the

system to search for?

<input type="submit" value="Search">

</form>

<%

}

%>

</body>

</html>

**********************************

the above code works but i am trying to output the results

in a RADIO BOX format, i have the code BUT when you press the

search button it sends some garbage parameter to the

search method and not the real option ... here is the code

for the radio part:

***********************

<form action ="searchAmazon.jsp" method="get">

<%

for(int i=1;i<=v.size();i++){

%>

<input type="radio" name="searchname">

<option value="<%= v.elementAt(i-1) %>"><%= v.elementAt(i-1) %>

<%

}

%>

Please select the Author name you would like the system to search for?

<input type="submit" value="Search" onclick="">

<input type="reset">

</form>

<%

}

%>

</body>

</html>

************************88

i don not know y it is doing that can anyone help pls

thanks moh[/nobr]

[2071 byte] By [moh_1_and_onlya] at [2007-11-26 18:01:41]
# 1

An HTML radio button does not require an OPTION tag.

Here's an example of how radio button works:

http://www.htmlcodetutorial.com/forms/_INPUT_TYPE_RADIO.html

So change your radio button code to:

<input type="radio" name="searchname" value="<%= v.elementAt(i-1) %>">

and remove the <option tags.>

appy77a at 2007-7-9 5:31:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

thanks appy77 for your help but somewhat the problem is more complicated. the radio box will contain two parameters from different

vectors i.e. one of the parameter is the Book title and the second is the

Author name.

thus i require that when a user clicks the option radio button both parameter should be read so i can them to search a method...

so for example on a web page we will have

*********************

book title, Author

so when the user clicks the submit button both of the above will be taken as different parameters

pls does anyone know hw to do it help,

moh

moh_1_and_onlya at 2007-7-9 5:31:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

If you want to pass in 2 parameters while using the radio button there are 2 ways to do it, there may be other ways too.

1) Concatinate the two values with a separator character like, an underscore for example, so it would look something like

<input type ="radio" name="radioName" value ="<%=variable1%>_<%=variable2%>"/>

When the form is submitted , the concatinated value gets passed through so the value will look something like for example ABC_123 assuming that ABC is stored in variable1 and 123 is stored in variable2

Then you will read the request parameter into a string

<%

String selectedValue = request.getParameter("radioName");

String fristValue = selectedValue.substringBefore("_");

String secondValue = selectedValue.substringAfter("_");

%>

The above should give you the 2 values.

But the drawback is that the above solution is not good in case you want to pass through too many parameters, for example if you have 10 parameters to pass through per radio button selection. That brings us to solution 2

2) Assuming that the parameters are stored in a persistant store like the database and for each record there's a primary key that uniquely identifies the record.

Simply store each record into a Java object along with the primary key for each object.

On your radio buttons simply pass the primary key to the next page, then when you have the primary key query the database and get the entire record form the database.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If you read the HTML spec for the <option tag, it is supposed to work only under the ><select tag, if you force it to work under any other tag it will not work.>

appy77a at 2007-7-9 5:31:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
thanks again appy77,but just want to ask you when using the method substringBefore, i haveimported the java.lang.* but still it gives me a compiler error not recognized. do you know what else i must be missingthanks for help,moh
moh_1_and_onlya at 2007-7-9 5:31:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Sorry, substringBefore and substringAfter are not defined in the String API http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html and that's why you are getting the compile error.

I modified the code to use, just the substring function and it works in the following sample, try it:

<%

String something ="123_566";

int indexOfUnderscore = something.indexOf('_');

out.println(indexOfUnderscore);

String firstString = something.substring(0,indexOfUnderscore);

String secondString = something.substring(indexOfUnderscore + 1);

out.println("First String is : " + firstString);

out.println("Second String is : " + secondString);

%>

On a side note fn:substringBefore and fn:substringAfter are JSTL functions http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/index.html which can also be used in JSPs , but for this you'll need to install and configure JSTL 1.1

JSTL is a new and improved way of writing JSPs it makes pages cleaner and easier to read, but it does take a little time to learn all the JSTL tags.

Message was edited by:

appy77

appy77a at 2007-7-9 5:31:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

[nobr]sorry appy77 i just tried ur code but no results are being displayed

dont know y...

this is the jsp radio part of it

**********************************

form action ="searchAmazon.jsp" method="get">

<%

for(int i=1;i<=v.size();i++){

%>

<input type="radio" name="authortitle" value=<%=v.elementAt(i-1)%>> <%=v.elementAt(i-1)%> ,

<%=v1.elementAt(i-1)%>

<%

}

%>

Please select the Title name you would like the system to search for?

<input type="submit" value="Search">

</form>

******************************

and this is where i read the parameters in a diff file

*********************

String read = "";

String selectedValue = request.getParameter("authortitle");

int separator = selectedValue.indexOf(",");

String firstValue = read.substring(0,separator);

String secondValue = read.substring(separator+1);

out.println("First String is : " + firstValue);

out.println("Second String is : " + secondValue);

****************

it is not also outputting the println outputs..

can anyone c what the problem is..

thanks,

moh[/nobr]

moh_1_and_onlya at 2007-7-9 5:31:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

[nobr]A few bits of advice, please reference the HTML syntax for the HTML tags you plan to use - it shows how they work.

In your code you are still passing only the first value in the <input type="radio" value="Passing Only One Value Here" />

If you changed it to pass 2 values as a concatenated string like this:

<input type="radio" value="FirstStringValue_SecondStringValue"> , then it would pass in both values as 1 string.

I've modified and tested the following code, it should work - try it:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

somefile.jsp

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

<%@ page import="java.util.Vector"%>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>

<head><title>Simple jsp page</title></head>

<body>

<form action="searchAmazon.jsp" method="get">

<%

Vector v = new Vector();

v.add("123");

v.add("456");

Vector v1 = new Vector();

v1.add("ABC");

v1.add("DEF");

%>

<%

for (int i = 1; i <= v.size(); i++) {

%>

<input type="radio" name="authortitle" value="<%=v.elementAt(i-1)%>,<%=v1.elementAt(i - 1)%>">

<%=v.elementAt(i - 1)%>,<%=v1.elementAt(i - 1)%>

<br/>

<%

}

%>

<br/>

Please select the Title name you would like the system to search for?

<input type="submit" value="Search">

</form>

</body>

</html>

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

serachAmazon.jsp

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>

<head><title></title></head>

<body>

<%

String read = ""; /* No need for this read variable, directly use selectedValue */

String selectedValue = request.getParameter("authortitle");

if (selectedValue != null) {

out.println("Selected Value is : " + selectedValue + "<br>");

int separator = selectedValue.indexOf(",");

out.println("Index of separator is : " + separator + "<br>");

String firstValue = selectedValue.substring(0, separator);

String secondValue = selectedValue.substring(separator + 1);

out.println("First String is : " + firstValue + "<br>");

out.println("Second String is : " + secondValue + "<br>");

/*The value stored in selectedValue remains unchanged*/

out.println("Selected Value is : " + selectedValue + "<br>");

} else {

out.println("Please press the back button and select a value. " + "<br>");

}

%>

</body>

</html>

[/nobr]

appy77a at 2007-7-9 5:31:20 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
thanks appy77 you really know ur stuff bout jspthanks again
moh_1_and_onlya at 2007-7-9 5:31:20 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

You're welcome. I'm still learning too ;-)

Once you become familiar with the basics of JSPs I encourage you to take a look at JSTL 1.1 - JSP Tag Libraries - they are a new and improved way of writing JSPs, with JSTL your JSP code will be much simpler, cleaner, easier to read and maintain.

Using scriplets <% /* code inside here is a scriplet */ %> or expressions <%=%> or declarative statements <%! %> , makes the code very hard to read expecially when it includes business logic.

Ideally JSPs are meant only for the presentation layer, and the business logic should be encapsulated inside JavaBeans and other backend Java code. With this design the page becomes much easier and cleaner to maintain.

Also XHTML is a better way of writing HTML , as it minimizes cross-browser compatibility issues.

But both XHTML and JSTL take a little time to learn, so as your schedule permits I encourage you to check them out.

appy77a at 2007-7-9 5:31:20 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...