• Post Reply Bookmark Topic Watch Topic
  • New Topic

Edit form data with radio buttons  RSS feed

 
Howard Watson
Ranch Hand
Posts: 67
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone know a better way to handle the following? I'm retrieving data from a db and need to reset the radio buttons and check boxes.

servletGender
String sGender = rs.getString("gender");
if (sGender.equals("M")) {
sb.append("<input type="radio" name="n" value="M" checked="checked" />");
sb.append("<input type="radio" name="n" value="F" />");
}
else {
sb.append("<input type="radio" name="n" value="M" />");
sb.append("<input type="radio" name="n" value="F" checked="checked" />");
}
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Howard,
you might want to resonsider the whole approach. Assembling the response html in a string buffer is a maintenance nightmare. Consider the following:
- create an XML file from your database (you could consider castor)
- use an XLST tranformation to create the html send back to the browser

This way someone else can take care of the look & feel and you don't need to touch your (Java) code for updates.
Hth
stw
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While I agree with Stephan that building HTML in Java is a poor approach, XSLT is a bit overkill for most usages. Have you considered forwarding to a JSP to render the view?
 
Neeraj Gupta
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Howard,

Though you can use XSLT and JSPs but if any case if you wanna go for servlets then u can optimize this 9 lines of code into just 2 and i hope your problem gets solved...

sb.append("<input type=\"radio\" name=\"n\" value=\"M\" " + rs.getString("gender").equals("M")?"checked=\"checked\"": "" +"\" />");

sb.append("<input type=\"radio\" name=\"n\" value=\"F\" " + rs.getString("gender").equals("F")?"checked=\"checked\"": "" + " />");

This way you dont have repeat the radio buttons for setting them....i have used "\" here as escape character for inserting quotes in response string....

Neeraj
SCJP
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Neeraj Gupta:
Though you can use XSLT and JSPs but if any case if you wanna go for servlets then u can optimize this 9 lines of code into just 2 and i hope your problem gets solved...

sb.append("<input type=\"radio\" name=\"n\" value=\"M\" " + rs.getString("gender").equals("M")?"checked=\"checked\"": "" +"\" />");

sb.append("<input type=\"radio\" name=\"n\" value=\"F\" " + rs.getString("gender").equals("F")?"checked=\"checked\"": "" + " />");

This way you dont have repeat the radio buttons for setting them....i have used "\" here as escape character for inserting quotes in response string....


it is about the better way. use jsp to render your view, as Bear mentioned above.
 
Howard Watson
Ranch Hand
Posts: 67
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stephan,
There isn't anyone else. At this point XML/XSLT is a level of complexity I don't need. Do you know a good reference that demonstates XML/XSLT in a context I need to use it?

Thanks Bear, Neeraj, adeel,
Thanks for the optimization suggestion! I have a JSP. action="servlet/saveParameters" to a database. What I'm working on now is:
read the data back from the datatbase
reset the form to those values
allow the user to edit the form
and resave the new values

The reason I chose a servlet was that the presentation (xhtml) was going to quickly become lost in scriplets deciding which radio button to select based on session variables passed from a servlet that read the original data from the database.
With text boxes it's a simple process <%=value%>. With radio buttons and check boxes... This page is all radio buttons, check boxes and drop-down lists. Although, not a purist for seperation of code and presentation, if I can't find the presentation in the code...
Also, a servlet allows me to query the database, reconstruct the page with rs.getString vaules, and use the same action="servlet/saveParameters".
Thanks again. Any additional comments/suggestions welcome.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason I chose a servlet was that the presentation (xhtml) was going to quickly become lost in scriplets deciding which radio button to select


There's no need for this. Use a servlet controller to make all the decisions for the page. Abstract the data the page needs and place it on the request as scoped variables (attributes). Forward to the JSP page and use (preferably) scriptless JSP notation (JSTL and EL to the rescue) to display your view.

This design achieves a number of good things: (a) complex computations are performed in Java classes where they belong, and (b) presentation is performed in JSP pages where it belongs.
 
Howard Watson
Ranch Hand
Posts: 67
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you saying something like:
dbServlet:
==========

String sGender = rs.getString("gender");

if (sGender.equals("M") {
sGender = "<input type="radio" name="n" value="M" checked="checked" /><br />
<input type="radio" name="n" value="F" />";
}
else {
sGender = "<input type="radio" name="n" value="M" /><br />
<input type="radio" name="n" value="F" checked="checked" />";
}

session.setAttribute("gender",sGender);

Gender.jsp (not scriptless, I did look at your link)
==========
<html>
<%
String sGender = session.getAttribute("gender");
%>
<%=sGender%> // display radio buttons, the correct one selected
</html>
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I hardly ever build up HTML in Java. That way lies baldness. I know.

More along the lines of setting an abstracted value that is easily digested in a JSP. So in the servlet, you might see something like:



and in the JSP (assuming JSP 2.0 -- if not use scriptlet expression or JSTL instead of EL):



Note how the (in this case very simple) computation is done in the servlet (a more complex computation would illustrate this better), so that what appears on the JSP is butt-simple.
[ October 04, 2004: Message edited by: Bear Bibeault ]
 
Howard Watson
Ranch Hand
Posts: 67
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. JSP 2.3. Between your example and the earlier suggested optimization I see what I didn't know. ?: if-then-else.

Something in common: Moved to Montana from Austin TX. (nice place in the 60s and 70s) in 1988. Worked for Control Data for a couple of years until they sold their automated wagering division.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSP 2.3


I assume you mean Servlet 2.3? (Which probably means JSP 1.2).

The current JSP version is 2.0.

Montana.... brrrrr!!!
 
Howard Watson
Ranch Hand
Posts: 67
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure why I thought I needed to make a decision about the entire line of mark up based on the value of the session variable, when I was only concerned with checked or not.

This is what I ended up using:

getData.servlet
session.setAttribute("gender",rs.getString("genter"));

EditDB.jsp
String sGender = (String) session.getAttribute("gender");
<input type="radio" id="male" name="gender" value="M" <%if (sGender.equals("M")) {out.println("checked=\"checked\"");}%> />
<input type="radio" id="female" name="gender" value="F" <%if (sGender.equals("F")) {out.println("checked=\"checked\"");} %> />

This works for me. Thanks to all that insisted the servlet approach was nuts.
 
ernest fakudze
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Howard,

Bear is right, you should use JSPs to build and display the view at all times. I can't for the life of me think of a situation where the developer has no choice but to output the view using a servlet. Servlets are good for use as controllers while JSPs are geared towards output.

Also, never find yourself using out.println() in a JSP because that belongs in a servlet ( I have seen this in books - shocking!). Instead, you need to switch between contexts (JSP and HTML) using the <% and %> tags. Then display computed values using expressions e.g. <%=firstName%>
Hope this helps
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!