File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes method showing different returns when declared on JSP or Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "method showing different returns when declared on JSP or Servlet" Watch "method showing different returns when declared on JSP or Servlet" New topic
Author

method showing different returns when declared on JSP or Servlet

Thiago Leobons
Greenhorn

Joined: Sep 04, 2011
Posts: 20

Hi,

I have
on a Servlet which calls



when I call it from servlet, it only returns the first occurrence of the database, and twice(!) but when I call it from the same JSP that shows only the doubled result, then it shows up all of the users on the database.

Result calling from servlet:
1 thiago borges leobons thiago thiagoleobons@gmail.com 1234-5678 Americas, 10000 Rio de Janeiro RJ
Genaro de Carvalho, 2000 apto 302 Rio de Janeiro RJ
1 thiago borges leobons thiago thiagoleobons@gmail.com 1234-5678 Americas, 10000 Rio de Janeiro RJ
Genaro de Carvalho, 2000 apto 302 Rio de Janeiro RJ

result calling from JSP:

[1, thiago, thiagoleobons@gmail.com, 1234-5678, 1, thiago, thiagoleobons@gmail.com, 1234-5678, 2, admin, admin@gmail.com, 1234-5678, 3, leobons, leobons@gmail.com, 1234-5678, 4, leob, thi@gmail.com, 12345666, 6, zzzz, leobo@gmail.com, 12345666, 8, thithi, th@gmail, 12322333]

Actually, if you see the result on the JSP, the first result is actually always coming doubled, even with the same primary key, which to me makes no sense!

Thanks for your help


OCJP - CHECK
OCWCD - IN PROGRESS
Gopakumar Naryanan
Ranch Hand

Joined: Jan 15, 2011
Posts: 72

Please post the code where you are invoking that function in your jsp, it will make us to understand the problem more clear.


Thanks & Regards
Gopakumar
Thiago Leobons
Greenhorn

Joined: Sep 04, 2011
Posts: 20


The scriplet on the bottom is for debugging purposes, when I uncomment the redirection, it sends me to the JSP that does show me the whole content of the user table on the database.

If you also want the whole method on the servlet:

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Do you really not indent your JSP code?

Where's the servlet code that loops through the list showing different results?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Thiago Leobons
Greenhorn

Joined: Sep 04, 2011
Posts: 20

the JSP loops through the list using the tag <c:forEach>
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

And the servlet code? You said it worked differently when you called it from a servlet, so you should be able to show us that code too.
Thiago Leobons
Greenhorn

Joined: Sep 04, 2011
Posts: 20

the 2nd block of code in my 1st answer is the servlet code
Thiago Leobons
Greenhorn

Joined: Sep 04, 2011
Posts: 20

You can see the loop on lines 4 and 5 of the JSP as I pointed out before (the forEach tag). As I also pointed out before, I put a scriplet on the bottom specially for checking out the answer I get when calling the same method from a JSP, not a Servlet.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

But look. You said
when I call it from servlet, it only returns the first occurrence of the database


Apparently this doesn't mean that when you call that code fragment from a servlet, it only returns the first occurrence of the database. It must mean something else, since you don't actually have any examples of calling that code fragment from a servlet.

So what exactly does it mean? You're saying that code fragment works differently in two situations, but you've only showed us one, as far as I can see. You're going to have to do better at explaining the two situations.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Thiago Leobons wrote:I mean, what would be the point of people doing when they could just not answer, right?


There's a lot of people here that I don't waste my time answering. You aren't in that category, not yet anyway.
Thiago Leobons
Greenhorn

Joined: Sep 04, 2011
Posts: 20


Paul Clapham wrote:
Apparently this doesn't mean that when you call that code fragment from a servlet, it only returns the first occurrence of the database. It must mean something else, since you don't actually have any examples of calling that code fragment from a servlet.


But yes I do! It's on line 15 of my servlet's code
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I think I see what you mean. But I don't understand what you are comparing... your first version (the EL expression) is this horribly cumbersome code with nested c:forEach loops and your second version (the scriptlet) is no code at all; I don't see how you can say anything at all based on that code.

I would simplify the testing code and use the same tests for both versions.
Thiago Leobons
Greenhorn

