Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

A minor error involving JSP

Posted on 2004-10-06
5
Medium Priority
?
240 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 200 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The first step to building an amazing About page is to figure out what you want the page to say about your company. You then must grab the attention of the reader, boast a bit, tell a story and let others brag about you. With a little bit of thought…
Steps to fix error: “Couldn’t mount the database that you specified. Specified database: HU-DB; Error code: An Active Manager operation fail”
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

963 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