aspose file tools*
The moose likes Servlets and the fly likes Clarification of Servlet life cycle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Clarification of Servlet life cycle" Watch "Clarification of Servlet life cycle" New topic
Author

Clarification of Servlet life cycle

Bryce Martin
Ranch Hand

Joined: Nov 19, 2007
Posts: 269
I have my search jsp. When I submit the form I post and then go to my FormManager servlet where I direct to the proper jsp corresponding to the search criteria. This is fine and I can get it to direct based on the form inputs all the time. Here is where the trouble comes in. I'm creating a list of Beans that holds my search results. I create the list using at the top of my doPost method. Then when I go into my 'if' conditionals I have a try block where I set 'list' equal to an ArrayList that is returned from my processing classes. If the returned list is .empty() then I want to call a function that sets a bean value called errorMsg in my ErrorBean and forward(request,response) to an error.jsp where I tell them that the search return no results and I redirect them back to the search page.

I haven't put the automatic redirect on the page yet, right now its just a hyperlink that loads the search page again. Everything works smooth as silk, until I do the second search. If I enter the same criteria as I entered before (because I know it will return no results) it will act like the returned ArrayList is !.empty(). And then it forwards on to the results jsp which displays nothing since the arraylist of beans has nothing in it.

Why does it see the ArrayList as not being empty the second time through?

Does this have to do with the life cycle of my doPost in the servlet? Isn't it a new instance of everything once I submit it again?

[ December 21, 2007: Message edited by: Bryce Martin ]
[ December 22, 2007: Message edited by: Bear Bibeault ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You aren't declaring a variable with a name of 'list' outside of any of your methods, are you?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bryce Martin
Ranch Hand

Joined: Nov 19, 2007
Posts: 269
You mean globally in my classes instead of inside my methods that are in the classes right? I'm gonna check on it...
Bryce Martin
Ranch Hand

Joined: Nov 19, 2007
Posts: 269
The only place I declare is in my doPost. All my other lists in methods are declared in my methods. I keep their scope to that method and return a list called myList.

and that List is the only variable I have in this project name list.
[ December 21, 2007: Message edited by: Bryce Martin ]
Bryce Martin
Ranch Hand

Joined: Nov 19, 2007
Posts: 269
New test reveals that I can enter a search that produces results. Then go back to the search page via my back button and put in criteria that produces no results and it will go to the error page. If I click my link and search again with bad criteria it just shoots on through to the results page again. I'm very confused by this behavior...
[ December 21, 2007: Message edited by: Bryce Martin ]
Bryce Martin
Ranch Hand

Joined: Nov 19, 2007
Posts: 269
I figured it out. It really helps if you don't use absolute paths. This is because if you do this, and forget to change it, it just might point to your production url instead of looking at the copy on the local machine after you've uploaded it. Oh happy day, all is working just as I need it to.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

Yes, your URLs should be server-relative (but not page-relative).


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bryce Martin
Ranch Hand

Joined: Nov 19, 2007
Posts: 269
I understand server-relative, but what do you mean by not page-relative?
[ December 21, 2007: Message edited by: Bryce Martin ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

Page-relative URLs are resolved relative to the current URL. When the URL is mapped to the servlet, the whole concept of page folders is moot, so the URLs rarely resolve correctly.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

More info.
Bryce Martin
Ranch Hand

Joined: Nov 19, 2007
Posts: 269
Already doing it correctly. Finally, I'm doing something right from the start. Wheeee. You wait and see, I just might actually get good at this web app stuff someday
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Clarification of Servlet life cycle