Joined: Sep 04, 2011
Posts: 20

The code on the scriplet calls the same method as I did on the servlet but it comes up with all the entities on the table, differently from the code that's formatted with what you call "horribly cumbersome". Let me know if you find a simpler way to format this table the way I want to.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You're saying that the code, when first executed, returns N records, and then when executed a second time, returns M records, where N and M are different numbers. You only need one line of code to produce N (instead of the 25 lines you have there) and likewise you only need one line of code to produce M, whereas you don't have any such code in what you posted.

So my suggestion is to change your code which tests your theory to do just that. Just those two lines of code.
Thiago Leobons
Greenhorn

Joined: Sep 04, 2011
Posts: 20

Please consider only lines 04 (calling attribute from line 15 of my servlet) and line 36 (the results from this line show on the redirection page if you uncomment it) for simplicity sake.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Okay. But you weren't asking me to test your code, were you? Obviously I don't have a lot of the infrastructure needed to do that. I was asking YOU to do that test -- a line like line 4 which shows the size of the first version of the collection, and an identical line which shows the size of the second collection. Nothing more or less than that.
Thiago Leobons
Greenhorn

Joined: Sep 04, 2011
Posts: 20

I put that on my first message.. I'll put that again in block code for you to see the result

Result calling from servlet:
1 thiago borges leobons thiago thiagoleobons@gmail.com 1234-5678 Americas, 10000 Rio de Janeiro RJ
Genaro de Carvalho, 2000 apto 302 Rio de Janeiro RJ
1 thiago borges leobons thiago thiagoleobons@gmail.com 1234-5678 Americas, 10000 Rio de Janeiro RJ
Genaro de Carvalho, 2000 apto 302 Rio de Janeiro RJ


result calling from JSP:

[1, thiago, thiagoleobons@gmail.com, 1234-5678, 1, thiago, thiagoleobons@gmail.com, 1234-5678, 2, admin, admin@gmail.com, 1234-5678, 3, leobons, leobons@gmail.com, 1234-5678, 4, leob, thi@gmail.com, 12345666, 6, zzzz, leobo@gmail.com, 12345666, 8, thithi, th@gmail, 12322333]


see that the code from servlet only brings up the two first results (which are "actually the same") or at least both have the same primary key

(I edited your post's format so that it didn't come out three times as wide as my screen - Paul C)
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

No, that was the output from your code that was partly too complicated and partly invisible to me. Let me know when you have the output from the test which I suggested. If you don't understand what I suggested, then let me know and I will write the code for you.
Thiago Leobons
Greenhorn

Joined: Sep 04, 2011
Posts: 20

oh wow.. thanks for the suggestion of taking the formatting out. It ends up that the addresses are not being created on the database and since the printing of the user values are linked to it by the varStatus on the nested loop, they're not showing.
still.. wonder why the array is coming from the bank with primary key #1 showing twice?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

It looks like you're getting somewhere now. Originally your theory was "servlets produce different data than JSPs" or something like that, and you seemed determined to prove that theory correct.

But when you're debugging, it's just as good to have a theory which you can prove to be incorrect. At least then you can check off something and say "This isn't the problem, then." That's why it's a good idea to produce the simplest possible test for a theory, even if it involves setting aside your existing code and writing a special test harness just for that theory. That makes it much easier to get rid of incorrect theories. Your code was far from being the simplest possible test, and as I suspected, it was leading you in the wrong direction.

I doubted your original theory from the start, or at least the version of it which I just stated. My (unstated) competing theory was "The second call produces different data than the first call." However it appears that's an incorrect theory as well, but I don't mind at all. The point isn't for me (or you) to be right, it's to find out what's going on.
Thiago Leobons
Greenhorn

Joined: Sep 04, 2011
Posts: 20

I wasn't determined to show it was true I was just repeating it because it seemed to me that people didn't get what I was trying to explain, but yeah you're right, that was a silly mistake. I'm just still having the issue of showing the first line of the table twice. any idea on why this should be happening?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: method showing different returns when declared on JSP or Servlet
 
Similar Threads
WA #1.....word association
******Let's do the counting for those who took the exam****
******Let's do the counting for those who PASSED the exam****
2016 olympics
Place Association