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 Unable to display arrays in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Unable to display arrays in JSP" Watch "Unable to display arrays in JSP" New topic
Author

Unable to display arrays in JSP

Bhaskar Singh
Greenhorn

Joined: Jul 23, 2005
Posts: 15
Hi Guys!
I am Starting to play with scriptless JSP from Head First book. Below is my code:
In the servlet:

[ September 12, 2005: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61774
    
  67

and... ???


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61774
    
  67

Guessing what you are asking about, is the following line correct?



Is musicList a URL mapping in your web.xml?
Bhaskar Singh
Greenhorn

Joined: Jul 23, 2005
Posts: 15
Dear Bibeault,
I named my JSP file as : MusicDisplay.jsp and my servlet as : MusicList.java.
Here's the url mapping in my servlet.Should it be:

so that it will take me to the JSP page to display the view??

It is so warm feeling to get the answer that quick. Thanks Mr. Bibeault!
Bhaskar Singh
Greenhorn

Joined: Jul 23, 2005
Posts: 15
Here's the remaining part, the JSP file:

Problems:
1)The only thing it is displaying is Music is :;instead of Music is :zero1;. However, the code compiles fine!
FYI: I am using JBuilder 2005.
It would be a great if you could advise me some necessary/additional steps that I have to take to display it correctly so that I can go onto other examples in the book.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61774
    
  67



It's usually not a good idea to use relative references. If the JSP is in the root of your web app use:



It may be that the array is not making its way to the JSP. That's the first thing to check which you can do very simply with:


[ September 12, 2005: Message edited by: Bear Bibeault ]
Bhaskar Singh
Greenhorn

Joined: Jul 23, 2005
Posts: 15
No luck!
It says there's no presence of the array. Is it true that I need to initialize my JSP file in the web.xml? I don't see any tags like <jsp-file> </jsp-file>. I need your help to get through this page sir!
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

What are you typing for a URL in the browser's address window?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61774
    
  67

No, you do not need to declare your JSPs.

Are you sure that you are addressing and invoking your servlet? -- which is what I am sure Ben is asking about.
Bhaskar Singh
Greenhorn

Joined: Jul 23, 2005
Posts: 15
http://localhost:8083/WebModule2/MusicDisplay.jsp
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You're bypassing your servlet.

Map your servlet in web.xml and use that mapping to invoke it.

You should never have to hit a JSP directly from a browser when doing MVC.
Bhaskar Singh
Greenhorn

Joined: Jul 23, 2005
Posts: 15
Yes that might be the case since JSP is not taking itself to the servlet to get the string of musics. But is it the right way to invoke a servlet from the JSP page?

MusicList is the name of the servlet file. I tried with MusicList.java too!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61774
    
  67

Yes that might be the case since JSP is not taking itself to the servlet to get the string of musics. But is it the right way to invoke a servlet from the JSP page?


No. It's the other way around. The servlet invokes the JSP. That's why the call to the request dispatcher is in the servlet. You should never invoke the JSP directly when using a servlet controller.

The purpose of a form in a JSP has nothing to do with the current request but only creates a form that can submit a new request.

To invoke your servlet, you would use the mapping that you set up in the web.xml:

http://localhost:8083/WebModule2/musiclist
[ September 12, 2005: Message edited by: Bear Bibeault ]
Bhaskar Singh
Greenhorn

Joined: Jul 23, 2005
Posts: 15
Hurrah! Mr. Bibeault and Mr. Souther, Finally, it worked!! Basically, we just create a JSP page for view, and servlet will do everything, isn't it? I thought JSP page has to call the servlet to display the contents.
One question is still in my mind that how can the JSP show the strings contained in the servlet with only this expression:
since there is nothing like "musicList" defined in the web.xml. Is it because request dispatcher makes a relationship between the servlet and the jsp with the jspfile we mentioned in :

And one more question is :
The only file I run is the Servlet file in the MVC architecture right??
Thanks for taking out the thorn....Yokatta!!(very good in japanese)!!! Now I can move on..
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61774
    
  67

we just create a JSP page for view, and servlet will do everything, isn't it?


Correct. In the pattern that you are following (and a good pattern it is), the servlet gets invoked first and does (or delegates) all the "real work". Then it creates scoped variables with the results, and forwards to a JSP to render the results.

One question is still in my mind that how can the JSP show the strings contained in the servlet


In your servlet, the code:



creates a scoped variable on the request named musciList which references the favoriteMusic array. This scoped variable is what is referenced by the EL expression on the JSP page.

Is it because request dispatcher makes a relationship between the servlet and the jsp


Yes. The servlet forwards the request (which now contains the musicList scoped variable) to the JSP page.

The only file I run is the Servlet file in the MVC architecture right?


Sort of. The servlet should be the only file you directly invoke. The JSP will get executed, but only as the result of being forwarded to by the servlet.
Bhaskar Singh
Greenhorn

Joined: Jul 23, 2005
Posts: 15
Thank you so much Sir. Today was a big day for me. I'm outta my lab now. Bye
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unable to display arrays in JSP