简体   繁体   English

Servlet,JSP,JavaBeans和HTML表单

[英]Servlet, JSP, JavaBeans and HTML form

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. 我正在使用一个servlet,该servlet与数据库建立连接以获取表之一的信息,并将该信息发送到jsp文件。 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: servlet看起来像这样:

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: 在JSP文件中,我可以打印带有信息的表,添加单选按钮,用户将使用该单选按钮选择1行,然后使用我可以添加的表单将所选信息发送到servlet:

< 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}. 在“值”字段中,我应该能够将$ {item.title}中存储的信息发送到servlet。 When I set value = ${item.title} and title is, for example "The bodyguard", in the servlet the information I can retrieve is just "The". 当我将value设置为$ {item.title}且title为例如“保镖”时,在servlet中,我可以检索到的信息仅为“ 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 谢谢

Check the generated HTML output (rightclick page in browser, choose View Source). 检查生成的HTML输出(在浏览器中右键单击页面,选择“查看源代码”)。 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). 是的,引号(注意突出显示的颜色的不同, bodyguard成了属性)。

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. 就是说,您的JDBC代码容易出现资源泄漏。 You should close all the resources Connection , Statement and ResultSet in the finally block of the try block you've acquired them. 您应该在已获取的try块的finally块中关闭所有资源ConnectionStatementResultSet 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. HTML在语法上也是无效的,但这可能只是一个复制粘贴错误,否则它将无法正常工作。

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 . 进一步,您的HTML表单被声明为使用GET的request方法,但也被声明为使用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 . 当您具有<input type="file"> 时才使用此enctype,如果是这种情况,则请求方法应为POST

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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