Hi Mark.
        I use the front controller and mvc patterns for pretty much all my
projects.
Its pretty simple once you understand what is going on.

        In regards to ur question.
        I'm assuming that this is ur page flow:

        Some intro page -- post --> Controller Servlet -- forward --> JSP

        In which case:
        Assume that the first page is a login with a field for username:
        <input type="text" name="username">

        It posts from the form to the controller servlet:

        <form action="controller" method="post">
                ...
                <input type="text" name="username">
                ...
        </form>


        in the UserBean assume that we have a field called username: private
String username;
        with appropriate get and set methods

        In the servlet we can do:
                UserBean user = new Userbean();
                user.setUserName( request.getParameter("username") );
        Then set that into the session scope:
                request.getSession().setAttribute("user", user);

        Now forward to the jsp:
                request.getRequestDispatcher("myjsp.jsp").forward( request,
response );

        In myjsp.jsp
        <jsp:useBean id="user" class="UserBean" scope="session"/>

        pretty straight forward so far, BUT! an error code occur here if
UserBean is in a package.
        remember that class="..." needs the full path of the bean class
        so mine might look like
        <jsp:useBean id="user" class="com.myproj.UserBean" scope="session"/>

        then just use the gets and sets as u like (via scriptlets or action
tags)
        ex:
        <jsp:getProperty name="user" property="username"/>

        Now if that doesnt work.. u should check in the Servlet that u are
getting something for username:
        ie:

                ...
                UserBean user = new Userbean();

                String username = request.getParameter("username");

                System.out.println("user was: " + username); //<<-- added
for debuggin

                user.setUserName( username );
                request.getSession().setAttribute("user", user);
                ...

Hope that helps.. if it doesnt.. then u might want to send the key sniplets
from the three players.
-Tim

-----Original Message-----
From: Mark Galbreath [mailto:[EMAIL PROTECTED]]
Sent: Friday, December 28, 2001 3:55 PM
To: [EMAIL PROTECTED]
Subject: Re: Last Question Today


Tim,

Do you put request.getSession().setAttribute( "user", userbean) in the JSP
or servlet after calling the bean's setter?  It doesn't work in the servlet;
still getting "NULL" returned to the JSP.  Does web.xml need to be modified
and JRUN restarted (I'm a bit fuzzy on when web.xml is required outside of
J2EE applications using EJBs)?

I am trying to model the behavior of the Front Controller pattern
(http://developer.java.sun.com/developer/restricted/patterns/FrontController
.html)
where the ServletFront servlet serves as the database gateway and passes
field values to a JavaBean ("UserBean" in my case) which acts as a Data
Access Object (DAO), accessible to any JSP within session scope, and then
returns control to the FrontController so the correct JSP can be dispatched.

I can't believe I cannot find any code examples to doing what should be a
very common usage of MVC using servlets.  And BTW, if you haven't looked at
the patterns associated with the above URL, the discussion is excellent.
But no code examples (not even in the Java library at
http://developer.java.sun.com/developer/codesamples/).

Thanks!
Mark

----- Original Message -----
From: "Chen, Gin" <[EMAIL PROTECTED]>
Sent: Friday, December 28, 2001 11:39 AM


> U need to add the userbean to the session scope within the servlet.
> This line:
> <jsp:useBean id="user" class="UserBean" scope="session" />
> is looking for userbean in the session scope.. since ur servlet doesnt put
> it there.. it decides to create a new one.
> I use this if i need a bean in a page that will set up a bean for the
> servlet.
> U should use:
>
> UserBean userbean = new UserBean();
> userbean.setSomething("something");
> request.getSession().setAttribute( "user", userbean );
>
> Common mistake.. i do it all the time.
> -Tim
>
> -----Original Message-----
> From: Mark Galbreath [mailto:[EMAIL PROTECTED]]
> Sent: Friday, December 28, 2001 11:42 AM
>
> I've never done this before and I can find no help in any of the Sun
> tutorials.  I'm trying to set the properties in a Bean with a servlet and
> get those properties with a JSP.  I believe I am doing everything
correctly,
> but I'm getting NULLs (no NullPointerExceptions, I'm getting "NULL" as
HTML)
> on the JSP as returns for the Bean's getters.
>
> The Bean DECLARES the properties:
>
> public class UserBean implements serializable {
>     private String something;
>
>     public String getSomething() {
>         return something;
>     }
>     public void setSomething( String thing) {
>         this.something = thing;
>     }
>     ...
> }
>
> The servlet SETS the properties:
>
> ...
> UserBean userbean = new UserBean();
> userbean.setSomething( String string);
> ...
>
> And the JSP GETS the properties:
>
> ...
> <jsp:useBean id="user" class="UserBean" scope="session" />
> <jsp:getProperty name="user" property="something" />
> ...
>
> Pretty straightforward, right?  Why am I getting "NULL" in the JSP instead
> of the string set by the servlet?  Am I missing something in the JSP
> getProperty?  Is the servlet setting the properties correctly?  One
> additional point, the code helper (class/method dot popups) in Forte does
> not display any of UserBean's methods when I type "userbean." (userbean +
> period).

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to