• 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
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

doubt

 
Ranch Hand
Posts: 53
Eclipse IDE MySQL Database Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am just a beginner in jsp and servlet. i have written a servlet which returns a list containing string of names.(fromList). i want to display that using option tag in a web page. in my jsp file i have included


but i am getting error. how to display the list of strings using <select><option>
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

hemamalini nithyanandam wrote:i am just a beginner in jsp and servlet. i have written a servlet which returns a list containing string of names.(fromList). i want to display that using option tag in a web page. in my jsp file i have included


but i am getting error. how to display the list of strings using <select><option>



Use this :
<select>
<%
List fromList=(List)request.getAttribute("fromList");

for(int i=0; i< fromList.size();i++)
{

String From=(String)fromList.get(i);
out.println("<option value='" + From + "'></option>");
}
%>
</select>
 
Ranch Hand
Posts: 207
jQuery Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First: Don't use scriptlets in JSP, use tags.
Second: use lower case letter for variables name (i.e. "String from" not "String From" )

But since you are a beginner then playing with scriptlets is ok.

What you are doing wrong here is you are accessing the From variable outside the loop. Remember whatever you write inside <% %> will be turned into java code and whatever you write outside will be shown as plain text on html page.
For example:


Resultant java code will be something like:


out.print("Plain Text") will just print "Plain Text" to your html page.

But if you write:

Resultant java code will be:

you will get 0,1, on the html page.

So now can you think what you need to do to print the list?
 
Sheriff
Posts: 67706
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sunil Sunny wrote:Use this :


No do not. The suggestion to use scriptlets in JSP is a poor one. Scriplets and Java code in a JSP has been discredited for almost 10 years now. To be using Java code in a JSP in 2011 is nothing short of irresponsible.

If you are a beginner to JSP, it's important to learn best practice right off the bat. Learn the JSTL and EL and be sure to completely avoid scriptlets in JSP pages.

It will also be helpful to read these articles:
  • The Secret Life of JSPs
  • The Front Man

  •  
    hemamalini nithyanandam
    Ranch Hand
    Posts: 53
    Eclipse IDE MySQL Database Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    thanks a lot for the suggestions. but still i am getting error as



    will start learning jstl from nowonwards.
     
    hemamalini nithyanandam
    Ranch Hand
    Posts: 53
    Eclipse IDE MySQL Database Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    this is my servlet
     
    Sunil Sunny
    Ranch Hand
    Posts: 42
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    An error occurred at line: 68 in the jsp file: /index.jsp
    From cannot be resolved
    65: {
    66:
    67: String from=(String)fromList.get(i);
    68: out.println("<option value='" + From + "'></option>");
    69: }
    70: %>
    71: </select>




    Look at this carefully "From" is not same as "from".

    String from=(String)fromList.get(i);

    here from is small case.


    68: out.println("<option value='" + From + "'></option>");


    here "From" is camel case.

    So a variable problem
     
    Bear Bibeault
    Sheriff
    Posts: 67706
    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 first step is stop using Java in the JSP. If you insist on proceeding with poor practices then at least stop using out.print()! That's an abomination. The whole purpose of using a JSP is so that you don't need to build up HTML in JaVa strings!

    As a beginner you are making a very poor start in learning JSP by following very poor practices.
     
    Piyush Joshi
    Ranch Hand
    Posts: 207
    jQuery Eclipse IDE Firefox Browser
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    hemamalini nithyanandam wrote:thanks a lot for the suggestions. but still i am getting error as



    will start learning jstl from nowonwards.


    Error message says it all: "From cannot be resolved."
     
    Sunil Sunny
    Ranch Hand
    Posts: 42
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Well Bear you are an author . written books on jQuery. I will surely read your book as i had to interact with Jquery
     
    hemamalini nithyanandam
    Ranch Hand
    Posts: 53
    Eclipse IDE MySQL Database Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    still getting error
     
    hemamalini nithyanandam
    Ranch Hand
    Posts: 53
    Eclipse IDE MySQL Database Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    will change to jstl .. will change the practice from now onwards. thank you so much for the suggestions
     
    Sunil Sunny
    Ranch Hand
    Posts: 42
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    hemamalini nithyanandam wrote:will change to jstl .. will change the practice from now onwards. thank you so much for the suggestions



    Ok that's good to learn jstl but see how to trace the error the error isin line : 64 saying clearly that your fromlist is null i.e. has nothing in it . that's why you are getting an error.
     
    Piyush Joshi
    Ranch Hand
    Posts: 207
    jQuery Eclipse IDE Firefox Browser
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Well, much have been said by Bear.
    But one thing I will point out that for a beginner to better understand how JSP page gets translated, its very helpful to look at the servlet source code generated by the JSP compiler. If you understand translation good enough then you can use Tags and EL very effectively.
     
    hemamalini nithyanandam
    Ranch Hand
    Posts: 53
    Eclipse IDE MySQL Database Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    oh . started with head first servlets and jsp. ..

    in that prog is there anything wrong with servlet code

    and this is my Login class . i checked this class. it works. so prob is either in servlet or in jsp.

    ..
     
    Sunil Sunny
    Ranch Hand
    Posts: 42
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    RequestDispatcher rd = request.getRequestDispatcher("/index.jsp"); Instead of this Try this


    RequestDispatcher rd =getServletContext().getRequestDispatcher("/index.jsp");
     
    Yup, yup, yup. Tiny ad:
    Master Gardener Program
    https://coderanch.com/t/771761/Master-Gardener-Program
    reply
      Bookmark Topic Watch Topic
    • New Topic