Servlet,JSP,JavaBeans和HTML表单 [英] Servlet, JSP, JavaBeans and HTML form

查看:82
本文介绍了Servlet,JSP,JavaBeans和HTML表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究与数据库建立连接的servlet,它获取表之一的信息并将此信息发送到jsp文件.该文件将打印在浏览器的信息表上,并添加单选按钮,使我们可以选择其中的一行.

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.

该servlet看起来像这样:

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);

在JSP文件中,我可以打印带有信息的表,添加用户将用来选择1行并将单选按钮使用我可以添加的表单发送到servlet的单选按钮:

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>

在值"字段中,我应该能够将$ {item.title}中存储的信息发送到servlet.当我将value设置为$ {item.title}且title为例如保镖"时,在servlet中,我可以检索到的信息仅为"The".它看起来像发送位于字符串的第一个空格之前的字符.我怎么能得到整个字符串?

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?

谢谢

推荐答案

检查生成的HTML输出(在浏览器中右键单击页面,选择查看源代码").你没想念什么吗?

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>

是的,引号(请注意突出显示颜色的差异,bodyguard成为属性).

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

因此,对其进行修复:

<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? :)

也就是说,您的JDBC代码容易出现资源泄漏.您应该在已获取的try块的finally块中关闭所有资源ConnectionStatementResultSet.有关更多提示,请参见本文.同样,列表不一定需要放在会话范围内. HTML在语法上也是无效的,但这可能只是一个复制粘贴错误,否则它将无法正常工作.

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.

此外,您的HTML表单被声明为使用GET的请求方法,但也被声明为使用multipart/form-data的编码类型.这完全没有道理. 在您拥有<input type="file">时使用此enctype,如果是这种情况,则请求方法应为POST.

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.

这篇关于Servlet,JSP,JavaBeans和HTML表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