JSPServletとDB接続テスト4

Java

tagファイルで接続する。

1)test2.jsp(テストファイル)の作成。
2)te.tag(テストファイル)の作成。
3)warファイルの作成。
5)Tomcatの起動。
6)ブラウザの立ち上げ後、URL(http://localhost:8080/warファイル名/test3.jsp)の入力で、データが表示されればOK。

test3.jspファイル


<%@ page contentType="text/html; charset=Windows-31J" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@ taglib prefix="test" tagdir="/WEB-INF/tags" %>

<html>
<head>
<title>データを表示する2</title>
</head>
<body>
<h1>結果2</h1>
<test:te>
<sql:query var="test" dataSource="${ds}">
SELECT * FROM us
</sql:query>
</test:te>

<c:forEach var="row" items="${test.rows}">
<table>
<tr>
<td><c:out value="${row.id}" /></td>
<td><c:out value="${row.name}" /></td>
</tr>
</table>
</c:forEach>

</body>
</html>

te.tagファイル

<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@ variable name-given="ds" %>
<sql:setDataSource url="jdbc:mysql://localhost/testdb?
  useUnicode=true&characterEncoding=SHIFT_JIS"
driver="com.mysql.jdbc.Driver"
user="ユーザー名" password="パスワード" var="ds"/>

<jsp:doBody/>

※2005,6年?の昔の記事

スポンサーリンク

コメント

タイトルとURLをコピーしました