You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Dahnke, Eric" <ED...@nextsource.com> on 2002/02/12 04:00:27 UTC

Sharing a bean between servlet and jsp. Arrggghhhhh

Now I'm going crazy. This can't be so hard. Please help... I can't figure
this out. I'm trying to share a bean between a jsp page and servlet.
Argghhhhhhhhh... Please.... I help on other lists... and donate time to
charity. I've been to Barnes and Noble and looked at a heap of texts. I've
done no less than 500 Google searches.


There are no errors, I just can't get this "<jsp:getProperty
name="myFormBean" property="userName" />" goddamned thing to display the
userName bean property if fb.validate() fails. Everything works. I've got
this begugger and can see the userName variable throughout the POST process.
Everything's cool until the rd.forward (request, response), but once the
forward takes place my jsp cannot again pick up the bean properties
eventhough I say <jsp:useBean id="myFormBean" class="beans.FormBean"
scope="session"/> I've tried every scope, and am using beans with straight
JSP no problem (same application).

Any ideas? Millions of Thanks


Here's the JSP page.
===============
<jsp:useBean id="myFormBean" class="beans.FormBean" scope="session"/>
<form method="POST" action="controller">
<input type="text" name="userName">
<jsp:getProperty name="myFormBean" property="userName" />
<input type="hidden" name="event" value="FORM_TEST">
<input type="submit" name="submit" value="next">
</form>

Here's the two relavant parts of the servlet.
================================
public void process (ServletContext sc, HttpServletRequest request,
                      HttpServletResponse response)
                      throws IOException, ServletException {

        FormBean fb = new FormBean();
        fb.setUserName(request.getParameter("userName"));

        if (fb.validate()) {
                URL = "index.jsp";
        } else {
                // go back
                URL = "signup1.jsp";
        }
}
public void forward (HttpServletRequest request,
        HttpServletResponse response) throws IOException,
        ServletException {

        RequestDispatcher rd = request.getRequestDispatcher(URL);
        rd.forward (request, response);
}

Here's the bean
===========
package beans;
public class FormBean implements java.io.Serializable {
        public String userName;
        public FormBean() {}
        public boolean validate() {
                boolean allOk=false;
                return allOk;
        }

        public String getUserName() {
                //return this.userName; // doesn't work either
                return userName;
        }
        public void setUserName(String uname) {
                this.userName = uname;
        }
}

--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: Sharing a bean between servlet and jsp. Arrggghhhhh

Posted by Nikola Milutinovic <Ni...@ev.co.yu>.
> Now I'm going crazy. This can't be so hard. Please help... I can't figure
> this out. I'm trying to share a bean between a jsp page and servlet.
> Argghhhhhhhhh... Please.... I help on other lists... and donate time to
> charity. I've been to Barnes and Noble and looked at a heap of texts. I've
> done no less than 500 Google searches.

Maybe you're not looking for the right thing...?

> There are no errors, I just can't get this "<jsp:getProperty
> name="myFormBean" property="userName" />" goddamned thing to display the
> userName bean property if fb.validate() fails. Everything works. I've got
> this begugger and can see the userName variable throughout the POST process.
> Everything's cool until the rd.forward (request, response), but once the
> forward takes place my jsp cannot again pick up the bean properties
> eventhough I say <jsp:useBean id="myFormBean" class="beans.FormBean"
> scope="session"/> I've tried every scope, and am using beans with straight
> JSP no problem (same application).

So far, I've been passing beans from JSP to a Servlet. I simply setup a bean in the "request" context (it attaches to the request object) and then forward to servlet. Servlet extracts it form the reqest with

bean = (OrgUnitBean)request.getAttribute( "bean" );

> Any ideas? Millions of Thanks
> 
> 
> Here's the JSP page.
> ===============
> <jsp:useBean id="myFormBean" class="beans.FormBean" scope="session"/>
> <form method="POST" action="controller">
> <input type="text" name="userName">
> <jsp:getProperty name="myFormBean" property="userName" />
> <input type="hidden" name="event" value="FORM_TEST">
> <input type="submit" name="submit" value="next">
> </form>
> 
> Here's the two relavant parts of the servlet.
> ================================
> public void process (ServletContext sc, HttpServletRequest request,
>                       HttpServletResponse response)
>                       throws IOException, ServletException {
> 
>         FormBean fb = new FormBean();
>         fb.setUserName(request.getParameter("userName"));
> 
>         if (fb.validate()) {
>                 URL = "index.jsp";
>         } else {
>                 // go back
>                 URL = "signup1.jsp";
>         }

            request.setAttribute( fb );

> }
> public void forward (HttpServletRequest request,
>         HttpServletResponse response) throws IOException,
>         ServletException {
> 
>         RequestDispatcher rd = request.getRequestDispatcher(URL);
>         rd.forward (request, response);
> }

