• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable to display arrays in JSP

 
Bhaskar Singh
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and... ???
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guessing what you are asking about, is the following line correct?



Is musicList a URL mapping in your web.xml?
 
Bhaskar Singh
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you typing for a URL in the browser's address window?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much Sir. Today was a big day for me. I'm outta my lab now. Bye
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic