• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

jsp:setProperty and requestDispatcher.forward problem

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

I am trying to execute a simple example from HeadFirst servlets and JSPs and running into a problem.
I have tried to resolve the issue in multiple ways but looks like I amm ending up nowhere.
Need your help on this.


Code in index.html


Code in HelloController.java my servlet class


Code in the forwarded page result.jsp


web.xml


When i try to call the index.html from the browser it gives me the page with a text box where in I enter "Fred" and click on "Submit"
I should be getting "Hello Bingo Fred" instead I get "Hello Bingo null", so it is quite sure that the attribute is not being set.
But what is the reason I am unable to narrow down on. Can you please help me with it. I know I am doing some kind of a silly error somewhere but unable to track it.

and by the way I even changed it to form action="result.jsp", yet there was no change..

Please please help...

Thanks
Anjali
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
change <%= request.getAttribute("name") %> to <%= request.getParameter("userName") %> directly getting value from client request on jsp.
[or ]

just set value to request scope before you forward result page .


and in jsp as you already done as <%= request.getAttribute("name") %>

<edit>

Anjali Raman wrote:
and by the way I even changed it to form action="result.jsp", yet there was no change..


</edit>
that is because you are using wrong key name. as well as wrong method . it should be request.getParameter("userName") in result.jsp


 
Anjali Raman
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Seetharaman for the reply, and yes I forgot to insert the line
request.setAttribute("name", name);

But from the example that I see in the book, I should be able to do
<%=request.getAttribute("name") %> and the name entered in the HTML page should be displayed as it is saved to the request when we did request.setAttribute("name", name) in the servlet.

So I want to know what is that I am doing wrong by using getAttribute and setAttribute, I am sure that it would work in case I used getParameter, but thats not what I want.
I am attempting the example shown in Page 345 of Head First Servlets and JSP.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anjali Raman wrote: yes I forgot to insert the line
request.setAttribute("name", name);


insert and test(compile and run) now. this should solve your problem :-)
 
Anjali Raman
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did it and still no luck.

Gives me "Hello Bingo null"
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) Remove the constructor. It's superfluous and extraneous. It's a bad practice to include crap that doesn't do anything. It's just confusing and a hook for possible future bugs.

2) Scriptlets have been obsolete and discredited for over 10 years. There is no valid excuse for using them in any JSP page written after 2002.

3)

But from the example that I see in the book, I should be able to do
<%=request.getAttribute("name") %> and the name entered in the HTML page should be displayed

You must be misinterpreting something because there is no magic that will make request parameters automatically appear as scoped variables (aka "attributes"). You must explicitly set values as scoped variables for them to exist.
 
Anjali Raman
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bear for the answer.

I know that we should not be using the scriptlets, but I was just attempting some of the examples in the book in preparation for SCJWD.

And i wanted to know what was I doing wrong that I wasnt getting the answer shown in the screenshot in the book.(the one I have also attached)

And yes it works with request.getParameter i have tested that.
Headfirst.png
[Thumbnail for Headfirst.png]
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anjali Raman wrote: in preparation for SCJWD.


You should alway mention that. Otherwise people assume you are writing "real" code.
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And, yup, that should work.
 
Anjali Raman
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But then why is it that I am getting "null" instead of "Fred" or any name that I would provide in the "Name" field of the HTML.

I have drilled my brain quite a lot on this. I would appreciate some help
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please show your latest code. No need to show the deployment descriptor.
 
Anjali Raman
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bear for replying back... Here is the code

index.html


HelloController.java


result.jsp
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does the servlet still have a constructor?

Why does the JSP have an import statement?

What URL are you using to display the results?
 
Anjali Raman
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry for that, I had deleted all the files and tried it all over again. And eclipse by default had the constructor there that I forgot to remove as what you had suggested.

I added the import just to check if it would make any difference, but obviously it was an unnecessary line of code there. I removed it.

I have made changes as per your earlier comments.

I run the index.html from eclipse directly using Run As --> Run on Server, which opens the "http://localhost/ServletsDemo/index.html" and I enter the name "Fred" in the textbox, and on submitting it takes me to "http://localhost/ServletsDemo/result.jsp", where in the text "Hello Bingo null" is shown.

I have attached the screenshot of the directory Structure.
DirectoryStructure.png
[Thumbnail for DirectoryStructure.png]
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will ask one last time: are you using the URL of the servlet or of the JSP?
 
Anjali Raman
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using the URL of HTML in the browser.(I hope i got your question right). The HTML has the form action pointing to the servlet /HelloController.
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then you click the submit button of the form? I feel like I'm pulling hen's teeth here.
 
Anjali Raman
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's right. I too am not able to figure out what am I doing wrong with such a simple example.
Somehow I have this vague feeling that the request is not going to the servlet code, where it was supposed to set the Attribute.

I have gone over the program for 20 times and unable to figure out the problem. Its so frustrating...

Thanks Bear for all the time that you have taken on looking into this.
 
Anjali Raman
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bear, can you please guide me with just one thing here.
Am I supposed to provide the servlet URL or the result.jsp URL in the form action of the HTML page?
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The servlet URL should always be used. Otherwise, how is it supposed to execute?

In fact, it's best practice to place the JSP views in a folder that's under WEB-INF so that they cannot be directly addressed without their controller.
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
P.S. Your form action should include the context path; e.g. ${pageContext.request.contextPath}
 
Anjali Raman
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had my JSPs under the WEB-INF folders. I am trying this other way now to make sure that index.html submit is calling the servlet. Will keep you posted if I see anything odd, but I am quite sure that my servlet is not invoked at all.
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That would certainly explain the null.
 
Always! Wait. Never. Shut up. Look at this tiny ad.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic