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 jsp:setProperty and requestDispatcher.forward problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "jsp:setProperty and requestDispatcher.forward problem" Watch "jsp:setProperty and requestDispatcher.forward problem" New topic
Author

jsp:setProperty and requestDispatcher.forward problem

Anjali Raman
Ranch Hand

Joined: Nov 28, 2007
Posts: 57
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
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Nov 28, 2007
Posts: 57
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Nov 28, 2007
Posts: 57
Did it and still no luck.

Gives me "Hello Bingo null"
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61417
    
  67

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.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Anjali Raman
Ranch Hand

Joined: Nov 28, 2007
Posts: 57
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.


[Thumbnail for Headfirst.png]

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61417
    
  67

Anjali Raman wrote: in preparation for SCJWD.

You should alway mention that. Otherwise people assume you are writing "real" code.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61417
    
  67

And, yup, that should work.
Anjali Raman
Ranch Hand

Joined: Nov 28, 2007
Posts: 57
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61417
    
  67

Please show your latest code. No need to show the deployment descriptor.
Anjali Raman
Ranch Hand

Joined: Nov 28, 2007
Posts: 57
Thanks Bear for replying back... Here is the code

index.html


HelloController.java


result.jsp
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61417
    
  67

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

Joined: Nov 28, 2007
Posts: 57
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.


[Thumbnail for DirectoryStructure.png]

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61417
    
  67

I will ask one last time: are you using the URL of the servlet or of the JSP?
Anjali Raman
Ranch Hand

Joined: Nov 28, 2007
Posts: 57
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61417
    
  67

Then you click the submit button of the form? I feel like I'm pulling hen's teeth here.
Anjali Raman
Ranch Hand

Joined: Nov 28, 2007
Posts: 57
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

Joined: Nov 28, 2007
Posts: 57
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61417
    
  67

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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61417
    
  67

P.S. Your form action should include the context path; e.g. ${pageContext.request.contextPath}
Anjali Raman
Ranch Hand

Joined: Nov 28, 2007
Posts: 57
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61417
    
  67

That would certainly explain the null.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jsp:setProperty and requestDispatcher.forward problem