Hi,

>In Model2Servlet, I can definitely get the values from the request object or
>write a method in UserInfoBean to process the request. But I want to use
>jsp:setProperty tag to populate bean data with the form data.

I dont believe you can. Using Model 2, your form action goes to a servlet.
The servlet receives the request object. In the method that handles the
request (usually dictated by hidden form element that you name
whatever..but "command" is a common name), you CREATE a bean, populate its
fields with the request parameters, put the bean in the HttpSession, then
forward to the bean:

public void handleFormXXX(HttpServletRequest request, HttpServletResponse
response)
{
  MyBean mybean = new MyBean();

  mybean.setName( request.getParameter("name") );
  mybean.setPassword( request.getParameter("password") );

  HttpSession session = request.getSession(true);
  session.putBalue("MyBean", mybean);

  // either RequestDispatcher.forward() or response.sendRedirect here..your
choice.

  response.sendRedirect("display_page.jsp");
}

Now, the display_page would be something like:

<jsp:useBean id="mybean" scope="session" class="mypackage.MyBean" />

<html><head></head>
<body>

Your name is <%= mybean.getName() %><br>
Your password is <%= mybean.getPassword() %><br>

</body>
</html>

I have wondered why in the servlet you cant create AND pass the Request
object to the bean for it to "auto-populate" the beans fields. You
certainly could do that if you want..but you need to create a new
constructor that first calls the bean constructor, then populates the
fields of the bean:

MyBean.java
-----------

public calss MyBean
{
   public MyBean()
   {
   }

   public MyBean(HttpServletRequest request)
   {
     this();

     setName( request.getParameter("name") );
     setPassword( request.getParameter("password") );
   }

}


Now..with the above, instead of your servlet populating the bean, you
simply create the bean and pass it the request object your servlet gets,
and the bean constructor would fill in the fields for you. Ofcourse, you
should put checks on the request.getParameter() calls so you dont get a NPE
if the parameter doesn't exist for some reason. But, if you are the only
one writing it and document it well and can guarantee those parameteres
always exist, you dont absolutely have to.

See, the JSP engine takes care of the JSP Model 1 -> Bean population of the
request. If you ever see a .java source that a JSP page turns into, you'll
see that there is a call to JSPBean (some name like that) and the request
object is passed along with the bean name and other things. That method
auto-populates the bean at that point.

If you need any more info, I will certainly try to help. Im sure others
here know more than I about this, but this is what I get out of it all.


Kevin Duffey
Software Engineer
[EMAIL PROTECTED]

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html

Reply via email to