• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Clarification of Servlet life cycle

 
Bryce Martin
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You aren't declaring a variable with a name of 'list' outside of any of your methods, are you?
 
Bryce Martin
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, your URLs should be server-relative (but not page-relative).
 
Bryce Martin
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
 
Bryce Martin
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic