aspose file tools*
The moose likes JSP and the fly likes doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "doubt" Watch "doubt" New topic
Author

doubt

hemamalini nithyanandam
Ranch Hand

Joined: Oct 24, 2011
Posts: 50

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>


have a nice day - hema
Sunil Sunny
Ranch Hand

Joined: Oct 18, 2011
Posts: 41
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>
Piyush Joshi
Ranch Hand

Joined: Jun 10, 2011
Posts: 207

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?


Piyush
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

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


  • [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    hemamalini nithyanandam
    Ranch Hand

    Joined: Oct 24, 2011
    Posts: 50

    thanks a lot for the suggestions. but still i am getting error as



    will start learning jstl from nowonwards.
    hemamalini nithyanandam
    Ranch Hand

    Joined: Oct 24, 2011
    Posts: 50

    this is my servlet
    Sunil Sunny
    Ranch Hand

    Joined: Oct 18, 2011
    Posts: 41
    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
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61648
        
      67

    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

    Joined: Jun 10, 2011
    Posts: 207

    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

    Joined: Oct 18, 2011
    Posts: 41
    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

    Joined: Oct 24, 2011
    Posts: 50

    still getting error
    hemamalini nithyanandam
    Ranch Hand

    Joined: Oct 24, 2011
    Posts: 50

    will change to jstl .. will change the practice from now onwards. thank you so much for the suggestions
    Sunil Sunny
    Ranch Hand

    Joined: Oct 18, 2011
    Posts: 41
    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

    Joined: Jun 10, 2011
    Posts: 207

    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

    Joined: Oct 24, 2011
    Posts: 50

    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

    Joined: Oct 18, 2011
    Posts: 41
    RequestDispatcher rd = request.getRequestDispatcher("/index.jsp"); Instead of this Try this


    RequestDispatcher rd =getServletContext().getRequestDispatcher("/index.jsp");
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: doubt