1

I'm working on jsp,servlet code . I have LESSON table in my db too which has lesson_id , lesson_name and etc.

Now I want to use "a" tag in my jsp page which link to a servlet page but each tag used in jsp should has a specific lesson_id which has token from my database.

for example data structure has a special id. so if i want to use many "a" tag in my jsp for each lesson that has a specific id, how can i do this?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
hdiz
  • 1,141
  • 2
  • 13
  • 27

1 Answers1

0

Yes, You can use JSTL & EL. For database access use JSTL SQL Tag library.

JSP: (sample code:)

<%@ 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>

Note: It's better to move the database code in the Servlet. Here is an example

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • glad to know it. If the issue is resolved then close the thread. [visit](http://stackoverflow.com/tour) – Braj Aug 01 '14 at 15:07
  • 1
    JSTL SQL and XML taglibs are for prototyping only. Don't use them in production applications. See also 1st paragraph on the Java EE tutorial you linked. – BalusC Mar 20 '16 at 18:52
  • @BalusC I have already added a note for the same to move the database logic on server side with working example. Thanks for making it more clear. – Braj Mar 21 '16 at 08:52