aspose file tools*
The moose likes Servlets and the fly likes Listener Class and Regular Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Listener Class and Regular Java" Watch "Listener Class and Regular Java" New topic
Author

Listener Class and Regular Java

Luke Shannon
Ranch Hand

Joined: Sep 30, 2004
Posts: 239
Hello;

This may be more of a design question, than servlet specific. That being said...

I have listener class that creates a connection object and hands it off to servlets that get initialized.

However, I have a lot of my database logic in some regular java files (sometimes the servlet instantiates them, sometimes a JSP files does, in near the future I would like a stand alone Swing App to be able to call them).

I don't think they can get access to the ServletContext. How can I centralize their DB connection procedure so I don't have to hard code connection information into each of these classes?

Thanks,

L


Luke
SCJP
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60071
    
  65

Since the means of obtaining the connection are dependent upon the UI environment, and since you are trying to keep your model classes independent of the UI environment, the connection should be passed to the class when they need to perform an operation.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Luke Shannon
Ranch Hand

Joined: Sep 30, 2004
Posts: 239
Thanks for the quick response.

If a JSP needs to call one of these objects it should be able to access the context-param in the web.xml file right?

If I can do that than I able to get the connection string (from web.xml) and pass it to the constructor of the class. The connection could be created in the constructor without having to hard code the connection string. This would be good.

I could do all this in a scriptlet on the JSP page. Once I have my reference I could call it methods to perform the data operations and than use its getter methods to display the resulting data on the page.

Does this sound reasonable?

Is there a way I can do all of this without using too much scriptlet?

Thanks,

L
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60071
    
  65

While context params are available to JSPs, I avoid using JSPs for any kind of processing (which I keep under control of servlet controllers). This eliminates the need for any scripting in the JSP pages (especially when using JSP 2.0).

I would also recommend -- in the servlet environment -- to use connection pooling; container-managed or otherwise.
Luke Shannon
Ranch Hand

Joined: Sep 30, 2004
Posts: 239
Ok. I have been striving to this very thing. The problem I am running into is when the JSP loads up a couple of the drop-downs in the form are created with data from the DB, before it posts to any servlet.

When a user selects various options from these drop downs, other fields in the form should be populated with data from the DB. I was trying to do this using a servlet.

Here is an example of what I mean (please bear with me...little joke):

This is one of the drop downs from the page Contact.jsp:

<select name="auctioneer" onChange="setValues();">

Here is setValues() function defined in Contact.jsp:

function setValues() {
var formName = window.document.contact;
formName.action = "ContactData";
formName.submit();
}

As you can see if sets the action of the form to a Servlet called ContactData.

Here is the do Post of ContactData servlet:

//get the name of the contact from the JSP form
String passedName = request.getParameter("auctioneer");
//get the connection string from the Servlet Context
Connection con = (Connection) getServletContext().getAttribute("db");
//create the Data Bean
ContactBeanData dbData = new ContactBeanData(con);
//use getContactBean method in the ContactBeanData to get a contact bean containing all the required data from the DB
beans.ContactBean contactInfo = dbData.getContactBean(passedName);
//pass the Contact bean to the jsp page
request.setAttribute("contactData", contactInfo);
RequestDispatcher view = request.getRequestDispatcher("Contact.jsp");
view.forward(request, response);

The plan was to have the Contact.jsp try and load the ContactBean each time. Than I could fill in the form with the getter methods from the bean.

If the servlet above hadn't created a bean a default would be created (all string fields in the bean are "" by default) and nothing would be written into the rest of the fields in the form.

However, if the page posted to the above servlet the bean would be created and passed back to the page. The page could than have all the data that I could write into the form.

The problem I ran into is it never seems to get the bean that is created. It creates a new bean every time the page is loaded, even if ContactData is forwarding to it.

Not sure if this is a stupid design or if I am just doing something wrong.

Thanks,
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60071
    
  65

The problem I am running into is when the JSP loads up a couple of the drop-downs in the form are created with data from the DB, before it posts to any servlet.


This is not a design approach that I like to use. I hardly ever post to a JSP page directly (unless the page is completely butt-simple). Rather, I always post to a servlet controller that does all the work of setting up whatever data structures a page will need (a list of name/value pairs for a dynamic dropdown, for example) so that the JSP page itself is straight-forward and as simple as poosible.

It sounds like a small change in the way of doing things, but I have found that it reaps a huge reward in making JSP pages easy to create, extend and maintain.
[ October 02, 2004: Message edited by: Bear Bibeault ]
Luke Shannon
Ranch Hand

Joined: Sep 30, 2004
Posts: 239
Thank you. I am restructuring my design taking your suggestion into account. The first version of this was entirely scriptlets and very complicated. I finally feel like I am moving in a direction where I will have maintainable code. Cheers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Listener Class and Regular Java
 
Similar Threads
hi(can i store data in files)
Swing and ofbiz (type) frameworks
accessing ServletContext parameters
How to get context-param from web.xml in struts app
Session in HTTPServlet