Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

jsp:useBean question

Status
Not open for further replies.

BStopp

Programmer
Dec 29, 2003
29
US
When using <jsp:useBean> tag the first step the application server does is search the scope you specify for the appropriate bean. What exactly does it search in the scope? I mean if i specify:

Code:
<jsp:useBean id="myBean" scope="request" class="myPackage.myBeanClass">

Does it search the attributes of the request object (ie those able to be found listed under "reqest.getAttribute()") or does it search some other location? If it is under those attributes found via the getAttribute method of the request object, does the 'id' attribute of the jsp:useBean tag need to be the name of the attribute that was attached to the request.

Or am i in the completely wrong mindset on how to do this?

B
 
I'm a newb when it comes to JSP (actually, I'm currently taking a couple classes on it in college). But from what I understand, the 'id' attribute is a name that you make up that is used to identify the bean. I basically understand it to be just like any other HTML tag with an 'id' property.

As for the getAttribute() question, I'm not entirely sure of that answer, myself, since I've only had a couple of days of study on it. If I find an answer on this, I'll let you know.

Nick Ruiz
Webmaster, DBA
 
Here's some more info, may help you all to understand what I am trying to do:

Code:
<%@ page contentType="text/html" %>
<%@ page import="sod.*" %>
<%@ taglib prefix="c" uri="[URL unfurl="true"]http://java.sun.com/jsp/jstl/core"[/URL]  %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>                        if (dataBean.getloginStatus() == "false")
<SCRIPT LANGUAGE="JavaScript" type="text/javascript">

function checksubmit() {
    if (document.login.userid.value == "") {
            alert("Please enter a User ID.")
            document.login.userid.focus()
            return false
        }
    if (document.login.password.value == "") {
            alert("Please enter a password.")
            document.login.password.focus()
            return false
        }

}
</script>

<HEAD>
    <title>QC_SOD Log In Page</title>
</HEAD>
    <BODY> 
        <FORM METHOD=post name="login" onSubmit="return checksubmit()" ACTION="/qcsodgui/sodAuth">
            <TABLE cellSpacing=0 cellPadding=3 width=400 border=0 ALIGN="CENTER">
                <TR>
                    <TD width=100>User ID:</TD>
                    <TD><INPUT id=userid name=userid></TD>
                </TR>
                <TR>
                    <TD width=100>Password:</TD>
                    <TD><INPUT id=password type=password name=password></TD>
                </TR>
                <TR>
                    <TD width=100>&nbsp;</TD>
                    <TD><INPUT type=submit value=Submit name=Submit></TD>
                </TR>
                    <jsp:useBean id="dataBean" scope="request" class="sod.sodDataBean">
                        <jsp:setProperty name="dataBean" property="loginStatus" value="empty" />
                    </jsp:useBean>
                    <c:out value="${dataBean.loginStatus}" />
                    <c:if test="${dataBean.loginStatus == 'true'}">
                            <TR>
                            <TD COLSPAN=2><B>Login Authorization Failed, Please Try Again</B></TD>
                            </TR>
                    </c:if>
                    <%  if (pageContext.findAttribute("dataBean") != null)
                        {
                        sodDataBean myBean = (sodDataBean) request.getAttribute("dataBean");
                        if (dataBean.getLoginStatus() == "false")
                        { %><TR><TD COLSPAN=2><B>Login Authorization Failed, Please Try Again</B></TD></TR>
                    <% }} %>


            </TABLE>
        </FORM>
    </BODY>
</HTML>

The sodDataBean only has one attribute (loginStatus) and two methods (getLoginStatus() and setLoginStatus()). It public and implements Serializable...

The jsp page is supposed just the two imput boxes the first time they arrive at it. And that works OK. When they type in a user name and password, it submits to a servlet... The servlet creates an instance of this bean and then tries to connect to the database using the id and password provided. This fails every time on purpose because i want to get this page's code working. When the DB connection fails, it does the following:

Set the bean's property of loginStatus to "false"
Add the bean as an attribute of the request
Redirect the client via server-side redirection back to the login page.

At this point, the login page has a request object with an attribute ("dataBean") that is of type sodDataBean. When doing the jsp:useBean to grab this attribute, nothing seems to be happening. All of the JSTL tags (c:if, c:set, c:eek:ut, etc) don't seem to be working. Which is odd because the scriptlet above between the <% %> tags works perfectly.

When i try to see what the value of the bean's property is by using the following code:

Code:
 <c:out value="${dataBean.loginStatus}" />
or
Code:
 <c:out value="${dataBean.getLoginStatus()}" />

or even using c:set to set a variable to the two expressions shown above, nothing comes out of the c:eek:ut except the actual litteral expression. It almost seems like the c:eek:ut tag isn't evaluating the expression.

I don't understand what the problem i'm encountering is. Is anyone able to help me?

B
 
For some clarification, there are two errors in the code of my last post..

The line that says:

Code:
 <HTML>                        if (dataBean.getloginStatus() == "false")

Ignore everything but the <HTML> tag.

and the line that says

Code:
  <c:if test="${dataBean.loginStatus == 'true'}">

Yes it says test for it to say 'true' but that was just me trying something. If I change it to 'false' it still does not work properly.

B
 
Try :

Code:
<HTML>                        

<% if (dataBean.getloginStatus() == "false") { %>
<SCRIPT LANGUAGE="JavaScript" type="text/javascript">

function checksubmit() {
    if (document.login.userid.value == "") {
            alert("Please enter a User ID.")
            document.login.userid.focus()
            return false
        }
    if (document.login.password.value == "") {
            alert("Please enter a password.")
            document.login.password.focus()
            return false
        }

}
</script>

<% } %>

--------------------------------------------------
Free Database Connection Pooling Software
 
Thanks for the help sedj but one of my co-workers found the problem. I was missing this:

Code:
 <%@ page isELIgnored="false" %>

Apparently my default value for ignoring the expression language is set to TRUE, therefore it wasn't doing the evaluation, it was printing litterals.
 
That can't be the only error, as this line :

<HTML> if (dataBean.getloginStatus() == "false")

will be rendered as HTML - the Java code (the 'if' statement) would not be evaluated as there is no scriptlet tag ...

--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top