Solved

A minor error involving JSP

Posted on 2004-10-06
5
228 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
some methods private some are not 8 196
eclipse luna javeEE perspecive missing 5 96
java operators 3 110
how to add new optional parameter to JSP 1 27
Large Outlook files lead to various unwanted errors and corruption issues. Furthermore, large outlook files can also make Outlook take longer to start-up, search, navigate, and shut-down. So, In this article, i will discuss a method to make your Out…
A customer recently asked me about anti-malware and the different deployment options available for his business. Daily news about cyberattacks, zero-day vulnerabilities, and companies that suffered a security breach made him wonder if the endpoint a…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

911 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

19 Experts available now in Live!

Get 1:1 Help Now