This week's book giveaway is in the OCP forum.
We're giving away four copies of OCP Java SE 8 Programmer II Exam Study Guide and have Kathy Sierra, Bert Bates, & Elizabeth Robson on-line!
See this thread for details.
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

difficulty in understanding <jsp:forward page=" " />and unusual code execution.  RSS feed

 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am preparing for SCWCD. I have wrtten three jsp files. index.jsp(has input text field), hello.jsp(does the conditional forward to handleit.jsp) and finally handleit.jsp.

The Ccode looks like this.

index.jsp



hello.jsp:


handleit.jsp:



Th problem is when I execute index.jsp even if i don't enter the name in the index.jsp, instead of displaying handle.jsp it displayes hello.jsp. But the EL in hello.jsp displayes nothing.

And another doubt, what do they mean by <jsp:forword> starts with clear response buffer.

Please help me understand the code and why so.
 
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you've cleared the output buffer in hello.jsp before doing the forward. So it will not work. You need to move the checking code to the top of hello.jsp and remove the out.flush() line. And the best thing would be that you move this logic into a servlet...
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess the userName parameter value would be a "" which is not the same as null
 
Anil Deshpande
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I removed it, but still It is not working as expected. Moving the logic to servlet is one option. But I want to do it in JSP.This example is there in Head First and I am just trying to execute it.

It is not working as explained in the book that's the problem.

And Vishwanath seems to be right. Empty string is not a null. Of I call request.getParameter("userName").length() it is giving answer as zero instead of throwing NullPointerException
 
Bartender
Posts: 4042
47
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Anil Deshpande wrote:Well, I removed it, but still It is not working as expected



Do the check for empty String as well and see what happens.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Vishwanath Murthi wrote:I guess the userName parameter value would be a "" which is not the same as null



yes this is the same thing i thought of. because even someone doesn't enter data into a form field, all the parameters will be there on the request. so that means if i asked the value of userName(in this case) it should return me a empty string not a null value.
 
Anil Deshpande
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't you think, it is wrong. When I am not entering anything in the text feild, I expect it to be a null value. not a empty String with length=0.

I will never be sure whether to check for empty string or handle NullPointerException.
 
Amila Jayatillaka
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Anil Deshpande wrote:Don't you think, it is wrong. When I am not entering anything in the text feild, I expect it to be a null value. not a empty String with length=0.

I will never be sure whether to check for empty string or handle NullPointerException.



Well if you do the same(putting a text field and call getText()) in swing you will get a empty string not a null value. So I think some what same applies here. Anyway even this is not the same I think it should work like I said.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!