Forums Register Login

ArrayList not clearing

+Pie Number of slices to send: Send
Lets start with the Servlet Code....



Here is what is happening...

When I run the app I'll enter some search criteria. I'll get good results. Then if I close the browser, or just go back and rerun the search it will give me the previous set of results plus the new ones.

My arraylist is a list of beans. The servlet runs each time the user makes a form submit from the first screen. What I need to do is make sure that the array list is clear before I do any of my lines.

I've already turned off browser caching using the meta tags in my template. My template is applied to all the pages.

Any ideas?
[ November 30, 2007: Message edited by: Bear Bibeault ]
+Pie Number of slices to send: Send
Bryce,
list is an instance level variable. It is initialized once one the servlet is created and then never again. You would need to re-initialize it each time or better yet make the list a local variable. The current approach is not thread safe. If two people access the servlet at the same time, they will get a mix of data back.
+Pie Number of slices to send: Send
So if I slide that down inside my doPost method I'll be ok then? That makes me wonder about my DB connections too then...

They are listed in the class and not in the methods... would this be a problem as well?
+Pie Number of slices to send: Send
Bryce, Servlet container creates only one instance of any servlet and all the request share the same object by multi thread, Servlet is a multitreaded hence there will be only one object for a give servlet and as the new request comes in , an new thread is created for the request and hence not a new object. and hence if you add data to the arraylist , it will be visible to all the request and till the time the servlet container(web server or app server) goes down. It is srongly advice not to use any variables in the servlets.

Rajesh
+Pie Number of slices to send: Send
Ok. I moved the list inside the method. It is thread safe now. I've tested it and I get no cross results while performing multiple actions on the servlet. My concern now turns toward my connection. I think, as I have it now, I only get one connection with the servlet and its being shared by all the requests. I'm guessing this is not the way to do it? I should have a connection per request? If I leave it this way, it is my thinking that the responses would be very slow for many requests over the connection?
+Pie Number of slices to send: Send
You should be using container-managed connection pooling to obtain your connections.

You should also seriously start thinking about factoring your DB code into a Model that is UI-agnostic.
+Pie Number of slices to send: Send
Well I have a separate class that I called ConnDB.java

In that class I have my two methods, one for connecting to the DB and one for obtaining a ResultSet. Here is my code...




Is this what you are talking about when you say DB code that is UI agnostic? Any java class can call these methods to make connections and get result sets.
How do I do the container controlled connection pooling? What is the coding difference? I had a hard time getting the scope right so that I could make the connection from my servlet. Should I have each method make a connection when it is called? I'm not real clear on how these different methods are handled... Any clarification would be appreciated.
+Pie Number of slices to send: Send
Bryce, what Bear meant is that you should refactor your code so that the database logic/connection stuff is not dependent on that particular view. So, that it can be accesed from a browser, a PDA, or a non-web application, etc.

You may want to look up or search the forum for the Model-View-Controller (MVC) pattern

briefly... your data access logic will be in the model (possibly using a DAO). The view will be your JSP in this case. And the Servlet will serve as the controller. It will serve a a communicator between the view and the model.

There is tons of stuff out there on MVC, so...
+Pie Number of slices to send: Send
I think that I've gotten it in MVC for the most part. I might be missing something though. I have my JSP. That is the view. The only thing on my JSPs are html tags and jstl tags with EL to get information stored in beans. So I have the view part right. Now I have a Bean, which I store my information in and I have a servlet that puts the information into a bean. The bean would be my model and the servlet would be my controller.

So that is MVC. But my issue seems to be with database connectivity. This would be a separate controller? How exactly does connections to the data base fit in the MVC? I thought they would be part of the Controller part. I'll go dig around for some examples of MVC that include database connections. But I think I'm pretty close on this. I can't be far off...
+Pie Number of slices to send: Send
If you are doing DB calls in the servlets, you're not even close to MVC. Can your app pass this test? Put a Swing or a command line UI on your app without copying or moving any model code.
+Pie Number of slices to send: Send
hi Bryce

MVC is used to loosly couple the business logic and the presentation (servlets,jsp,...) so never put your business logic into servlets or into jsp it is a bad design, well you can grow existing frameworks like struts or spring which hve implemented MVC, and use datasource so that you can reap the benifit of connection pooling and transactions based on the server implementation for datasource


Rajesh Mammidi
+Pie Number of slices to send: Send
Well, I just had a nice long response with some questions, then nature called me to my "special thinking spot". And while thinking I figured it out. I processed it. I was looking at it from the wrong angle.

Here is how I was looking at it...

My servlet needed to get some info from the db to the bean. And I was thinking, "what do I need to do first?". I need a DB connection. So, that was what I tried to do. Then I needed my result set. So I built my sql string and made my db call and got my result set. But just because things are supposed to happen in that order doesn't mean the servlet should be written in a strict top down approach. This is completely wrong.

These are the steps I need to happen.
1.create sql string
2.connect to db
3.get resultset
4.make list of beans by iterating over resultset.

In my servlet I need to call a class called SetBean. That class will make a call to my BuildList method, which calls the ResultSet method, which will make a call to the connection method. By staring at the end result and creating separate classes and methods as I go backwards. This will make my code fall into the MVC pattern. The only thing my servlet is going to do is direct traffic. It will figure out which way I should be moving, pass the appropriate string out to the generic list building methods which will need to only return the list to my servlet so that I can do my request.setAttribute("list",list);
Then forward my getRequestDispatcher to the right jsp.

Whew! I know it took me a while to finally see this. Thank you guys for all your patience and direction. If I'm still missing something please let me know.

Thanks
Bryce Martin
Blueberry pie is best when it is firm and you can hold in your hand. Smell it. And smell this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1105 times.
Similar Threads
Can't perform logic on my bean class getter methods
NullPointerException...
Eating the Beans with a side of Servlets
Method visibility among classes...
Clear bean values onLoad
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 01:53:23.