JSP java代码+ html表格

发布于 2024-10-07 16:49:05 字数 1020 浏览 4 评论 0原文

我遇到了一个我不太明白的问题。我使用java代码访问数据库,然后使用jsp部分,以html格式显示结果。 代码如下所示:

out.println("<html><body>");

out.println("<form method=\"GET\" action=\"http://localhost:1234/WebLabJSP/delete\">");

out.println("<table>");

out.println("<tr><th>Name</th><th>Quantity</th><th>Category</th></tr>");

ResultSet rs = s.executeQuery("select * from Products");


while(rs.next()){


out.println("<tr><td>" + rs.getString(2) + "</td><td>" + rs.getString(3) + "</td><td>" + rs.getString(4) + "</td><td><input type=\"submit\" value=\"Delete\" name=\"delete" + rs.getString(1) + "\"/></td><td><input type=\"submit\" value=\"Update\" name=\"update" + rs.getString(1) + "\"/></td></tr>");

   } 

out.println("</table></form>");

out.println("</body></html>");

问题是,如果我在表单中只有删除按钮,它可以正常工作,但是当我添加第二个按钮时,页面根本不显示任何内容。 如果有人知道发生了什么事,请帮忙。

I have encountered a problem that I don't really understand. I'm using java code to access a database and then using the jsp part, display in html format the result.
The code looks like this:

out.println("<html><body>");

out.println("<form method=\"GET\" action=\"http://localhost:1234/WebLabJSP/delete\">");

out.println("<table>");

out.println("<tr><th>Name</th><th>Quantity</th><th>Category</th></tr>");

ResultSet rs = s.executeQuery("select * from Products");


while(rs.next()){


out.println("<tr><td>" + rs.getString(2) + "</td><td>" + rs.getString(3) + "</td><td>" + rs.getString(4) + "</td><td><input type=\"submit\" value=\"Delete\" name=\"delete" + rs.getString(1) + "\"/></td><td><input type=\"submit\" value=\"Update\" name=\"update" + rs.getString(1) + "\"/></td></tr>");

   } 

out.println("</table></form>");

out.println("</body></html>");

The problem is, that if I have only the delete button in the form, it works perfectly, but when I added the second button, the page didn't display anything at all.
If anyone knows what's going on, please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

负佳期 2024-10-14 16:49:05

如果您得到一个空白页面,那么这通常是因为在呈现响应的过程中引发了异常,并且 servlet 容器无法显示带有异常详细信息的标准错误页面,因为响应已经提交。

异常将记录到服务器日志文件中。查找它,读取堆栈跟踪并相应地解决问题。


与实际问题无关,您所展示的代码实践实际上是一种糟糕的实践。 Java 代码应该放在 Java 类中(由 servlet 控制),HTML 代码应该放在 JSP 中,而不使用 out.println()。应该使用 JSTL 来控制页面流。另请参阅此答案了解示例。这样,异常将/可以被及时捕获并最终出现在错误页面中。

If you get a blank page, then this is often caused because an exception is been thrown in midst of rendering the response and the servletcontainer is unable to display the standard error page with exception details, because the response is already committed.

The exception will be logged to server log file. Lookup it, read the stacktrace and fix the problem accordingly.


Unrelated to the actual problem, the code practice you're showing is actually a poor practice. Java code should go in a Java class (controlled by a servlet) and HTML code should go in JSP without utilizing out.println(). JSTL should be used to control the page flow. See also this answer for an example. This way exceptions will/can be caught timely and end up in an error page.

十秒萌定你 2024-10-14 16:49:05

首先这不是你应该如何使用jsp。
在jsp上使用jstl,在servlet上使用java代码。

关于您的问题,请尝试检查生成的 HTML。它应该有

<form ...>
.
.
.
.<input type="submit" name="something" value="delete"/>
.<input type="submit" name="something" value="update"/>
</form>

或检查服务器的日志,

另请参阅

First of all this is not how you should use jsp.
On jsp use jstl on servlet use java code.

and about your issue try checking generated HTML. It should have

<form ...>
.
.
.
.<input type="submit" name="something" value="delete"/>
.<input type="submit" name="something" value="update"/>
</form>

Or check for server's log,

See also

陌上芳菲 2024-10-14 16:49:05

用 try/catch 块包围 :

ResultSet rs = s.executeQuery("select * from Products");
while(rs.next()){
out.println("<tr><td>" + rs.getString(2) + "</td><td>" + rs.getString(3) + "</td><td>" + rs.getString(4) + "</td><td><input type=\"submit\" value=\"Delete\" name=\"delete" + rs.getString(1) + "\"/></td><td><input type=\"submit\" value=\"Update\" name=\"update" + rs.getString(1) + "\"/></td></tr>");
   } 

以查看是否存在异常。

Surround your :

ResultSet rs = s.executeQuery("select * from Products");
while(rs.next()){
out.println("<tr><td>" + rs.getString(2) + "</td><td>" + rs.getString(3) + "</td><td>" + rs.getString(4) + "</td><td><input type=\"submit\" value=\"Delete\" name=\"delete" + rs.getString(1) + "\"/></td><td><input type=\"submit\" value=\"Update\" name=\"update" + rs.getString(1) + "\"/></td></tr>");
   } 

with a try/catch block to see if there is an Exception.

脱离于你 2024-10-14 16:49:05

不要在jsp页面中使用连接过程和其他更新过程,尝试使用servlet或其他javabean。使用该 servlet 的标记...如果您想更新,则只需调用该 servlet 内声明的函数即可。 servlet 充当 jsp 页面的控制器。它的功能更强大。

Don't use connection process and other updating process in jsp page, try to use servlets or other javabeans. tag to use that servlets... if u want to update means simply cal that function, declared inside that servlet. servlet s act as a controller of ur jsp page.its more powerful.

冷血 2024-10-14 16:49:05

为什么不把变量放在request中

request.setAttribute("string1",rs.getString(1));

然后在jsp中调用它

<td><%=request.getParameter("string1")%>;

Why dont you put the variables in request

request.setAttribute("string1",rs.getString(1));

then call it in your jsp

<td><%=request.getParameter("string1")%>;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文