我正在编写jsp,servlet代码。我的数据库中也有LESSON表,其中有lesson_id、lesson_name等。
现在,我想在jsp页面中使用"a"标记,它链接到servlet页面,但是jsp中使用的每个标记都应该有一个特定的lesson_id,其中包含来自我的数据库的令牌。
例如,数据结构有一个特殊的id。因此,如果我想在我的jsp中为每个具有特定id的课程使用许多"a"标记,我如何做到这一点?
发布于 2014-08-01 14:46:48
是的,你可以使用JSTL & EL。对于数据库访问,请使用JSTL SQL标记库。
JSP:(示例代码:)
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<sql:setDataSource var="webappDataSource"
driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/test"
user="root" password="root" />
<sql:query dataSource="${webappDataSource}"
sql="select lesson_id, lesson_name from lessons" var="result" />
<table width="100%" border="1">
<c:forEach var="row" items="${result.rows}">
<tr>
<td>${row.lesson_id}</td>
<td>${row.lesson_name}</td>
<td>
<a href="${pageContext.servletContext.contextPath}/servletURL/${row.lesson_id}">
click here
</a>
</td>
</tr>
</c:forEach>
</table>注意:最好在Servlet中移动数据库代码。这是一个示例
https://stackoverflow.com/questions/25082774
复制相似问题