This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes JSP and the fly likes ComboBox in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » JSP
Reply Bookmark "ComboBox in jsp" Watch "ComboBox in jsp" New topic
Author

ComboBox in jsp

Nick Price
Greenhorn

Joined: Jan 03, 2009
Posts: 25
I have been working through some of the previous errors i had, and now i want to try and validate that everything is working on the jsp side. I have create a combobox within a form like so


Now when the user selects a value from the above combo box, does it get saved into Events? Because my next step in my PrintResultsServlet, i am doing this


And i just wanted to check that this would work, because i am not getting the desired output yet, so i want to rule this out if i can.
thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56202
    
  13

It's important to use correct terminology. HTML has no "combobox". Please read this for more information.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56202
    
  13

Yes, the request parameter will reflect the user's selection.

And, why are you needlessly casting the result of the getParameter() method?
Nick Price
Greenhorn

Joined: Jan 03, 2009
Posts: 25
Sorry, not quite sure as to what you mean by casting the result. Could you elaborate please?
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Expect of the unnecessary cast to String, I don't see a problem in the logic.

Edit: Sorry, Bear already pointed that out. Didn't see his reply before I posted my reply.


Code depot of a Java EE / JSF developer | JSF / Eclipse / Tomcat kickoff tutorial | DAO kickoff tutorial | I ♥ Unicode
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Nick Price wrote:Sorry, not quite sure as to what you mean by casting the result. Could you elaborate please?

Read the API docs what HttpServletRequest#getParameter() actually returns.
http://java.sun.com/javaee/5/docs/api/javax/servlet/http/HttpServletRequest.html
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56202
    
  13

This line is needlessly complicated:

Nick Price
Greenhorn

Joined: Jan 03, 2009
Posts: 25
Ok, get you, so here is my major problem. So my selecting of events is in a form which is linked to my PrintResults servlet. In my servlet, i do thi


Now that should (or so i think) get the event, send it of to my database through a method, get returned a list and then forward this list back to my PrintResults.jsp. I know i have a successful list getting to my servlet, because at the bottom, i put a main method to test


And this returns me back the three fastest competitors for the 100M Run.
So now back to the jsp. I try to get the request and display it in a table. SO this is what i have done (problably tottally wrong but i have tried so many things)


As far as what happens, i am being returned an empty white page. Can you see where i might be going wrong?
thanks
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458

As said, the cast is unnecessary.


Didn't you probably mean printCompetitors(event) instead? You're now passing the literal String instead of the variable you got.

Completely unnecessary. You should also not be writing scriptlets. Everytime you think you need scriptlets, there's a flaw in your design. Think twice about it then.
Nick Price
Greenhorn

Joined: Jan 03, 2009
Posts: 25
Sorry, meant a hardcoded event, i was previously trying to pass it a String incase my "combo box" was not working. so i have forwarder the list to my jsp. If i dont need a scriplet, how would i display the list in my jsp?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56202
    
  13

What exactly is it that you think that scriptlet is doing for you?

Not only are scriptlets to be avoided like the plague, but this one is completely superfluous.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Nick Price wrote:If i dont need a scriplet, how would i display the list in my jsp?


In ELroughly translates towhich scans for the attribute value associated with the given key in all scopes (page, request, session and application) and thus on its turn roughly translates to the following when you have placed it in the request scope using HttpServletRequest#setAttribute()With other words, your scriptlet is completely unnecessary.
Nick Price
Greenhorn

Joined: Jan 03, 2009
Posts: 25
thats what i am trying to understand better so i can get rid of the scriplet. I thought that scriplet was getting the forwarded list i forward in my servlet. What i am trying to find out is how i can disply my list in my jsp. I have forwarded the list from my servlet to my jsp, so how do i retrieve this?

Sorry, i seem to struggle when it comes to jsp and servlets. Does that mean to get my list, i just place
request.getAttribute("searchResults");
in my jsp?

Or would you have any simple examples or tutorials that can show me the correct way to do this.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Nick Price wrote:thats what i am trying to understand better so i can get rid of the scriplet. I thought that scriplet was getting the forwarded list i forward in my servlet. What i am trying to find out is how i can disply my list in my jsp. I have forwarded the list from my servlet to my jsp, so how do i retrieve this?

You already did it by putting it in the request scope using HttpServletRequest#setAttribute(). The problem lies somewhere else. Your list may be for instance empty. Or the application may have thrown an exception. Debug your code (do a sysout of the list immediately after retrieving it from the DAO), check both the HTML source and the application server logs for some oddities.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Nick Price wrote:
Sorry, i seem to struggle when it comes to jsp and servlets. Does that mean to get my list, i just place
request.getAttribute("searchResults");
in my jsp?
No, just ${searchResults} as you already did.

Or would you have any simple examples or tutorials that can show me the correct way to do this.
This is covered by Sun Java EE tutorial. Also at the JSP/Servlet part of my DAO tutorial you can find examples of how the stuff ought to work.
Nick Price
Greenhorn

Joined: Jan 03, 2009
Posts: 25
Where abouts would i find your DAO tutorial? I cant seem to find it anywhere on this site.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Nick Price wrote:Where abouts would i find your DAO tutorial? I cant seem to find it anywhere on this site.
I thought you already know it. I pointed it out to you before: http://forums.sun.com/thread.jspa?messageID=10560020#10560020
Nick Price
Greenhorn

Joined: Jan 03, 2009
Posts: 25
thank you very much for the help.
Nick Price
Greenhorn

Joined: Jan 03, 2009
Posts: 25
Somthing has happened since last time, i looked at your further exercises part which helped a lot. I am pretty sure that my list is getting to my servlet, cos i can display it if i add a main to my servlet class. I founf there was a problem when i called it up in my jsp. I was using the wrong name. So i changed it and now i get an error, which seems more hopeful than before. It displays


Then some stacktrace. An then it gives me the root cause


Not understanding as to why it needs to be converted to an integer, or if its trying to convert to an integer. Any clues about this error?
cheers
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2552
    
    2

Yes the clue is that you cannot have something like printResult.nationality where printResult is a String object.


SCJP, SCWCD.
|Asking Good Questions|
Nick Price
Greenhorn

Joined: Jan 03, 2009
Posts: 25
Sorry, not quite understanding, printResult shouldnt be a String object, it should be a list of my own object Type. So how can i get rid of this error?
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458

Shouldn't the items be ${printResults} or so?
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: ComboBox in jsp
 
Similar Threads
servlet programming problem in Eclipse
Value from select element is null in request
help in inserting checkbox value into data base
How many attributes can a servlet have?
Forwarding data to jsp from servlet