简体   繁体   中英

Servlet retrieve pre selected radio button

I pre select my radio buttons by the following code. The following inputs wrap in a form that post back to the same servlet.

buf.append("<input type=\"radio\" name=\"FTNAME\" value=\""+ FTNAME+ "\" " + (FTNAME.equals("Arial") ? "checked=\"checked\"" : "") + ">Arial &nbsp &nbsp &nbsp");
buf.append("<input type=\"radio\" name=\"FTNAME\" value=\""+ FTNAME+ "\" " + (FTNAME.equals("Serif") ? "checked=\"checked\"" : "") + ">Serif &nbsp &nbsp &nbsp");
buf.append("<input type=\"radio\" name=\"FTNAME\" value=\""+ FTNAME+ "\" " + (FTNAME.equals("SansSerif") ? "checked=\"checked\"" : "") + ">SansSerif <br><br>");

However, when I try to do

FTNAME = request.getParameter("FTNAME") == null ? "Arial" : request.getParameter("FTNAME"); //Arial as font name default

to get my FTNAME, it always return what it was set from the code above, not my new selection.

Any suggestion?

Yes: My suggestion is you'd better read the HTML code produced by your servlet. You'll find that all three radios have THE SAME VALUE.

Because you're setting your value as Arial for all three radio buttons. So no matter what radio button you choose it will always return you arial.

This how your html page would look like:

<input type="radio" name="FTNAME" value="Arial" checked="checked">
<input type="radio" name="FTNAME" value="Arial">
<input type="radio" name="FTNAME" value="Arial">

And you servlet request.getParameter("FTNAME") would alway return you the value "Arial". You need to change to something like this

buf.println("<input type=\"radio\" name=\"FTNAME\" value=\"Arial\"" + (FTNAME.equals("Arial") ? "checked=\"checked\"" : "") + ">Arial &nbsp &nbsp &nbsp");
buf.println("<input type=\"radio\" name=\"FTNAME\" value=\"Serif\""+ (FTNAME.equals("Serif") ? "checked=\"checked\"" : "") + ">Serif &nbsp &nbsp &nbsp");
buf.println("<input type=\"radio\" name=\"FTNAME\" value=\"SansSerif\""+ (FTNAME.equals("SansSerif") ? "checked=\"checked\"" : "") + ">SansSerif <br><br>");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM