hi
i have radio button in jsp
i want check that radio is selected or not!!

<input type="radio" name="code1" value="1" >

how can i check in input that radio is selected or not!!
for example put if in input

<input type="radio" name="code1" value="1"  <% if(?)%>>

Dani AI

Generated

You cannot flip a Java/JSP variable from an onclick because JSP runs on the server before the page is sent, while onclick runs later in the browser. Check the selection on the client, then submit and read the value on the server.

Client-side check (before submit):

<form id="myForm" onsubmit="return validate()">
  <input type="radio" name="code1" value="1"> 1
  <input type="radio" name="code1" value="2"> 2
  <button type="submit">Submit</button>
</form>

<script>
function validate() {
  const selected = document.querySelector('input[name="code1"]:checked');
  if (!selected) {
    alert('Please select an option.');
    return false; // prevent submit
  }
  // selected.value has the chosen value
  return true;
}
</script>

Server-side (JSP/Servlet) on postback:

String code = request.getParameter("code1"); // null if nothing selected
boolean isSelected = "1".equals(code);

If you need to redisplay the page preserving the user’s choice, use EL/JSTL instead of scriptlets:

<input type="radio" name="code1" value="1" ${param.code1 == '1' ? 'checked' : ''}>
<input type="radio" name="code1" value="2" ${param.code1 == '2' ? 'checked' : ''}>

Notes:

  • Only the selected radio in a group (same name) is submitted; if none is selected, the parameter is absent (null), not "false".
  • Prefer request scope for one-time form input; avoid storing simple form state in the session.

Good references: MDN on radios (input type="radio") and the Servlet API for reading parameters (ServletRequest#getParameter).

Recommended Answers

All 4 Replies

Chaeck this site:
http://www.w3schools.com/default.asp
Find the attributes of the radio button at the html tutorials or the html references where all the tags and their attributes are listed.

boolean isCkecked = ....;
<input type="radio" name="code1" value="1"  <%= (isCkecked )?"checked":"" %> />

I am almost certain the "checked" is the right thing to use but better check the site as well

tank you for your reply.
i test that but in both case (selected radio and unselected), it return false value.
i put this

<% 

boolean isCkecked = false;
%>
1<input type="radio" name="code1" value="1"  <%= (isCkecked )?"checked":"" %>   >

<%

session.setAttribute("isCkecked",isCkecked); %>

but anyway it return false !!! :(

i need some like this

<% 
boolean isCkecked = false;
%>
1<input type="radio" name="code1" value="1"   onclick="<%isCkecked = true;%>"   >
<%session.setAttribute("isCkecked",isCkecked); %>

Of course it returns false, since you do this:

<% 
boolean isCkecked = false;
%>
.....

<%
session.setAttribute("isCkecked",isCkecked); 
%>

The isCkecked is declared false. Then you pass its value which is false at the session.

This:

<input type="radio" name="code1" value="1"   onclick="<%isCkecked = true;%>"

Will not do what you think it does. Every time you run it the value of isCkecked will ALWAYS BE TRUE, because this: isCkecked = true; is executed before the page loads. It doesn't display anything at the page.

When it loads the isCkecked is already true and nothing can change its value. When the page loads the html code that will be rendered is this:

<input type="radio" name="code1" value="1"   onclick=""

This: <%isCkecked = true;%> just executes this code: isCkecked = true; before the page loads

Whenever you click the radio button you CANNOT change the value of any java variable. The java code is executed at the server and javascript is executed at the client. The onlclick executes javascript code and whatever you write there doesn't change any java values.

What you need is to submit a form with the radio button, take the value with request.getParameter, do whatever you want and then redirect back to the page.

This:

<% 
boolean isCkecked = true;
%>

<input type="radio" name="code1" value="1" <%=(isCkecked)?"checked":"" %>   >

First the isCkecked becomes true.
Then this is executed: (isCkecked)?"checked":"" and the result is the String checked.
Then the page loads like this: <input type="radio" name="code1" value="1" [B]checked[/B] > So in the end the html code that you see at the page is this:

<input type="radio" name="code1" value="1" checked />

So how many times you click the radio button nothing will happen.

This: <%= (...) %> makes the result be displayed but the result is calculated BEFORE the page loads. When it does is just simple html text. (checked )

So TnX for your advantageous guides.
it seems that i go wrong way!!:icon_wink:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.