Solved

A minor error involving JSP

Posted on 2004-10-06
5
226 Views
Last Modified: 2011-09-20
I got this jsp example from the internet but the problem is that, it can not show some fields like firstname, lastname, address etc. To my surprise again it can show fields starting from state downwards. So i was wondering if you could help me figure out the problem.

 <% @ page import="java.util.";%>
  <% @ page language="java"%>
   
H <HTML><HEAD><TITLE>Dynamic Form</TITLE></HEAD>
  <BODY>
  <B>Form</B>
  <FORM ACTION=dynamicForm.jsp METHOD=POST>
  <TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0>
  <%  String[] textFields = {"FirstName","LastName","Address","City","Zip"};
     for(int j=0; j<textFields.length; j++){     %>
     <TR>    <TD> <%=textFields[j]%>:                       </TD>
              <TD> <INPUT TYPE=TEXT NAME=<%=textFields[j]%>> </TD>
     </TR>
  <%  }   %>
     <TR>    <TD> State  </TD>
             <TD> <SELECT NAME=State>
  <%  String[] states = {"AZ", "CA", "NM", "MA", "ME", "MD", "..."};
      for(int s=0; s<states.length; s++) {    %>
                     <OPTION><%=states[s]%></OPTION>
  <%  }   %>
                  </SELECT></TD>
     </TR>
     <TR>    <TD> Card Number  </TD>
             <TD> <INPUT TYPE=TEXT NAME=cNumber></TD>
     </TR>
     <TR>    <TD> Card Type  </TD>
             <TD> <SELECT NAME=CardType>
  <%  String[] cTypes = {"Amex", "Visa", "Master Card", "Discovery", "..."};
     for(int t=0; t<cTypes.length; t++) {    %>
                     <OPTION><%=cTypes[t]%></OPTION>
  <%  }   %>
                  </SELECT></TD>
     </TR>
     <TR>    <TD> Expiration Date (MM/DD/YYYY) </TD>
    <TD> <INPUT TYPE=TEXT NAME=cMonth SIZE=2><INPUT TYPE=TEXT NAME=cDay SIZE=2>
             <SELECT NAME=cYear>
             <%  int startYear = 2000;
                 int endYear = 2010;
                 for(int y=startYear; y<endYear; y++) {    %>
                     <OPTION><%=y%></OPTION>
            <%  }   %>
             </SELECT></TD>
     </TR>
  </TABLE>
     <INPUT TYPE=SUBMIT VALUE=Submit>
  </FORM>
  <HR>
  <B>Form Content</B><BR>
  <TABLE>
  <%  Enumeration parameters = request.getParameterNames();
      while(parameters.hasMoreElements()){
         String parameterName = (String)parameters.nextElement();
         String parameterValue = request.getParameter(parameterName); %>
        <TR>
             <TD><%=parameterName%></TD>
             <TD><%=parameterValue%></TD>
         </TR>
  <%  }   %>
  </BODY></HTML>
0
Comment
Question by:pkeketso
  • 3
5 Comments
 
LVL 33

Accepted Solution

by:
sajuks earned 50 total points
ID: 12235462

<%@ page import="java.util.*" %>
  <%@ page language="java"%>
   
H <HTML><HEAD><TITLE>Dynamic Form</TITLE></HEAD>
  <BODY>
  <B>Form</B>
  <FORM ACTION=dynamicForm.jsp METHOD=POST>
  <TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0>
  <%  String[] textFields = {"FirstName","LastName","Address","City","Zip"};
     for(int j=0; j<textFields.length; j++){     %>
     <TR>    <TD> <%=textFields[j]%>:                       </TD>
              <TD> <INPUT TYPE=TEXT NAME=<%=textFields[j]%>> </TD>
     </TR>
  <%  }   %>
     <TR>    <TD> State  </TD>
             <TD> <SELECT NAME=State>
  <%  String[] states = {"AZ", "CA", "NM", "MA", "ME", "MD", "..."};
      for(int s=0; s<states.length; s++) {    %>
                     <OPTION><%=states[s]%></OPTION>
  <%  }   %>
                  </SELECT></TD>
     </TR>
     <TR>    <TD> Card Number  </TD>
             <TD> <INPUT TYPE=TEXT NAME=cNumber></TD>
     </TR>
     <TR>    <TD> Card Type  </TD>
             <TD> <SELECT NAME=CardType>
  <%  String[] cTypes = {"Amex", "Visa", "Master Card", "Discovery", "..."};
     for(int t=0; t<cTypes.length; t++) {    %>
                     <OPTION><%=cTypes[t]%></OPTION>
  <%  }   %>
                  </SELECT></TD>
     </TR>
     <TR>    <TD> Expiration Date (MM/DD/YYYY) </TD>
    <TD> <INPUT TYPE=TEXT NAME=cMonth SIZE=2><INPUT TYPE=TEXT NAME=cDay SIZE=2>
             <SELECT NAME=cYear>
             <%  int startYear = 2000;
                 int endYear = 2010;
                 for(int y=startYear; y<endYear; y++) {    %>
                     <OPTION><%=y%></OPTION>
            <%  }   %>
             </SELECT></TD>
     </TR>
  </TABLE>
     <INPUT TYPE=SUBMIT VALUE=Submit>
  </FORM>
  <HR>
  <B>Form Content</B><BR>
  <TABLE>
  <%  Enumeration parameters = request.getParameterNames();
      while(parameters.hasMoreElements()){
         String parameterName = (String)parameters.nextElement();
         String parameterValue = request.getParameter(parameterName); %>
        <TR>
             <TD><%=parameterName%></TD>
             <TD><%=parameterValue%></TD>
         </TR>
  <%  }   %>
  </BODY></HTML>
   

<HTML><HEAD><TITLE>Dynamic Form</TITLE></HEAD>
  <BODY>
  <B>Form</B>
  <FORM ACTION=dynamicForm.jsp METHOD=POST>
  </BODY></HTML>
0
 
LVL 33

Expert Comment

by:sajuks
ID: 12235464
notice this change
<%@ page import="java.util.*" %>
  <%@ page language="java"%>
0
 
LVL 33

Expert Comment

by:sajuks
ID: 12235501
Also you cannot ensure the order of the parameters being submitted .
You have to use something like
  if (parameterName .equals("param1"))
<TD><%=parameterName%></TD>
            <TD><%=parameterValue%></TD>

}

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In this article, I show you step by step with screenshots to assist you - HOW TO: Deploy and Install the VMware vCenter Server Appliance 6.5 (VCSA 6.5), with some helpful tips along the way.
Are you unable to connect or configure Hotmail email account in Microsoft Outlook 2010, 2007? Or Outlook.com emails are not downloading to Outlook? Lets’ see the problem and resolve Outlook Connector error syncing folder hierarchy (0x8004102A).
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now