How can I write html or JSTL tags in Java Scriplets?

Hello People,
How can I write html or JSTL tags in Java Scriplets? I have done a sql query using JSTL tags now I want to have assign the results to string in java scriplets as show below but I am getting an error. Please how can I achieve this? How can I solve this problem? Please help.

CODE
<sql:setDataSource var=“datasource”
driver=“com.mysql.jdbc.Driver”
url=“jdbc:mysql://localhost/publish”
user=“publishuser” password=“secret”/>

<sql:query var=“books” dataSource=“${datasource}”>
SELECT id, title, price FROM book
</sql:query>
<c:forEach items=“${books.rows}” var=“row”>

SCRIPLETS

<%
String id =c:out value=${row.id};
String title =c:out value=${row.title};
String price =c:out value=${row.price};

%>
</c:forEach>

Well I wouldn’t really recommend this but it will probably work…

You do a:

 <c:set var="theId" value="${row.id}"/>

Then later in your code you should be able to read it off the page context Map with:


String id = (String) pageContext.getAttribute("theId");

It will probably work, but there is probably a nicer solution to this problem (maybe one that doesn’t involve accessing the database from a JSP page…)

The error I am getting is as follows:
An error occurred at line: 46 in the jsp file: /Income/hook/Status/tatus2.jsp
Syntax error on token “$”, delete this token

Please give an example of connecting to the database if possible and then use String id = (String) row.get(“id”); I I am trying something like below and but is not working

DAO dcon=new DAO();
ResultSet searchView=dcon.GetTransaction();

//request.setAttribute(“searchResults”,searchView);
ResultSet results=(ResultSet)request.getAttribute(searchView);
while(results.next())

And the error message is … ?

I think you’ve missed the point of what a scriptlet is. Anything inside the <% %> is considered Java and will be compiled as Java. So <c : out /> won’t compile since it’s not Java.

I would avoid using scriptlets altogether and set the variables using <c : set />. You can search <c : set /> on google for proper usage. Or if you are going to use scriptlets, you need to do get the row variable from the page scope, and I’m not even sure what type of Object that is, which would then have an id attribute. Let’s say it’s a Map, but it might not be, if it’s not you’ll get a ClassCastException:


Map row = (Map) pageContext.getAttribute("row");

String id = (String) row.get("id");
// same for the others

Hopefully this shows why you shouldn’t use scriptlets.