You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by ALEXANDROS KOTSIRAS <AL...@prodigy.net> on 2000/06/15 04:49:16 UTC

Can i use
Hello, 
I am developing a project with servlets,JSP and jDBC and i am almost exclusively using Model 2 architecture.
But i never use <jsp:useBean tag in my JSP files. 
Is there a way to use that tag and also <jsp:getProperty ... with Model 2 ? 
My approach is the following :
All the requests from the JSP files go to a Controller servlet. 
In  this servlet then i instantiate the Database bean as  : 

EmployeeBean employee  = new EmployeeBean(request, response);
    // now the EmployeeBean holds all the values from the database
request.setAttribute("emplyeeBean" , employee);
getServletContext().getRequestDispatcher("/jsp/employee.jsp").forward(request, response);

In the employee.jsp file i do : 

<%
    EmplyeeBean employee = (EmployeeBean) request.getAttribute("employeeBean");
%>
<TR><TD><%= employee.getLastName() %></TD></TR>
etc.....

The above is the case when i want to display ONE record.
When i want to display many records from a search i usually create the Resultset object from my bean 
and in the servlet i  put the Resultset object in the Request , and then I retrieve the ResultSet in my JSP as : 
<%
    ResultSet rs = (ResultSet) request.getAttribute("myresultSet")
    while(rs.next()) { 
%>
<TR><TD><%= rs.getString(1) %></TD></TR>  ...etc ...

So where is the use of <jsp:useBean tag with Model 2 ?
I never need to instantiate a bean directly from any JSP file. All the beans are instantiated within the Servlet Controller.
Also the above approach keeps pretty clean the JSP files from JAVA code.

Am i missing something ? ?

Re: Can i use Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
See embedded comments below.

ALEXANDROS KOTSIRAS wrote:

> Hello,I am developing a project with servlets,JSP and jDBC and i am
> almost exclusively using Model 2 architecture.But i never use
> <jsp:useBean tag in my JSP files.Is there a way to use that tag and
> also <jsp:getProperty ... with Model 2 ?My approach is the following
> :All the requests from the JSP files go to a Controller servlet.In
> this servlet then i instantiate the Database bean as  : EmployeeBean
> employee  = new EmployeeBean(request, response);    // now the
> EmployeeBean holds all the values from the
> databaserequest.setAttribute("emplyeeBean" ,
> employee);getServletContext().getRequestDispatcher("/jsp/employee.jsp").forward(request,
> response); In the employee.jsp file i do : <%    EmplyeeBean employee
> = (EmployeeBean) request.getAttribute("employeeBean");%><TR><TD><%=
> employee.getLastName() %></TD></TR>etc.....

The above scriptlet is pretty much equivalent to the following:

    <jsp:useBean id="employeeBean" type="EmployeeBean" scope="request"/>

    <tr><td><jsp:getProperty name="employeeBean"
property="lastName"/></td></tr>

because the <jsp:useBean> tag will discover that the bean already
exists, so it does not need to be created.

Craig McClanahan



Re: Can i use Posted by Ken Kress <kk...@ospreypartners.com>.
Alexandros,

I'm still learning, but I started off with useBean and really like
it. It seems very clean. I try to avoid scriptlets -- except when
I have to iterate through a ResultSet.

What I do is set up a bean to hold information from the query. I've
seen people put the query glue into a servlet, but I've made the
bean more active. It knows to establish the db connection, do
the query and fill in the instance variables.  In my JSP, I just
getProperties. It's sweet.

I'm using model 2, also. I like to think of the beans as state
machines. The controller servlet can check the state and take
appropriate action. I'm using a hashMap for the dispatching, but
will be moving it into a db table before I go live. It should let
me re-use my beans and JSPs on other projects.

   Ken.

--
Ken Kress                               kkress@ospreypartners.com
Osprey Partners LLC                                  610.240.7289


----- Original Message -----
From: "ALEXANDROS KOTSIRAS" <AL...@prodigy.net>
To: <to...@jakarta.apache.org>
Sent: Wednesday, June 14, 2000 10:49 PM
Subject: Can i use <jsp:useBean .. tag with Model 2 Architecture ? ?


Hello,
I am developing a project with servlets,JSP and jDBC and i am almost
exclusively using Model 2 architecture.
But i never use <jsp:useBean tag in my JSP files.
Is there a way to use that tag and also <jsp:getProperty ... with Model 2 ?
My approach is the following :
All the requests from the JSP files go to a Controller servlet.
In  this servlet then i instantiate the Database bean as  :

EmployeeBean employee  = new EmployeeBean(request, response);
    // now the EmployeeBean holds all the values from the database
request.setAttribute("emplyeeBean" , employee);
getServletContext().getRequestDispatcher("/jsp/employee.jsp").forward(reques
t, response);

In the employee.jsp file i do :

<%
    EmplyeeBean employee = (EmployeeBean)
request.getAttribute("employeeBean");
%>
<TR><TD><%= employee.getLastName() %></TD></TR>
etc.....

The above is the case when i want to display ONE record.
When i want to display many records from a search i usually create the
Resultset object from my bean
and in the servlet i  put the Resultset object in the Request , and then I
retrieve the ResultSet in my JSP as :
<%
    ResultSet rs = (ResultSet) request.getAttribute("myresultSet")
    while(rs.next())

%>
<TR><TD><%= rs.getString(1) %></TD></TR>  ...etc ...

So where is the use of <jsp:useBean tag with Model 2 ?
I never need to instantiate a bean directly from any JSP file. All the beans
are instantiated within the Servlet Controller.
Also the above approach keeps pretty clean the JSP files from JAVA code.

Am i missing something ? ?