如何实现来自 java servlet 的图像幻灯片放映,而不需要在 html 中对图像进行硬编码?
作为学习练习,我改编了 BalusC 编写的一系列 Java servlet 来存储上传到 mysql 数据库中的图像列表。该应用程序运行良好,并按照从数据库静态读取的顺序以 html 形式显示图像列表。然而,我正在尝试调整 html 以在幻灯片中动态显示这些内容。我在互联网上找到了几个示例,但它们似乎都在浏览器代码中硬编码了图像名称。我正在寻找某种方法将从数据库查询读取的图像传输到客户端中的幻灯片。
研究了几个例子后,似乎使用 javascript 是最常见的这样做的方法,但我找不到一个动态执行此操作的示例,这是我的尝试(我在 java servlet 中使用嵌入了 html 的 javascript),但它无法执行更多操作,无法连续打印出图像的标题页面上的列表(但没有图像):
try {
Class.forName(driver);
Connection con = DriverManager.getConnection(url+dbName, userName, password);
PreparedStatement ps = con.prepareStatement("select * from photos");
ResultSet rs = ps.executeQuery();
out.println("<h1>Photos</h1>");
while ( rs.next()) {
out.println("<h4>" + rs.getString("title") + "</h4>");
out.println("<script type=\"text/javascript\">");
out.println(" function clearScreen(){");
out.println(" document.image.src=displayphoto?id=\" + rs.getString(\"id\")\"");
out.println(" document.body.innerHTML=\"\";");
out.println(" setTimeout('clearScreen()','5000');}");
out.println("</script>");
}
con.close();
}
任何关于更好的方法的指示或建议,甚至纯粹使用 Java servlet,都将不胜感激。
非常感谢
阿拉斯泰尔
As a learning exercise I have adapted a series of Java servlets written by BalusC to store a list of images uploaded in a mysql database. The application works fine and displays a list of the images in the sequence they are read from the database statically in html. Howver, I am trying to adapt the html to display these dynamically in a slideshow. I have found several examples on the internet but all of them seem to hard-code the names of images in the browser code. I'm looking for some way of transfering the images read from the database query to a slideshow in the client.
After having researched several examples it seems that using javascript is the most common way for doing it, but I cannot find an example which does it dynamically here is my attempt (I am using javascript embedded with html within a java servlet) at it which fails to do more that print out the titles of the images in a consecutive list on the page (but no image):
try { Class.forName(driver); Connection con = DriverManager.getConnection(url+dbName, userName, password); PreparedStatement ps = con.prepareStatement("select * from photos"); ResultSet rs = ps.executeQuery(); out.println("<h1>Photos</h1>"); while ( rs.next()) { out.println("<h4>" + rs.getString("title") + "</h4>"); out.println("<script type=\"text/javascript\">"); out.println(" function clearScreen(){"); out.println(" document.image.src=displayphoto?id=\" + rs.getString(\"id\")\""); out.println(" document.body.innerHTML=\"\";"); out.println(" setTimeout('clearScreen()','5000');}"); out.println("</script>"); } con.close(); }
Any pointers or suggestions on a better way of doing this, even purely with Java servlets would be greatly appreciated.
Many thanks
Alastair
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此代码生成一个
块,为表中找到的每张照片定义相同的
clearScreen
函数(但在每次迭代时使用不同的照片 ID)。目前还不清楚你想做什么。要进行幻灯片放映,您必须迭代图片 ID 列表。您可以将此 ID 列表存储
如果你的目标是实现第一个解决方案,那确实不难。以下是在 JavaScript 中声明包含三个 ID(id1、id2 和 id3)的数组的方式:
因此,您所要做的就是从包含
的
、List
生成上述字符串。 “id1”“id2”
和“id3”
。一个简单的循环就可以了:This code generates a
<script>
block defining the sameclearScreen
function for each photo found in the table (but with a different photo ID at each iteration).It's not very clear what you're trying to do. To do a slideshow, you have to iterate over a list of picture IDs. You can store this list of IDs
If your goal is to implement the first solution, it's really not difficult. Here is how an array of three IDs (id1, id2 and id3) is declared in JavaScript:
So, all you have to do is to generate the above String frm a
List<String>
containing"id1"
,"id2"
and"id3"
. A simple loop will do: