views:

167

answers:

1

Hi, I'm working on a servlet that makes a connection to a database gets the information of one of the tables ans sends this information to a jsp file. This file will print on the brower the information table, adding radio buttons that allows us to choose one of the rows.

The servlet looks like this:

List<InfoBean> items = new ArrayList<InfoBean>();
if (!conexion.isClosed()){
  Statement st = (Statement) conexion.createStatement();          
  ResultSet rs = st.executeQuery("select * from lista_audio" );
  while (rs.next())
  {items.add(getRow(rs));}
  conexion.close();}
req.getSession().setAttribute("items", items);

In the JSP file I can print a table with the information, adding radio buttons that the user will use to choose 1 row and send the selected info to a servlet using a form I can add:

< form action="administ" method=get enctype=multipart/form-data>    
< table>
 < table border=\"1\">< tr>< th>Title< /th>< th>Author< /th>< th>Album< /th>< /tr>
 < c:forEach items="${items}" var="item">
 < tr>< td><input type="radio" name="SongInfo" value=${item.title}>
 < td>${item.title}< /td>
 < td>${item.author}< /td>
 < td>${item.album}< /td>< /tr>
 < /c:forEach>
< /table>

In the field 'value' I should be able to send to the servlet the information stored in ${item.title}. When I set value = ${item.title} and title is, for example "The bodyguard", in the servlet the information I can retrieve is just "The". It looks like it sends the characters located before the first white space of the string. How could I get the whole string?

Thanks

A: 

Check the generated HTML output (rightclick page in browser, choose View Source). Don't you miss something?

<input type="radio" name="SongInfo" value=The bodyguard>

Yes, the quotes (note the difference in highlighted color, bodyguard became an attribute).

So, fix it:

<input type="radio" name="SongInfo" value="${item.title}">

This way it'll be generated as follows:

<input type="radio" name="SongInfo" value="The bodyguard">

Simple fix, isn't it? :)


That said, your JDBC code is prone to resource leaks. You should close all the resources Connection, Statement and ResultSet in the finally block of the try block you've acquired them. For more hints see this article. Also the list doesn't necessarily need to be put in the session scope. Also the HTML is syntactically invalid, but that's maybe just a copypaste error, it would otherwise not have worked.

Further on, your HTML form is declared to use the request method of GET, but it is also declared to use encoding type of multipart/form-data. This makes no utter sense. Only use this enctype whenever you have an <input type="file"> and if this is the case, the request method ought to be POST.

BalusC