JSP dropdown linked to Java app

Hello all

This has probably been asked before!

I have a jsp page which uses servlets to embed a java class in. This Java class retrieves data from a DB and displays it on the page. However, what I need to do is to create a dropdown menu such as

5

10

30

60

90

This value would be passed to

[if(calculateDifference(jsqlD,dateStr) >= 6)

{

if(calculateDifference(jsqlD,dateStr) < <<VALUE>>)

{

oldL.add(pathL.get(i));

oldL.add(sizeL.get(i));

oldL.add(timeL.get(i));

}//end if

}//end if

So, what I would to know if possible is

1. How to create a dropdown in jsp

2. How to pass the value the user selects to that portion of my code.

Any help would be very much appreciated

Regards

[1064 byte] By [Chucklesa] at [2007-10-2 20:53:09]
# 1

Ok so I found the HTML for creating a dropdown

<SELECT name="test">

<OPTION value="0"> -Select- </OPTION>

<OPTION value="1"> Daffy Duck </OPTION>

<OPTION value="1"> Bugs Bunny </OPTION>

<OPTION value="1"> Tweety </OPTION>

</SELECT>

Could anyone tell me how to pass the option I select to my program?

Chucklesa at 2007-7-13 23:37:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
That requires some research on your part. Study this:1) HTML forms2) the HttpServletRequest object3) the HttpServletRequest.getParameter() methodPut these three together and you have your answer.
gimbal2a at 2007-7-13 23:37:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Thx

I've imported javax.servlet.http.* and created the object

private HttpServletRequest request = null

in the main class

When I use the object I call the getParameter() method as

String str = <class name>.request.getParameter(<dropdown>)

When run on Weblogic 8.1 sp4 a null-pointer exception is generated (unsurprisingly). I understand why the exception is being generated (no value is being passed to it) but I'm not sure how to solve it. Any hints would be appreciated, thanks

Chucklesa at 2007-7-13 23:37:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

> private HttpServletRequest request = null

This is not an object, it's just an empty reference. You have to assign an instance of HttpServletRequest to it and you can only get it from your servlet or JSP. You'll have to pass the request as a parameter to the constructor for example.

So your constructor might look like this:

public YourClass(HttpServletRequest theRequest)

{

request = theRequest;

}

And then in your JSP you can construct the object like this:

YourClass yc = new YourClass(request);

This is all VERY basic stuff, and the fact that you are struggling with this worries me. I would suggest picking up a good book about Java web application development, it's a very though subject to pick up just by hacking around. If you are short on cash, you can download the excellent "Core Servlets & Jsp's" for free:

http://www.coreservlets.com

It's the first edition and somewhat dated, but it might just help you to grasp the subject.

gimbal2a at 2007-7-13 23:37:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

"This is all VERY basic stuff, and the fact that you are struggling with this worries me."

Patronising to say the least. I apologise for calling it an object, just an elementary mistake and when you've got the flu and a throbbing headache concentrating on your work gets very difficult at times, but even a starter to web dev does not need to be made to feel stupid in the way you did.

I looked at what you suggested

YourClass yc = new YourClass(request);

but where exactly is HttpServletRequest instantiated outside my class? Having googled some more it might be the case that I have to use Javabeans as extending the HttpServletRequest interface is more commonly used in servlets, which is not what I'm using. If I can use them in a jsp page then I would very much like to know how.

Chucklesa at 2007-7-13 23:37:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

"Patronising to say the least."

At least we all now know that you are easily offended. You may wish, in the future, to announce the fact that you are a newb. I believe Gimbal2's statement was merely meant as a revelation that you are (indeed) a newbie, as you had failed to previously mention.

linxpdaa at 2007-7-13 23:37:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...