David Mossakowski wrote:
> How to make a connection to a database reusable? I do not want a bean
> to load the driver for each session. It would be great if I could have
> a ConnectionServlet that I could pass a request from a bean let's say.
> Or would it be easier to call servlet from JSP? How can this be done?
The way I do this is to pass the connection to a .jsp page through a servlet:
http://localhost/servlet/ConnectorServlet/path/to/page.jsp
The only purpose in life this servlet has is to attach a previously obtained Connector
to the current session, and then forward() to the desired /path/to/page.jsp. When the
processRequest() method of a bean associated with page.jsp is invoked, the connector
has been attached to the session and is available for use.
> Basically what's needed is a search box to enter criteria and then two
> pages: one with list of results and other with detailed view of a
> result.
Create a bean (QueryBean) with properties that represent all of your data elements.
The field names on QueryPage.html need to match the property names in the bean. Then
POST/GET to http://localhost/servlet/ConnectorServlet/ResultPage.jsp.
In ResultPage.jsp you should have (assuming JSP 0.92):
<usebean name="querybean" type="package.QueryBean" lifespan="session">
<setfromrequest beanproperty="*">
</usebean>
If the field names match the property names of your bean, the magic here is that the
properties will be automatically filled. You can then examine them and use them for
querying the database in the QueryBean.processRequest() method. At this point, you
will probably want a separate ResultsBean to contain the results of your query.
> This also brings another question. Does clicking on a link in a jsp
> page transfer the request information with it to the next jsp page for
> use in a bean? Or is it only transferred by the use of form?
The magic is in the:
<setfromrequest beanproperty="*">
This causes the all bean properties that match the field names to be populated with
the contents of the fields from the preceding POST or GET (the one that got you here).
Hope this helps.
cc
begin:vcard
n:Cobb;Christopher
tel;cell:703-909-7550
tel;fax:703-648-7475
tel;work:703-648-6725
x-mozilla-html:TRUE
org:Powerhouse Technologies, Inc.
adr:;;;;;;
version:2.1
email;internet:[EMAIL PROTECTED]
title:SW Architect
fn:Christopher Cobb
end:vcard