Solved

A minor error involving JSP

Posted on 2004-10-06
5
233 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: 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

Configuring Remote Assistance for use with SCCM
This article is a collection of issues that people face from time to time and possible solutions to those issues. I hope you enjoy reading it.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

734 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