Hi,

I'm trying to do something that's fairly simple in CGI land, but doesn't
work at all with JSP.

I've got a file app.jsp which has loads of HTML tags for displaying a
complex <FORM>. I want to have the action of the form, be this file
itself (app.jsp).

The first time this file is loaded, the implicit request object is null
(I believe) because no request has been made. I want to have app.jsp
check for that condition, and set various form properties to defaults
(e.g. I have code that generates a <INPUT TYPE=SELECT></SELECT> and I
want to have one of the <OPTION SELECTED> by default.)

When the visitor submits the form, the request object should now contain
all the parameters of the form.

My <FORM> building code notices that, and sets all the defaults for the
input items based on the request parameters.

Here's an example:

<SELECT NAME="state">
<% //code to create drop down list of states... and select one of
them...
  String[] statea =
{"AL","AK","AZ","AR","CA","CO","CT","DE","DC","FL","GA","HI","ID",

"IL","IN","IA","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO",

"MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA",

"PR","RI","SC","SD","TX","TN","UT","VT","VI","VA","WA","WV","WI",
                     "WY" };
  String[] statef =
{"Alamaba","Alaska","Arizona","Arkansas","California*","Colorado",
                     "Connecticut*","Delaware","District Of
Columbia","Florida*","Georgia",

"Hawaii","Idaho","Illinois*","Indiana","Iowa","Kansas","Kentucky",

"Louisiana","Maine","Maryland","Massachusetts","Michigan","Minnesota","Mississippi",

                     "Missouri","Montana","Nebraska","Nevada","New
Hampshire","New Jersey*",
                     "New Mexico","New York*","North Carolina","North
Dakota","Ohio*",
                     "Oklahoma","Oregon","Pennsylvania*","Puerto
Rico","Rhode Island",
                     "South Carolina","South
Dakota","Texas*","Tennessee","Utah","Vermont",
                     "Virgin Islands","Virginia","Washington","West
Virginia","Wisconsin","Wyoming"};

  for (int i = 0; i < 53; i++) {
    String value = (String) statea[i];
    String name = (String) statef[i];
    if (request.getParameter("state") != null) { // leaving this check
out, give null pointer exception at compile time
      if (request.getParameter("state").equals(value)){
        out.println ("<OPTION VALUE='" + value + "' SELECTED>" + name +
"</OPTION>");
      }
    } else if ( value.equals("CA") ) { // CA is the default
      out.println ("<OPTION VALUE='" + value + "' SELECTED>" + name +
"</OPTION>");
    } else {
      out.println ("<OPTION VALUE='" + value + "'>" + name +
"</OPTION>");
    }
  }
%>
</SELECT>

My problem is, this approach doesn't work. I get very strange results.
The first time the page loads, it seems to be fine. But loading the page
after submit totally wipes it out. The code for populating the select
menu does not appear to get executed correctly, I only get one entry,
and it's not the entry the person selected!

I must be doing something wrong, but I'm new to Java and JSP as of a
couple weeks ago, and I don't see my mistake. Any suggestions would be
greatly appreciated.

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html

Reply via email to