You're not saving that bean in any context, so it is created and lost after you do forward. Just add that line I've added and it should be OK.

Nix.

Re: Sharing a bean between servlet and jsp. Arrggghhhhh

Posted by Charles Baker <ra...@yahoo.com>.
Which is first the jsp or the servlet? I don't see any
code in your servlet snippet that adds the new
instance of the bean to the session. If the bean
already exists in the session as created by the jsp, a
session.setAttribute("myFormBean", fb) will overwrite
the old bean values w/ the new values. If the servlet
is first, you still have to add the bean to the
session so that the jsp can use it.

--- "Dahnke, Eric" <ED...@nextsource.com> wrote:
> Now I'm going crazy. This can't be so hard. Please
> help... I can't figure
> this out. I'm trying to share a bean between a jsp
> page and servlet.
> Argghhhhhhhhh... Please.... I help on other lists...
> and donate time to
> charity. I've been to Barnes and Noble and looked at
> a heap of texts. I've
> done no less than 500 Google searches.
> 
> 
> There are no errors, I just can't get this
> "<jsp:getProperty
> name="myFormBean" property="userName" />" goddamned
> thing to display the
> userName bean property if fb.validate() fails.
> Everything works. I've got
> this begugger and can see the userName variable
> throughout the POST process.
> Everything's cool until the rd.forward (request,
> response), but once the
> forward takes place my jsp cannot again pick up the
> bean properties
> eventhough I say <jsp:useBean id="myFormBean"
> class="beans.FormBean"
> scope="session"/> I've tried every scope, and am
> using beans with straight
> JSP no problem (same application).
> 
> Any ideas? Millions of Thanks
> 
> 
> Here's the JSP page.
> ===============
> <jsp:useBean id="myFormBean" class="beans.FormBean"
> scope="session"/>
> <form method="POST" action="controller">
> <input type="text" name="userName">
> <jsp:getProperty name="myFormBean"
> property="userName" />
> <input type="hidden" name="event" value="FORM_TEST">
> <input type="submit" name="submit" value="next">
> </form>
> 
> Here's the two relavant parts of the servlet.
> ================================
> public void process (ServletContext sc,
> HttpServletRequest request,
>                       HttpServletResponse response)
>                       throws IOException,
> ServletException {
> 
>         FormBean fb = new FormBean();
>        
> fb.setUserName(request.getParameter("userName"));
> 
>         if (fb.validate()) {
>                 URL = "index.jsp";
>         } else {
>                 // go back
>                 URL = "signup1.jsp";
>         }
> }
> public void forward (HttpServletRequest request,
>         HttpServletResponse response) throws
> IOException,
>         ServletException {
> 
>         RequestDispatcher rd =
> request.getRequestDispatcher(URL);
>         rd.forward (request, response);
> }
> 
> Here's the bean
> ===========
> package beans;
> public class FormBean implements
> java.io.Serializable {
>         public String userName;
>         public FormBean() {}
>         public boolean validate() {
>                 boolean allOk=false;
>                 return allOk;
>         }
> 
>         public String getUserName() {
>                 //return this.userName; // doesn't
> work either
>                 return userName;
>         }
>         public void setUserName(String uname) {
>                 this.userName = uname;
>         }
> }
> 
> --
> To unsubscribe:  
> <ma...@jakarta.apache.org>
> For additional commands:
> <ma...@jakarta.apache.org>
> Troubles with the list:
> <ma...@jakarta.apache.org>
> 


=====
rascharles@yahoo.com
Hacking is a "Good Thing!"
See http://www.tuxedo.org/~esr/faqs/hacker-howto.html

__________________________________________________
Do You Yahoo!?
Send FREE Valentine eCards with Yahoo! Greetings!
http://greetings.yahoo.com

--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>