*
The moose likes JSP and the fly likes Capturing form values in scriplet within the same JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Capturing form values in scriplet within the same JSP" Watch "Capturing form values in scriplet within the same JSP" New topic
Author

Capturing form values in scriplet within the same JSP

Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 319
I have a form with radio buttons and a Submit button. Within that JSP, I wish to utilize the selected radio button value in a scriplet.
So far, I am failing due to null values.

What is the best way to approach this?

Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60822
    
  65

The scriptlets in your JSP* execute on the server long before the resulting HTML is sent to the browser, where the form elements are interpreted, presented to the user, and allow input. Trying to get those value before they even exist obviously is not going to work. You might want to check out this article for a run-down on how JSP operates.

To deal with the form values, you need to submit the form to a servlet where the values can be processed.



* Scriptlets are over 6 years out-of-date. You should be concentrating on using modern technology in your JSPs' namely, the JSTL and EL.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 319
What you describe is exactly my understanding; but I did find an example on the web where someone was doing just what I have described.
As you know, the entire code was not there, but the indication was that you could do this. I thought I was just not doing it "right" and that
was the reason my attempt was failing. See replies 3, 4, and 5 to http://forums-beta.sun.com/thread.jspa?messageID=1146080.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60822
    
  65

Most likely what you are seeing is a JSP with a form that submits to the same JSP. The second time around, the values will be there, but obviously not the first.

That's a very old-fashioned and discredited practice that you should not be following. Here's another article that details modern web app structures.

It sounds as if you are finding some very old information on the web that's very outdated and leading you down the path of poor and outdated practices.
Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 319
Is there a simple way to direct a jsp to one of two pages depending on user input without going to the servlet? I'm going to read every article suggested; but meanwhile, the object is to do one of two things. After choosing the thing I want to do, then I need to display a page to get more information before actually performing the action. Do I need two servlets then so I go to two different doPost methods, one after the initial choice and another when the details are provided?

My thought was to forward to the initial choice and then get the details; but I can't do that without going through a servlet. Do I need another servlet then to process the details?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60822
    
  65

To be honest, I'd be making that decision on the client using JavaScript and/or Ajax to show the "extra" info in the same JSP.

But if that's a bit advanced for the time being, you can cause the action of a form to change prior to submission so that the redirect will be to the appropriate "next" page (hint: via an onsubmit handler).

Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 319
Will check into the onsubmit handler though client says no JavaScript. That sounds like java script to me.

Actually, I need a new input page I think for the "extra" information as the information is quite different for the two options; could look quite messy to have both on the same page. Here is where an "include" could come in handy I guess. Hmmm....
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60822
    
  65

Sigh. Another Luddite?

Yeah, without JavaScript you'll have a hard time writing anything other than basic forms and links.
Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 319
My client thinks his users will have JavaScript disabled. That used to be common advice from security experts; is that no longer the case?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60822
    
  65

Not with people used to using web apps. It may still be more common among those whose aim is to look at sites with pictures of kittens.

Sites like gmail or google maps are not possible without JavaScript.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Capturing form values in scriplet within the same JSP
 
Similar Threads
JSP Radio /CheckBox
print database values with radio button
retreaving the value in servlet which is set in session in a jsp page
Form submission and iFrame
radio button in struts 2