How to assign resultset values to jsp variable?
I ran a sql query, then I just want to assign the num field from resultset to a java variable, so that I can evaluate if num==1 or not, but i just don't know the linkage between C tab lib, expression language and variable...
Can anyone help?
Below are segments of my codes but errors.
...
<sql:query var="items" dataSource="jdbc/Trade18">
SELECT count(*) AS num FROM admin WHERE username='<% out.print(username); %>' AND password='<% out.print(password); %>'
</sql:query>
<c:forEach var="row" items="${items.rows}">
<%// String num = ${row.num}; %>
<c:set var="num" value="${row.num}"/>
</c:forEach>
...
<%
if(num==1){
out.println("Passed");
}else{
out.println("Failed");
}
...
[848 byte] By [
roamera] at [2007-11-26 15:53:33]

# 1
JSTL/EL expressions are stored in attributes - by default as pageContext attributes, though you can specify request, session or application scope if you wish.
Thus you can get hold of them in java code via
String num = (String)pageContext.findAttribute("num");
However scriptlet code is a bad idea.
The equivalent JSTL code to that if statement:
<c:choose>
<c:when test="${num == 1}">
Passed
</c:when>
<c:otherwise>
Failed
</c:otherwise>
</c:choose>
Your query also seems confused.
Where do the values "username" and "password" come from?
I have rewritten it below assuming the values are coming from request parameters.
<sql:query var="items" dataSource="jdbc/Trade18">
SELECT count(*) AS num FROM admin
WHERE username=? AND password=?
<sql:param value="${param.username}"/>
<sql:param value="${param.password}"/>
</sql:query>
<c:forEach var="row" items="${items.rows}">
<c:set var="num" value="${row.num}"/>
</c:forEach>
Cheers,
evnafets
# 2
Thx much.For this code, I got this error:String numb = (String) pageContext.findAttribute("num");java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.StringHow can I convert the type from Long to String?
# 3
String numb = pageContext.findAttribute("num").toString();
# 4
thank you.How about casting it to Integer value? I found that replacing toString() TO toInt() didn't work. Is there any other method?^^
# 5
int numb = Integer.parseInt(pageContext.findAttribute("num").toString());
# 6
Integer num = java.lang.Integer.parseInt(pageContext.findAttribute("num").toString());I used this, and I found that I don't have to add this line in top of my page, it is already ready to use.<%@ page language="java" import="java.lang.*" %>
# 7
i think java.lang package is imported by default
# 8
In the login.jsp page, when user is not authenicated,
<jsp:useBean id="userdata" class="user.UserData" scope="session"/>
<jsp:setProperty name="userdata" property="message" value="Your login is incorrect"/>
<jsp:forward page="./index.jsp"/>
Then in index.jsp page,
<jsp:useBean id="userdata" class="user.UserData" scope="session"/>
<jsp:getProperty name="userdata" property="message"/>
<jsp:setProperty name="userdata" property="message" value=""/>
When user failed to login, they will be redirected to index.jsp, with a message "Your login is incorrect". But when I refreshed the page again, the message is STILL HERE. I would like to ask I already set that property back to empty, and why it STILL APPEARS?
# 9
ummm.. probably your scope affects it.. change the scope to request
# 10
I tried used scope="request" for both pages, but the result is still the same. I still get "your login is incorrect" after refreshing my page.
How can I clear the bean property value?
My UserData.java file is:
package user;
public class UserData {
String username;
String email;
Boolean authenicated = false;
String message;
public void setUsername( String value )
{
username = value;
}
public void setEmail( String value )
{
email = value;
}
public void setAge( int value )
{
age = value;
}
public void setAuthenicated(Boolean value){
authenicated = value;
}
public void setMessage(String value){
message = value;
}
public String getUsername() { return username; }
public String getEmail() { return email; }
public int getAge() { return age; }
public String getMessage(){
return message;
}
}
-
# 11
change this line:<jsp:forward page="./index.jsp"/>to <%response.sendRedirect("../index.jsp")%>
# 12
Done, thx for suggestion.But the scope has to change back to session, what are jsp:forward and response.redirect() difference? Does forward just 'include' in current page rather than 'jump' to others?
# 13
both jump to other pages, but the difference is jsp:forward takes the request to the next page while response.sendRedirect makes new request, so the previous request is cleared out