KPax
asked on
JSTL result.rowCount
Here is code which should work (part of main page which takes care about login, I know this shouldn't be done from JSP/JSTL this is just one example from book I am reading):
Quick check in DB (hsqldb) shows that there is indeed one user properly populated with username and password and RSS i.e. result.rowCount should be greater than zero, but it isn't.
I added <c:otherwise> option when trying to debug what's wrong.
What could be wrong here?
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>
<sql:query var="result">
select * from users where user=? and password=?
<sql:param value="${param.user}" />
<sql:param value="${param.pw}" />
</sql:query>
<c:choose>
<c:when test="${result.rowCount > 0}">
<c:set var="user" scope="session" value="${param.user}" />
<c:set var="rss" scope="session" value="${result.rows[0].RSS}" />
</c:when>
<c:otherwise>
<c:set var="failedLogin" scope="request" value="true"/>
<c:set var="user" value="${result.rows[0].user}" />
<c:set var="pw" value="${result.rows[0].pw}" />
<c:set var="rowCount" value="${result.rowCount}" />
<c:set var="resultSQL" value="${result}" />
<jsp:forward page="failed.jsp" />
</c:otherwise>
</c:choose>
<jsp:forward page="dynamicPortal.jsp" />
Quick check in DB (hsqldb) shows that there is indeed one user properly populated with username and password and RSS i.e. result.rowCount should be greater than zero, but it isn't.
I added <c:otherwise> option when trying to debug what's wrong.
What could be wrong here?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
failed login again...
also I tried adding one more user - failed login again
also I tried adding one more user - failed login again
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
If I try to do:
<c:out value="Row Count: ${param.rowCount}"></c:out> <br />
there's no output at all.
ASKER
in failed.jsp I tried to do this:
This is all I get:
Failed credentials were:
Username: nenad
Password: pass12345
Row Count:
resultSQL:
count:
Name Pass
where
Username: nenad
Password: pass12345
match DB record
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<c:out value="Failed credentials were:"></c:out><br />
<c:out value="Username: ${param.user}"></c:out> <br />
<c:out value="Password: ${param.pw}"></c:out> <br />
<c:out value="Row Count: ${param.rowCount}"></c:out> <br />
<c:out value="resultSQL: ${param.resultSQL}"></c:out> <br />
<c:out value="count: ${param.count}"></c:out> <br />
<table>
<tr>
<th>Name</th>
<th>Pass</th>
</tr>
<c:forEach var="row" items="${resultSQL.rows}">
<tr>
<td>
<c:out value="${row.user}"/>
</td>
<td><c:out value="${row.pw}"/></td>
</tr>
</c:forEach>
</table>
This is all I get:
Failed credentials were:
Username: nenad
Password: pass12345
Row Count:
resultSQL:
count:
Name Pass
where
Username: nenad
Password: pass12345
match DB record
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
it hits DB, here is result if I change to 'foo' (just as expected):
HTTP Status 500 - javax.servlet.ServletException: javax.servlet.jsp.JspException:
type Exception report
message javax.servlet.ServletException: javax.servlet.jsp.JspException:
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: javax.servlet.ServletException: javax.servlet.jsp.JspException:
select * from foo where user=? and password=?
: user lacks privilege or object not found: FOO in statement [
select * from foo where user=? and password=?
]
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:592)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:462)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
javax.servlet.ServletException: javax.servlet.jsp.JspException:
select * from foo where user=? and password=?
: user lacks privilege or object not found: FOO in statement [
select * from foo where user=? and password=?
]
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I believe you are aware that what you wrote will produce plain HTML output so:
Then we will have output like this:
user is ${param.user}
pw is ${param.pw}
will display just that. On the other hand if I use <c:out> like this:user is: <c:out value="${param.user}" /> <br />
pw is: <c:out value="${param.pw}" /> <br />
<sql:query var="result">
select * from users where user=? and password=?
<sql:param value="${param.user}" />
<sql:param value="${param.pw}" />
</sql:query>
user0 is <c:out value="${result.rows[0].user}" /> <br />
user0 is <c:out value="${result.rows[0].pw}" /> <br />
user0 is <c:out value="${result.rows[0].RSS}" /> <br />
Then we will have output like this:
user is: nenad
pw is: pass12345
user0 is
user0 is
user0 is
ASKER
Also, this will display no data:
<table border="1" width="100%">
<tr>
<th>Username</th>
<th>Pass</th>
<th>RSS</th>
</tr>
<c:forEach var="row" items="${result.rows}">
<tr>
<td><c:out value="${row.user}" /></td>
<td><c:out value="${row.pw}" /></td>
<td><c:out value="${row.RSS}" /></td>
</tr>
</c:forEach>
</table>
ASKER
BTW DB works OK, I have several more tables, for user posts, DB keeps records and I can read and display them, also there is visitors counter which works OK, here is code:
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>
<sql:transaction>
<sql:update>
update counter set counter = counter + 1
</sql:update>
<sql:query var="result">
select * from counter
</sql:query>
<c:set var="count" value="${result.rows[0].counter}" />
</sql:transaction>
<p>
This site has been accessed
<b><c:out value="${count}" /></b>
times!
</p>
<p>
<a target="_blank"
href="<c:url value="/chapter12/viewMessages.jsp">
<c:param name="messageBoard" value="3"/>
</c:url>">
Discuss this counter
</a>
</p>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Which version of Tomcat are you using? EL has been allowed in template text for a long time.
ASKER
Apache Tomcat 7.0.70 Server
Tomcat version 7.0 implements the Servlet 3.0. Your web app's web.xml file should look like
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<!-- Config here. -->
</web-app>
ASKER
Because it is correct solution. Thanks everyone for help.
ASKER
Open in new window
and on failed.jsp I can c:out them with:
Open in new window